From 7acdc1b6afee2446769c583e553d64e78ef2b466 Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Thu, 20 Aug 2015 16:43:30 -0700 Subject: [PATCH] Hide the plugin command from help output --- main.go | 23 ++++++++++++++++++++++- main_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index a0d3190d1..ac61d211b 100644 --- a/main.go +++ b/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. diff --git a/main_test.go b/main_test.go index 7a14bed19..2b6686b38 100644 --- a/main_test.go +++ b/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