govendor fetch github.com/mitchellh/cli/...

This commit is contained in:
Martin Atkins 2017-09-25 16:57:52 -07:00
parent d78b575536
commit 564f5134e3
2 changed files with 43 additions and 17 deletions

View File

@ -59,8 +59,20 @@ type CLI struct {
// For example, if the key is "foo bar", then to access it our CLI // For example, if the key is "foo bar", then to access it our CLI
// must be accessed with "./cli foo bar". See the docs for CLI for // must be accessed with "./cli foo bar". See the docs for CLI for
// notes on how this changes some other behavior of the CLI as well. // notes on how this changes some other behavior of the CLI as well.
//
// The factory should be as cheap as possible, ideally only allocating
// a struct. The factory may be called multiple times in the course
// of a command execution and certain events such as help require the
// instantiation of all commands. Expensive initialization should be
// deferred to function calls within the interface implementation.
Commands map[string]CommandFactory Commands map[string]CommandFactory
// HiddenCommands is a list of commands that are "hidden". Hidden
// commands are not given to the help function callback and do not
// show up in autocomplete. The values in the slice should be equivalent
// to the keys in the command map.
HiddenCommands []string
// Name defines the name of the CLI. // Name defines the name of the CLI.
Name string Name string
@ -116,6 +128,7 @@ type CLI struct {
autocomplete *complete.Complete autocomplete *complete.Complete
commandTree *radix.Tree commandTree *radix.Tree
commandNested bool commandNested bool
commandHidden map[string]struct{}
subcommand string subcommand string
subcommandArgs []string subcommandArgs []string
topFlags []string topFlags []string
@ -173,7 +186,7 @@ func (c *CLI) Run() (int, error) {
// Just print the help when only '-h' or '--help' is passed. // Just print the help when only '-h' or '--help' is passed.
if c.IsHelp() && c.Subcommand() == "" { if c.IsHelp() && c.Subcommand() == "" {
c.HelpWriter.Write([]byte(c.HelpFunc(c.Commands) + "\n")) c.HelpWriter.Write([]byte(c.HelpFunc(c.helpCommands(c.Subcommand())) + "\n"))
return 0, nil return 0, nil
} }
@ -216,7 +229,7 @@ func (c *CLI) Run() (int, error) {
raw, ok := c.commandTree.Get(c.Subcommand()) raw, ok := c.commandTree.Get(c.Subcommand())
if !ok { if !ok {
c.HelpWriter.Write([]byte(c.HelpFunc(c.helpCommands(c.subcommandParent())) + "\n")) c.HelpWriter.Write([]byte(c.HelpFunc(c.helpCommands(c.subcommandParent())) + "\n"))
return 1, nil return 127, nil
} }
command, err := raw.(CommandFactory)() command, err := raw.(CommandFactory)()
@ -298,6 +311,14 @@ func (c *CLI) init() {
c.HelpWriter = os.Stderr c.HelpWriter = os.Stderr
} }
// Build our hidden commands
if len(c.HiddenCommands) > 0 {
c.commandHidden = make(map[string]struct{})
for _, h := range c.HiddenCommands {
c.commandHidden[h] = struct{}{}
}
}
// Build our command tree // Build our command tree
c.commandTree = radix.New() c.commandTree = radix.New()
c.commandNested = false c.commandNested = false
@ -398,32 +419,32 @@ func (c *CLI) initAutocomplete() {
func (c *CLI) initAutocompleteSub(prefix string) complete.Command { func (c *CLI) initAutocompleteSub(prefix string) complete.Command {
var cmd complete.Command var cmd complete.Command
walkFn := func(k string, raw interface{}) bool { walkFn := func(k string, raw interface{}) bool {
// Keep track of the full key so that we can nest further if necessary
fullKey := k
if len(prefix) > 0 { if len(prefix) > 0 {
// If we have a prefix, trim the prefix + 1 (for the space) // If we have a prefix, trim the prefix + 1 (for the space)
// Example: turns "sub one" to "one" with prefix "sub" // Example: turns "sub one" to "one" with prefix "sub"
k = k[len(prefix)+1:] k = k[len(prefix)+1:]
} }
// Keep track of the full key so that we can nest further if necessary if idx := strings.Index(k, " "); idx >= 0 {
fullKey := k // If there is a space, we trim up to the space. This turns
// "sub sub2 sub3" into "sub". The prefix trim above will
if idx := strings.LastIndex(k, " "); idx >= 0 { // trim our current depth properly.
// If there is a space, we trim up to the space
k = k[:idx] k = k[:idx]
} }
if idx := strings.LastIndex(k, " "); idx >= 0 {
// This catches the scenario just in case where we see "sub one"
// before "sub". This will let us properly setup the subcommand
// regardless.
k = k[idx+1:]
}
if _, ok := cmd.Sub[k]; ok { if _, ok := cmd.Sub[k]; ok {
// If we already tracked this subcommand then ignore // If we already tracked this subcommand then ignore
return false return false
} }
// If the command is hidden, don't record it at all
if _, ok := c.commandHidden[fullKey]; ok {
return false
}
if cmd.Sub == nil { if cmd.Sub == nil {
cmd.Sub = complete.Commands(make(map[string]complete.Command)) cmd.Sub = complete.Commands(make(map[string]complete.Command))
} }
@ -571,6 +592,11 @@ func (c *CLI) helpCommands(prefix string) map[string]CommandFactory {
panic("not found: " + k) panic("not found: " + k)
} }
// If this is a hidden command, don't show it
if _, ok := c.commandHidden[k]; ok {
continue
}
result[k] = raw.(CommandFactory) result[k] = raw.(CommandFactory)
} }

6
vendor/vendor.json vendored
View File

@ -1723,10 +1723,10 @@
"revisionTime": "2017-01-23T01:43:24Z" "revisionTime": "2017-01-23T01:43:24Z"
}, },
{ {
"checksumSHA1": "KXrCoifaKi3Wy4zbCfXTtM/FO48=", "checksumSHA1": "UIqCj7qI0hhIMpAhS9YYqs2jD48=",
"path": "github.com/mitchellh/cli", "path": "github.com/mitchellh/cli",
"revision": "b633c78680fa6fb27ac81694f38c28f79602ebd9", "revision": "65fcae5817c8600da98ada9d7edf26dd1a84837b",
"revisionTime": "2017-08-14T15:07:37Z" "revisionTime": "2017-09-08T18:10:43Z"
}, },
{ {
"checksumSHA1": "ttEN1Aupb7xpPMkQLqb3tzLFdXs=", "checksumSHA1": "ttEN1Aupb7xpPMkQLqb3tzLFdXs=",