diff --git a/command/cli/cli.go b/command/cli/cli.go new file mode 100644 index 000000000..6a7edd720 --- /dev/null +++ b/command/cli/cli.go @@ -0,0 +1,32 @@ +package cli + +import ( + "io" + + mcli "github.com/mitchellh/cli" +) + +// Ui implements the mitchellh/cli.Ui interface, while exposing the underlying +// io.Writer used for stdout and stderr. +// TODO: rename to UI to match golang style guide +type Ui interface { + mcli.Ui + Stdout() io.Writer + Stderr() io.Writer +} + +// BasicUI augments mitchellh/cli.BasicUi by exposing the underlying io.Writer. +type BasicUI struct { + mcli.BasicUi +} + +func (b *BasicUI) Stdout() io.Writer { + return b.BasicUi.Writer +} + +func (b *BasicUI) Stderr() io.Writer { + return b.BasicUi.ErrorWriter +} + +// Command is an alias to reduce the diff. It can be removed at any time. +type Command mcli.Command diff --git a/command/commands_oss.go b/command/commands_oss.go index 07866a0e7..cfa8a7e62 100644 --- a/command/commands_oss.go +++ b/command/commands_oss.go @@ -41,6 +41,7 @@ import ( catlistdc "github.com/hashicorp/consul/command/catalog/list/dc" catlistnodes "github.com/hashicorp/consul/command/catalog/list/nodes" catlistsvc "github.com/hashicorp/consul/command/catalog/list/services" + "github.com/hashicorp/consul/command/cli" "github.com/hashicorp/consul/command/config" configdelete "github.com/hashicorp/consul/command/config/delete" configlist "github.com/hashicorp/consul/command/config/list" @@ -108,8 +109,6 @@ import ( "github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/command/version" "github.com/hashicorp/consul/command/watch" - - "github.com/mitchellh/cli" ) func init() { diff --git a/command/registry.go b/command/registry.go index 2b092ae72..a96818c2f 100644 --- a/command/registry.go +++ b/command/registry.go @@ -6,7 +6,9 @@ import ( "os/signal" "syscall" - "github.com/mitchellh/cli" + mcli "github.com/mitchellh/cli" + + "github.com/hashicorp/consul/command/cli" ) // Factory is a function that returns a new instance of a CLI-sub command. @@ -24,14 +26,14 @@ func Register(name string, fn Factory) { registry[name] = fn } -// Map returns a realized mapping of available CLI commands in a format that +// CommandsFromRegistry returns a realized mapping of available CLI commands in a format that // the CLI class can consume. This should be called after all registration is // complete. -func Map(ui cli.Ui) map[string]cli.CommandFactory { - m := make(map[string]cli.CommandFactory) +func CommandsFromRegistry(ui cli.Ui) map[string]mcli.CommandFactory { + m := make(map[string]mcli.CommandFactory) for name, fn := range registry { thisFn := fn - m[name] = func() (cli.Command, error) { + m[name] = func() (mcli.Command, error) { return thisFn(ui) } } diff --git a/main.go b/main.go index 6ffcd431f..9505f2906 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,13 @@ import ( "log" "os" + mcli "github.com/mitchellh/cli" + "github.com/hashicorp/consul/command" + "github.com/hashicorp/consul/command/cli" "github.com/hashicorp/consul/command/version" "github.com/hashicorp/consul/lib" _ "github.com/hashicorp/consul/service_os" - "github.com/mitchellh/cli" ) func init() { @@ -24,19 +26,21 @@ func main() { func realMain() int { log.SetOutput(ioutil.Discard) - ui := &cli.BasicUi{Writer: os.Stdout, ErrorWriter: os.Stderr} - cmds := command.Map(ui) + ui := &cli.BasicUI{ + BasicUi: mcli.BasicUi{Writer: os.Stdout, ErrorWriter: os.Stderr}, + } + cmds := command.CommandsFromRegistry(ui) var names []string for c := range cmds { names = append(names, c) } - cli := &cli.CLI{ + cli := &mcli.CLI{ Args: os.Args[1:], Commands: cmds, Autocomplete: true, Name: "consul", - HelpFunc: cli.FilteredHelpFunc(names, cli.BasicHelpFunc("consul")), + HelpFunc: mcli.FilteredHelpFunc(names, mcli.BasicHelpFunc("consul")), HelpWriter: os.Stdout, ErrorWriter: os.Stderr, }