-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
170 lines (140 loc) · 2.95 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bufio"
"fmt"
"github.com/mitchellh/cli"
"github.com/sirupsen/logrus"
"os"
)
func main() {
ui := &cli.ColoredUi{
InfoColor: cli.UiColorGreen,
ErrorColor: cli.UiColorRed,
WarnColor: cli.UiColorYellow,
Ui: &cli.BasicUi{
Reader: bufio.NewReader(os.Stdin),
Writer: os.Stdout,
ErrorWriter: os.Stderr,
},
}
c := cli.NewCLI("hello-hc-cli", "0.0.1")
c.Args = os.Args[1:]
c.AutocompleteInstall = "auto-comp"
c.AutocompleteUninstall = "uninstall-auto-comp"
c.Commands = map[string]cli.CommandFactory{
// default command when key is blank
"": func() (cli.Command, error) {
return &defaultCommand{ui}, nil
},
"flag": func() (cli.Command, error) {
return &flagCommand{ui}, nil
},
"bar": func() (cli.Command, error) {
return &barCommand{ui}, nil
},
"foo": func() (cli.Command, error) {
return &fooCommand{}, nil
},
"foo f1": func() (cli.Command, error) {
return &f1Command{}, nil
},
"foo f2": func() (cli.Command, error) {
return &f2Command{}, nil
},
}
exitStatus, err := c.Run()
if err != nil {
logrus.Error(err)
}
os.Exit(exitStatus)
}
type defaultCommand struct {
UI cli.Ui
}
func (c *defaultCommand) Synopsis() string {
return "default"
}
func (c *defaultCommand) Help() string {
return `default help`
}
func (c *defaultCommand) Run(args []string) int {
c.UI.Info(fmt.Sprintf("default run with %+v", args))
return 0
}
type flagCommand struct {
UI cli.Ui
}
func (c *flagCommand) Synopsis() string {
return "flag"
}
func (c *flagCommand) Help() string {
return `
flag long help
`
}
func (c *flagCommand) Run(args []string) int {
logrus.Infof("flag run with %+v", args)
return 0
}
type barCommand struct {
UI cli.Ui
}
func (c *barCommand) Synopsis() string {
return "bar"
}
func (c *barCommand) Help() string {
return `
bar help
`
}
func (c *barCommand) Run(args []string) int {
logrus.Infof("bar run with %+v", args)
name, err := c.UI.Ask("your name:")
if err != nil {
c.UI.Error(err.Error())
}
c.UI.Warn(fmt.Sprintf("name: %s", name))
pass, err := c.UI.AskSecret("login password:")
if err != nil {
c.UI.Error(err.Error())
}
c.UI.Error(fmt.Sprintf("password: %s", pass))
c.UI.Info("name and pass got")
return 0
}
type fooCommand struct {
}
func (c *fooCommand) Synopsis() string {
return "foo"
}
func (c *fooCommand) Help() string {
return `foo help`
}
func (c *fooCommand) Run(args []string) int {
logrus.Infof("run foo with %+v", args)
return cli.RunResultHelp
}
type f1Command struct {
}
func (c *f1Command) Synopsis() string {
return "foo f1"
}
func (c *f1Command) Help() string {
return `foo f1 help`
}
func (c *f1Command) Run(args []string) int {
logrus.Infof("foo f1 run with %+v", args)
return 0
}
type f2Command struct {
}
func (c *f2Command) Synopsis() string {
return "foo f2"
}
func (c *f2Command) Help() string {
return `foo f2 help`
}
func (c *f2Command) Run(args []string) int {
logrus.Infof("foo f2 run with %+v", args)
return 0
}