-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
61 lines (51 loc) · 881 Bytes
/
cmd.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
package main
import "fmt"
type Cmd struct {
Name string
Desc string
Args []string
Aliases []string
Fn func(args []string) error
}
var config Config
var CmdList []*Cmd
func init() {
CmdList = []*Cmd{
CmdVersion,
CmdInit,
CmdHelp,
CmdRun,
}
}
func CommandDispatch(args []string) error {
var command *Cmd
var name string
var commandArgs = make([]string, 0)
var err error
if len(args) < 2 {
name = "help"
} else {
name = args[1]
}
for _, cmd := range CmdList {
if cmd.Name == name {
command = cmd
break
}
}
LoadArgs()
if command == nil {
return fmt.Errorf("Command \"%s\" not found", name)
} else if name != "init" &&
name != "help" &&
name != "version" {
config, err = LoadConfig()
if err != nil {
return err
}
err = command.Fn(commandArgs)
return err
}
err = command.Fn(commandArgs)
return err
}