-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
59 lines (51 loc) · 1.01 KB
/
cli.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
package cli
import (
"os"
"github.com/jvikstedt/alarmii/cli/action"
"github.com/jvikstedt/alarmii/cli/service"
"github.com/urfave/cli"
)
var jobAction = action.NewJob(os.Stdout, service.Job{})
var actionMap = map[string]action.Execute{
"job_add": jobAction.Add,
"job_list": jobAction.List,
}
// Run executes cli app
func Run() {
app := cli.NewApp()
app.Name = "Alarmii"
app.Usage = ""
setupActions(app)
app.Run(os.Args)
}
func setupActions(app *cli.App) {
app.Commands = []cli.Command{
{
Name: "job",
Subcommands: []cli.Command{
{
Name: "add",
Action: getAction("job_add"),
},
{
Name: "list",
Action: getAction("job_list"),
},
},
},
}
}
func getAction(name string) func(*cli.Context) error {
return func(c *cli.Context) error {
context := action.Context{}
context.Args = c.Args()
flags := map[string]string{}
for _, v := range c.FlagNames() {
b := c.String(v)
flags[v] = b
}
context.Flags = flags
actionMap[name](context)
return nil
}
}