From c78f62b83d4e426060de2cf26be84874ec46a2ad Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Thu, 9 Feb 2017 17:31:52 -0500 Subject: [PATCH] Convert monitor command to use base.Command --- command/monitor.go | 32 ++++++++----------- commands.go | 5 ++- ...tml.markdown => monitor.html.markdown.erb} | 11 +++---- 3 files changed, 22 insertions(+), 26 deletions(-) rename website/source/docs/commands/{monitor.html.markdown => monitor.html.markdown.erb} (76%) diff --git a/command/monitor.go b/command/monitor.go index f24646662..4460b66d4 100644 --- a/command/monitor.go +++ b/command/monitor.go @@ -1,19 +1,19 @@ package command import ( - "flag" "fmt" - "github.com/hashicorp/logutils" - "github.com/mitchellh/cli" "strings" "sync" + + "github.com/hashicorp/consul/command/base" ) // MonitorCommand is a Command implementation that queries a running // Consul agent what members are part of the cluster currently. type MonitorCommand struct { + base.Command + ShutdownCh <-chan struct{} - Ui cli.Ui lock sync.Mutex quitting bool @@ -29,40 +29,34 @@ Usage: consul monitor [options] example your agent may only be logging at INFO level, but with the monitor you can see the DEBUG level logs. -Options: +` + c.Command.Help() - -log-level=info Log level of the agent. - -rpc-addr=127.0.0.1:8400 RPC address of the Consul agent. -` return strings.TrimSpace(helpText) } func (c *MonitorCommand) Run(args []string) int { var logLevel string - cmdFlags := flag.NewFlagSet("monitor", flag.ContinueOnError) - cmdFlags.Usage = func() { c.Ui.Output(c.Help()) } - cmdFlags.StringVar(&logLevel, "log-level", "INFO", "log level") - rpcAddr := RPCAddrFlag(cmdFlags) - if err := cmdFlags.Parse(args); err != nil { + + f := c.Command.NewFlagSet(c) + f.StringVar(&logLevel, "log-level", "INFO", "Log level of the agent.") + + if err := c.Command.Parse(args); err != nil { return 1 } - client, err := RPCClient(*rpcAddr) + client, err := c.Command.HTTPClient() if err != nil { c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) return 1 } - defer client.Close() - logCh := make(chan string, 1024) - monHandle, err := client.Monitor(logutils.LogLevel(logLevel), logCh) + eventDoneCh := make(chan struct{}) + logCh, err := client.Agent().Monitor(logLevel, eventDoneCh, nil) if err != nil { c.Ui.Error(fmt.Sprintf("Error starting monitor: %s", err)) return 1 } - defer client.Stop(monHandle) - eventDoneCh := make(chan struct{}) go func() { defer close(eventDoneCh) OUTER: diff --git a/commands.go b/commands.go index efd9fc1df..30aebced6 100644 --- a/commands.go +++ b/commands.go @@ -155,7 +155,10 @@ func init() { "monitor": func() (cli.Command, error) { return &command.MonitorCommand{ ShutdownCh: makeShutdownCh(), - Ui: ui, + Command: base.Command{ + Flags: base.FlagSetClientHTTP, + Ui: ui, + }, }, nil }, diff --git a/website/source/docs/commands/monitor.html.markdown b/website/source/docs/commands/monitor.html.markdown.erb similarity index 76% rename from website/source/docs/commands/monitor.html.markdown rename to website/source/docs/commands/monitor.html.markdown.erb index 0d609dc3b..fed4084a6 100644 --- a/website/source/docs/commands/monitor.html.markdown +++ b/website/source/docs/commands/monitor.html.markdown.erb @@ -22,14 +22,13 @@ logs and watch the debug logs if necessary. Usage: `consul monitor [options]` -The command-line flags are all optional. The list of available flags are: +#### API Options + +<%= partial "docs/commands/http_api_options_client" %> + +#### Command Options * `-log-level` - The log level of the messages to show. By default this is "info". This log level can be more verbose than what the agent is configured to run at. Available log levels are "trace", "debug", "info", "warn", and "err". - -* `-rpc-addr` - Address to the RPC server of the agent you want to contact - to send this command. If this isn't specified, the command checks the - CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC - address will be set to "127.0.0.1:8400".