-
Notifications
You must be signed in to change notification settings - Fork 8
/
command.go
52 lines (41 loc) · 976 Bytes
/
command.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
package command
import (
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// FlagFlagger wraps a method of adding a flag to a FlagSet
type FlagFlagger interface {
AddFlags(*pflag.FlagSet)
}
// CmdFlagger wraps a method of adding a flag to a Command
type CmdFlagger interface {
AddFlags(*cobra.Command)
}
// Describer describes a CLI application
type Describer interface {
Usage() string
Version() string
Example() string
}
// RunOrDie runs the cobra command or panics
func RunOrDie(usage, version, example string, run func(*cobra.Command, []string), ff []FlagFlagger, cf []CmdFlagger) {
cmd := &cobra.Command{
Use: usage,
Version: version,
Example: example,
Run: func(cmd *cobra.Command, args []string) {
run(cmd, args)
},
}
flags := cmd.PersistentFlags()
for _, flagger := range ff {
flagger.AddFlags(flags)
}
for _, flagger := range cf {
flagger.AddFlags(cmd)
}
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}