forked from adampointer/cali
-
Notifications
You must be signed in to change notification settings - Fork 7
/
command.go
68 lines (58 loc) · 1.71 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cali
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)
// Command is the actual command run by the cli and essentially just wraps cobra.Command and
// has an associated Task
type Command struct {
name string
RunTask *Task
cobra *cobra.Command
}
// SetShort sets the short description of the command
func (c *Command) SetShort(s string) {
c.cobra.Short = s
}
// SetLong sets the long description of the command
func (c *Command) SetLong(l string) {
c.cobra.Long = l
}
// setPreRun sets the cobra.Command.PreRun function
func (c *Command) setPreRun(f cobraFunc) {
c.cobra.PreRun = f
}
// setRun sets the cobra.Command.Run function
func (c *Command) setRun(f cobraFunc) {
c.cobra.Run = f
}
// Task is something executed by a command
func (c *Command) Task(def interface{}) *Task {
t := &Task{DockerClient: NewDockerClient()}
switch d := def.(type) {
case string:
t.SetImage(d)
t.SetFunc(defaultTaskFunc)
case TaskFunc:
t.SetFunc(d)
default:
// Slightly unidiomatic to blow up here rather than return an error
// choosing to so as to keep the API uncluttered and also if you get here it's
// an implementation error rather than a runtime error.
log.Fatalf("Unknown Task type. Must either be an image (string) or a TaskFunc")
}
c.RunTask = t
return t
}
// Flags returns the FlagSet for the command and is used to set new flags for the command
func (c *Command) Flags() *flag.FlagSet {
return c.cobra.PersistentFlags()
}
// BindFlags needs to be called after all flags for a command have been defined
func (c *Command) BindFlags() {
c.Flags().VisitAll(func(f *flag.Flag) {
_ = myFlags.BindPFlag(f.Name, f)
myFlags.SetDefault(f.Name, f.DefValue)
})
}