From ea66375063d6f9c0a211ffccf40b9d4711264144 Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Thu, 9 Feb 2017 20:36:01 -0500 Subject: [PATCH] Convert watch command to use base.Command --- command/base/command.go | 12 +++ command/watch.go | 91 ++++++++----------- command/watch_test.go | 11 ++- commands.go | 5 +- ....html.markdown => watch.html.markdown.erb} | 14 +-- 5 files changed, 65 insertions(+), 68 deletions(-) rename website/source/docs/commands/{watch.html.markdown => watch.html.markdown.erb} (78%) diff --git a/command/base/command.go b/command/base/command.go index 804564b02e..b443c4b26e 100644 --- a/command/base/command.go +++ b/command/base/command.go @@ -58,6 +58,18 @@ func (c *Command) HTTPClient() (*api.Client, error) { return api.NewClient(config) } +func (c *Command) HTTPAddr() string { + return c.httpAddr.String() +} + +func (c *Command) HTTPToken() string { + return c.token.String() +} + +func (c *Command) HTTPDatacenter() string { + return c.datacenter.String() +} + func (c *Command) HTTPStale() bool { var stale bool c.stale.Merge(&stale) diff --git a/command/watch.go b/command/watch.go index a6ec677a19..bcdfac6c2c 100644 --- a/command/watch.go +++ b/command/watch.go @@ -3,22 +3,21 @@ package command import ( "bytes" "encoding/json" - "flag" "fmt" "os" "strconv" "strings" "github.com/hashicorp/consul/command/agent" + "github.com/hashicorp/consul/command/base" "github.com/hashicorp/consul/watch" - "github.com/mitchellh/cli" ) // WatchCommand is a Command implementation that is used to setup // a "watch" which uses a sub-process type WatchCommand struct { + base.Command ShutdownCh <-chan struct{} - Ui cli.Ui } func (c *WatchCommand) Help() string { @@ -32,49 +31,36 @@ Usage: consul watch [options] [child...] Providing the watch type is required, and other parameters may be required or supported depending on the watch type. -Options: +` + c.Command.Help() - -http-addr=127.0.0.1:8500 HTTP address of the Consul agent. - -datacenter="" Datacenter to query. Defaults to that of agent. - -token="" ACL token to use. Defaults to that of agent. - -stale=[true|false] Specifies if watch data is permitted to be stale. - Defaults to false. - -Watch Specification: - - -key=val Specifies the key to watch. Only for 'key' type. - -name=val Specifies an event name to watch. Only for 'event' type. - -passingonly=[true|false] Specifies if only hosts passing all checks are displayed. - Optional for 'service' type. Defaults false. - -prefix=val Specifies the key prefix to watch. Only for 'keyprefix' type. - -service=val Specifies the service to watch. Required for 'service' type, - optional for 'checks' type. - -state=val Specifies the states to watch. Optional for 'checks' type. - -tag=val Specifies the service tag to filter on. Optional for 'service' - type. - -type=val Specifies the watch type. One of key, keyprefix - services, nodes, service, checks, or event. -` return strings.TrimSpace(helpText) } func (c *WatchCommand) Run(args []string) int { - var watchType, datacenter, token, key, prefix, service, tag, passingOnly, stale, state, name string - cmdFlags := flag.NewFlagSet("watch", flag.ContinueOnError) - cmdFlags.Usage = func() { c.Ui.Output(c.Help()) } - cmdFlags.StringVar(&watchType, "type", "", "") - cmdFlags.StringVar(&datacenter, "datacenter", "", "") - cmdFlags.StringVar(&token, "token", "", "") - cmdFlags.StringVar(&key, "key", "", "") - cmdFlags.StringVar(&prefix, "prefix", "", "") - cmdFlags.StringVar(&service, "service", "", "") - cmdFlags.StringVar(&tag, "tag", "", "") - cmdFlags.StringVar(&passingOnly, "passingonly", "", "") - cmdFlags.StringVar(&stale, "stale", "", "") - cmdFlags.StringVar(&state, "state", "", "") - cmdFlags.StringVar(&name, "name", "", "") - httpAddr := HTTPAddrFlag(cmdFlags) - if err := cmdFlags.Parse(args); err != nil { + var watchType, key, prefix, service, tag, passingOnly, state, name string + + f := c.Command.NewFlagSet(c) + f.StringVar(&watchType, "type", "", + "Specifies the watch type. One of key, keyprefix services, nodes, "+ + "service, checks, or event.") + f.StringVar(&key, "key", "", + "Specifies the key to watch. Only for 'key' type.") + f.StringVar(&prefix, "prefix", "", + "Specifies the key prefix to watch. Only for 'keyprefix' type.") + f.StringVar(&service, "service", "", + "Specifies the service to watch. Required for 'service' type, "+ + "optional for 'checks' type.") + f.StringVar(&tag, "tag", "", + "Specifies the service tag to filter on. Optional for 'service' type.") + f.StringVar(&passingOnly, "passingonly", "", + "Specifies if only hosts passing all checks are displayed. "+ + "Optional for 'service' type, must be one of `[true|false]`. Defaults false.") + f.StringVar(&state, "state", "", + "Specifies the states to watch. Optional for 'checks' type.") + f.StringVar(&name, "name", "", + "Specifies an event name to watch. Only for 'event' type.") + + if err := c.Command.Parse(args); err != nil { return 1 } @@ -87,18 +73,18 @@ func (c *WatchCommand) Run(args []string) int { } // Grab the script to execute if any - script := strings.Join(cmdFlags.Args(), " ") + script := strings.Join(f.Args(), " ") // Compile the watch parameters params := make(map[string]interface{}) if watchType != "" { params["type"] = watchType } - if datacenter != "" { - params["datacenter"] = datacenter + if c.Command.HTTPDatacenter() != "" { + params["datacenter"] = c.Command.HTTPDatacenter() } - if token != "" { - params["token"] = token + if c.Command.HTTPToken() != "" { + params["token"] = c.Command.HTTPToken() } if key != "" { params["key"] = key @@ -112,13 +98,8 @@ func (c *WatchCommand) Run(args []string) int { if tag != "" { params["tag"] = tag } - if stale != "" { - b, err := strconv.ParseBool(stale) - if err != nil { - c.Ui.Error(fmt.Sprintf("Failed to parse stale flag: %s", err)) - return 1 - } - params["stale"] = b + if c.Command.HTTPStale() { + params["stale"] = c.Command.HTTPStale() } if state != "" { params["state"] = state @@ -143,7 +124,7 @@ func (c *WatchCommand) Run(args []string) int { } // Create and test the HTTP client - client, err := HTTPClient(*httpAddr) + client, err := c.Command.HTTPClient() if err != nil { c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) return 1 @@ -213,7 +194,7 @@ func (c *WatchCommand) Run(args []string) int { }() // Run the watch - if err := wp.Run(*httpAddr); err != nil { + if err := wp.Run(c.Command.HTTPAddr()); err != nil { c.Ui.Error(fmt.Sprintf("Error querying Consul agent: %s", err)) return 1 } diff --git a/command/watch_test.go b/command/watch_test.go index eaa9376d5a..8325981b8e 100644 --- a/command/watch_test.go +++ b/command/watch_test.go @@ -1,9 +1,11 @@ package command import ( - "github.com/mitchellh/cli" "strings" "testing" + + "github.com/hashicorp/consul/command/base" + "github.com/mitchellh/cli" ) func TestWatchCommand_implements(t *testing.T) { @@ -15,7 +17,12 @@ func TestWatchCommandRun(t *testing.T) { defer a1.Shutdown() ui := new(cli.MockUi) - c := &WatchCommand{Ui: ui} + c := &WatchCommand{ + Command: base.Command{ + Ui: ui, + Flags: base.FlagSetHTTP, + }, + } args := []string{"-http-addr=" + a1.httpAddr, "-type=nodes"} code := c.Run(args) diff --git a/commands.go b/commands.go index e339c5642e..d9f7b7d2d4 100644 --- a/commands.go +++ b/commands.go @@ -238,7 +238,10 @@ func init() { "watch": func() (cli.Command, error) { return &command.WatchCommand{ ShutdownCh: makeShutdownCh(), - Ui: ui, + Command: base.Command{ + Flags: base.FlagSetHTTP, + Ui: ui, + }, }, nil }, } diff --git a/website/source/docs/commands/watch.html.markdown b/website/source/docs/commands/watch.html.markdown.erb similarity index 78% rename from website/source/docs/commands/watch.html.markdown rename to website/source/docs/commands/watch.html.markdown.erb index 14cf8d2a0e..b940aa8b40 100644 --- a/website/source/docs/commands/watch.html.markdown +++ b/website/source/docs/commands/watch.html.markdown.erb @@ -27,18 +27,12 @@ data view. Depending on the type, various options may be required or optionally provided. There is more documentation on watch [specifications here](/docs/agent/watches.html). -The list of available flags are: +#### API Options -* `-http-addr` - Address to the HTTP server of the agent you want to contact - to send this command. If this isn't specified, the command will contact - "127.0.0.1:8500" which is the default HTTP address of a Consul agent. +<%= partial "docs/commands/http_api_options_client" %> +<%= partial "docs/commands/http_api_options_server" %> -* `-datacenter` - Datacenter to query. Defaults to that of the agent. - -* `-token` - ACL token to use. Defaults to that of the agent. - -* `-stale=[true|false]` - Specifies if watch data is permitted to be stale. Defaults - to false. +#### Command Options * `-key` - Key to watch. Only for `key` type.