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{
|
cli := &cli.CLI{
|
||||||
Args: args,
|
Args: args,
|
||||||
Commands: Commands,
|
Commands: Commands,
|
||||||
HelpFunc: cli.BasicHelpFunc("packer"),
|
HelpFunc: excludeHelpFunc(Commands, []string{"plugin"}),
|
||||||
HelpWriter: os.Stdout,
|
HelpWriter: os.Stdout,
|
||||||
Version: Version,
|
Version: Version,
|
||||||
}
|
}
|
||||||
|
@ -195,6 +195,27 @@ func wrappedMain() int {
|
||||||
return exitCode
|
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
|
// extractMachineReadable checks the args for the machine readable
|
||||||
// flag and returns whether or not it is on. It modifies the args
|
// flag and returns whether or not it is on. It modifies the args
|
||||||
// to remove this flag.
|
// to remove this flag.
|
||||||
|
|
27
main_test.go
27
main_test.go
|
@ -3,9 +3,36 @@ package main
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"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) {
|
func TestExtractMachineReadable(t *testing.T) {
|
||||||
var args, expected, result []string
|
var args, expected, result []string
|
||||||
var mr bool
|
var mr bool
|
||||||
|
|
Loading…
Reference in New Issue