Hide the plugin command from help output

This commit is contained in:
Chris Bednarski 2015-08-20 16:43:30 -07:00
parent bfe5b5b4b6
commit 7acdc1b6af
2 changed files with 49 additions and 1 deletions

23
main.go
View File

@ -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.

View File

@ -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