Implement the help method
This commit is contained in:
parent
91c524c7ec
commit
4d10489f51
|
@ -1,7 +1,12 @@
|
|||
// The packer package contains the core components of Packer.
|
||||
package packer
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A command is a runnable sub-command of the `packer` application.
|
||||
// When `packer` is called with the proper subcommand, this will be
|
||||
|
@ -11,6 +16,7 @@ import "os"
|
|||
// Environment struct.
|
||||
type Command interface {
|
||||
Run(env *Environment, args []string) int
|
||||
Synopsis() string
|
||||
}
|
||||
|
||||
// The environment struct contains all the state necessary for a single
|
||||
|
@ -71,5 +77,32 @@ func (e *Environment) Ui() Ui {
|
|||
|
||||
// Prints the CLI help to the UI.
|
||||
func (e *Environment) PrintHelp() {
|
||||
e.ui.Say("Bad.\n")
|
||||
// Created a sorted slice of the map keys and record the longest
|
||||
// command name so we can better format the output later.
|
||||
commandKeys := make([]string, len(e.command))
|
||||
i := 0
|
||||
maxKeyLen := 0
|
||||
for key, _ := range e.command {
|
||||
commandKeys[i] = key
|
||||
if len(key) > maxKeyLen {
|
||||
maxKeyLen = len(key)
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
// Sort the keys
|
||||
sort.Strings(commandKeys)
|
||||
|
||||
e.ui.Say("usage: packer [--version] [--help] <command> [<args>]\n\n")
|
||||
e.ui.Say("Available commands are:\n")
|
||||
for _, key := range commandKeys {
|
||||
command := e.command[key]
|
||||
|
||||
// Pad the key with spaces so that they're all the same width
|
||||
key = fmt.Sprintf("%v%v", key, strings.Repeat(" ", maxKeyLen - len(key)))
|
||||
|
||||
// Output the command and the synopsis
|
||||
e.ui.Say(" %v %v\n", key, command.Synopsis())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,3 +43,8 @@ func TestEnvironment_DefaultUi(t *testing.T) {
|
|||
assert.Equal(rwUi.Writer, os.Stdout, "default UI should go to stdout")
|
||||
assert.Equal(rwUi.Reader, os.Stdin, "default UI should read from stdin")
|
||||
}
|
||||
|
||||
func TestEnvironment_PrintHelp(t *testing.T) {
|
||||
// Just call the function and verify that no panics occur
|
||||
NewEnvironment().PrintHelp()
|
||||
}
|
||||
|
|
|
@ -10,3 +10,7 @@ func (versionCommand) Run(env *Environment, args []string) int {
|
|||
env.Ui().Say("Packer v%v\n", Version)
|
||||
return 0
|
||||
}
|
||||
|
||||
func (versionCommand) Synopsis() string {
|
||||
return "print Packer version"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue