/
lims2.go
86 lines (67 loc) · 1.53 KB
/
lims2.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
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"errors"
"fmt"
"os"
"github.com/docopt/docopt-go"
"github.com/gosexy/yaml"
)
var (
//所有命令
commandsMap = map[interface{}]*Command{
"up": cmdUp,
"get-cron": cmdGetCron,
"get-sphinx": cmdGetSphinx,
"update-cron": cmdUpdateCron,
}
//定义操作容器的名称
containerName = "lims2"
)
type Command struct {
command string
args map[string]interface{}
Run func(cmd *Command) int
info *yaml.Yaml
}
func main() {
usage := `Lims2 Autodeploy.
Usage:
lims2 <command> [<args>...]
lims2 -h | --help
lims2 --version
Options:
-h --help Show this screen.
--version Show version.
Commands:
up Create and start containers
get-cron Output crontab
get-sphinx Output sphinxsearch config
update-cron Output to /etc/cron.d/lims2 in container.`
var defaultArgs []string
if len(os.Args[1:]) == 0 {
defaultArgs = []string{
"-h",
}
} else {
defaultArgs = os.Args[1:]
}
args, _ := docopt.Parse(usage, defaultArgs, true, "Lims2 Autodeploy 0.1", true)
command := args["<command>"]
subCommand, exits := commandsMap[command]
//如果传值错误
if !exits {
docopt.Parse(usage, []string{"-h"}, true, "Lims2 Autodeploy 0.1", true)
}
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
os.Exit(1)
}
}()
if _, err := os.Stat("main.yml"); err == nil {
subCommand.info, _ = yaml.Open("main.yml")
} else {
panic(errors.New("main.yml not exists."))
}
subCommand.Run(subCommand)
}