-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.go
73 lines (59 loc) · 1.62 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"errors"
"github.com/jroimartin/gocui"
"strings"
)
type Cmd interface {
Run(*gocui.View) error
Validator([]string) error
SetArgs([]string)
GetArgs() []string
Usage() string
Description() string
}
var validCmds = map[string]Cmd{
"help": &Help{},
"get": &Get{},
}
var jobStates = map[string]bool{
"ready": true,
"delayed": true,
"buried": true,
}
var (
ErrInvalidSyntax = errors.New("Invalid syntax")
ErrInvalidCommand = errors.New("Invalid command. Type help for a list of commands")
ErrInvalidJobState = errors.New("Invalid job state. Should be one of ready/delayed/buried")
)
func ParseCmd(c string) (cmd Cmd, err error) {
parts := strings.Split(c, " ")
cmd, exists := validCmds[parts[0]]
if !exists {
return cmd, ErrInvalidCommand
}
if err := cmd.Validator(parts); err != nil {
if err == ErrInvalidSyntax {
return cmd, errors.New(err.Error() + ": Usage '" + cmd.Usage() + "'")
}
return cmd, err
}
//Add additional cmd args if specified
if len(parts) > 1 {
cmd.SetArgs(parts[1:])
debugLog("Set cmd args to ", cmd.GetArgs())
}
return cmd, nil
}
func getNext(state string) (uint64, []byte, error) {
switch state {
case "ready":
return cTubes.Conns[cTubes.SelectedIdx].PeekReady()
case "delayed":
return cTubes.Conns[cTubes.SelectedIdx].PeekDelayed()
case "buried":
return cTubes.Conns[cTubes.SelectedIdx].PeekBuried()
}
debugLog("Invalid state ", state)
return 0, []byte(""), ErrInvalidJobState
}