-
Notifications
You must be signed in to change notification settings - Fork 6
/
command.go
112 lines (97 loc) · 3.24 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
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
package console
import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const (
// CommandFilterKey should be used as a key to in a cobra.Annotation map.
// The value will be used as a filter to disable commands when the console
// calls the Filter("name") method on the console.
// The string value will be comma-splitted, with each split being a filter.
CommandFilterKey = "console-hidden"
)
// Commands is a simple function a root cobra command containing an arbitrary tree
// of subcommands, along with any behavior parameters normally found in cobra.
// This function is used by each menu to produce a new, blank command tree after
// each execution run, as well as each command completion invocation.
type Commands func() *cobra.Command
// SetCommands requires a function returning a tree of cobra commands to be used.
func (m *Menu) SetCommands(cmds Commands) {
m.mutex.RLock()
defer m.mutex.RUnlock()
m.cmds = cmds
}
// HideCommands - Commands, in addition to their menus, can be shown/hidden based
// on a filter string. For example, some commands applying to a Windows host might
// be scattered around different groups, but, having all the filter "windows".
// If "windows" is used as the argument here, all windows commands for the current
// menu are subsequently hidden, until ShowCommands("windows") is called.
func (c *Console) HideCommands(filters ...string) {
next:
for _, filt := range filters {
for _, filter := range c.filters {
if filt == filter {
continue next
}
}
if filt != "" {
c.filters = append(c.filters, filt)
}
}
}
// ShowCommands - Commands, in addition to their menus, can be shown/hidden based
// on a filter string. For example, some commands applying to a Windows host might
// be scattered around different groups, but, having all the filter "windows".
// Use this function if you have previously called HideCommands("filter") and want
// these commands to be available back under their respective menu.
func (c *Console) ShowCommands(filters ...string) {
c.mutex.RLock()
defer c.mutex.RUnlock()
updated := make([]string, 0)
if len(filters) == 0 {
c.filters = updated
return
}
next:
for _, filt := range c.filters {
for _, filter := range filters {
if filt == filter {
continue next
}
}
updated = append(updated, filt)
}
c.filters = updated
}
// resetFlagsDefaults resets all flags to their default values.
//
// Slice flags accumulate per execution (and do not reset),
//
// so we must reset them manually.
//
// Example:
//
// Given cmd.Flags().StringSlice("comment", nil, "")
// If you run a command with --comment "a" --comment "b" you will get
// the expected [a, b] slice.
//
// If you run a command again with no --comment flags, you will get
// [a, b] again instead of an empty slice.
//
// If you run the command again with --comment "c" --comment "d" flags,
// you will get [a, b, c, d] instead of just [c, d].
func resetFlagsDefaults(target *cobra.Command) {
target.Flags().VisitAll(func(flag *pflag.Flag) {
flag.Changed = false
switch value := flag.Value.(type) {
case pflag.SliceValue:
var res []string
if len(flag.DefValue) > 0 && flag.DefValue != "[]" {
res = append(res, flag.DefValue)
}
value.Replace(res)
default:
flag.Value.Set(flag.DefValue)
}
})
}