Hide the plugin command from help output
This commit is contained in:
parent
bfe5b5b4b6
commit
7acdc1b6af
23
main.go
23
main.go
|
@ -181,7 +181,7 @@ func wrappedMain() int {
|
|||
cli := &cli.CLI{
|
||||
Args: args,
|
||||
Commands: Commands,
|
||||
HelpFunc: cli.BasicHelpFunc("packer"),
|
||||
HelpFunc: excludeHelpFunc(Commands, []string{"plugin"}),
|
||||
HelpWriter: os.Stdout,
|
||||
Version: Version,
|
||||
}
|
||||
|
@ -195,6 +195,27 @@ func wrappedMain() int {
|
|||
return exitCode
|
||||
}
|
||||
|
||||
// excludeHelpFunc filters commands we don't want to show from the list of
|
||||
// commands displayed in packer's help text.
|
||||
func excludeHelpFunc(commands map[string]cli.CommandFactory, exclude []string) cli.HelpFunc {
|
||||
// Make search slice into a map so we can use use the `if found` idiom
|
||||
// instead of a nested loop.
|
||||
var excludes = make(map[string]interface{}, len(exclude))
|
||||
for _, item := range exclude {
|
||||
excludes[item] = nil
|
||||
}
|
||||
|
||||
// Create filtered list of commands
|
||||
helpCommands := []string{}
|
||||
for command := range commands {
|
||||
if _, found := excludes[command]; !found {
|
||||
helpCommands = append(helpCommands, command)
|
||||
}
|
||||
}
|
||||
|
||||
return cli.FilteredHelpFunc(helpCommands, cli.BasicHelpFunc("packer"))
|
||||
}
|
||||
|
||||
// extractMachineReadable checks the args for the machine readable
|
||||
// flag and returns whether or not it is on. It modifies the args
|
||||
// to remove this flag.
|
||||
|
|
27
main_test.go
27
main_test.go
|
@ -3,9 +3,36 @@ package main
|
|||
import (
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/mitchellh/packer/command"
|
||||
)
|
||||
|
||||
func TestExcludeHelpFunc(t *testing.T) {
|
||||
commands := map[string]cli.CommandFactory{
|
||||
"build": func() (cli.Command, error) {
|
||||
return &command.BuildCommand{
|
||||
Meta: command.Meta{},
|
||||
}, nil
|
||||
},
|
||||
|
||||
"fix": func() (cli.Command, error) {
|
||||
return &command.FixCommand{
|
||||
Meta: command.Meta{},
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
||||
helpFunc := excludeHelpFunc(commands, []string{"fix"})
|
||||
helpText := helpFunc(commands)
|
||||
|
||||
if strings.Contains(helpText, "fix") {
|
||||
t.Fatal("Found fix in help text even though we excluded it: \n\n%s\n\n", helpText)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractMachineReadable(t *testing.T) {
|
||||
var args, expected, result []string
|
||||
var mr bool
|
||||
|
|
Loading…
Reference in New Issue