/
standalone.go
113 lines (90 loc) · 2.06 KB
/
standalone.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"strings"
"github.com/bettercallshao/kwt/pkg/cmd"
"github.com/bettercallshao/kwt/pkg/menu"
"github.com/urfave/cli/v2"
)
const DRY = "dry"
func commands() []*cli.Command {
commands := make([]*cli.Command, 0)
dryFlag := &cli.BoolFlag{
Name: DRY,
Usage: "render command but don't run",
}
menuMap := menu.Map()
// we need the ordering provided by List()
for _, name := range menu.List() {
menu := menuMap[name]
subCommands := make([]*cli.Command, 0)
for _, action := range menu.Actions {
flags := make([]cli.Flag, 0)
for _, param := range action.Params {
flags = append(
flags,
&cli.StringFlag{
Name: param.Name,
Value: param.Value,
Usage: param.Help,
Aliases: param.Aliases,
},
)
}
flags = append(flags, dryFlag)
subCommands = append(
subCommands,
&cli.Command{
Name: action.Name,
Usage: action.Help,
Flags: flags,
Action: act,
Aliases: action.Aliases,
},
)
}
commands = append(
commands,
&cli.Command{
Name: menu.Name,
Usage: menu.Help + " " + menu.Path,
Subcommands: subCommands,
Aliases: menu.Aliases,
},
)
}
return commands
}
func act(c *cli.Context) error {
dry := c.Bool(DRY)
// only HelpName field under command can we find parent command
name := strings.Split(c.Command.HelpName, " ")[1]
loaded, err := menu.Load(name)
if err != nil {
cli.Exit("failed to open menu "+name, -1)
}
action := menu.Action{}
for _, item := range loaded.Actions {
if item.Name == c.Command.Name {
action = item
break
}
}
if action.Name == "" {
cli.Exit("failed to find action "+c.Command.Name, -1)
}
for idx, param := range action.Params {
action.Params[idx].Value = c.String(param.Name)
}
input, err := menu.Render(action)
if err != nil {
cli.Exit(fmt.Sprintf("action failed to render %+v", action), -1)
}
if dry {
fmt.Printf("template: %s\n", action.Template)
fmt.Printf("rendered: %s\n", input)
return nil
}
cmd.Run(input, nil)
return nil
}