From 684362b1d0a014fc045127ca30247a11afea5bda Mon Sep 17 00:00:00 2001 From: Preetha Appan Date: Fri, 13 Oct 2017 10:15:54 -0500 Subject: [PATCH] Move monitor command to its own package --- command/commands.go | 9 +--- command/{ => monitor}/monitor.go | 70 ++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 33 deletions(-) rename command/{ => monitor}/monitor.go (60%) diff --git a/command/commands.go b/command/commands.go index 8f39012e1..e18ab9787 100644 --- a/command/commands.go +++ b/command/commands.go @@ -26,6 +26,7 @@ import ( "github.com/hashicorp/consul/command/lock" "github.com/hashicorp/consul/command/maint" "github.com/hashicorp/consul/command/members" + "github.com/hashicorp/consul/command/monitor" "github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/version" "github.com/mitchellh/cli" @@ -137,13 +138,7 @@ func init() { }, "monitor": func() (cli.Command, error) { - return &MonitorCommand{ - ShutdownCh: makeShutdownCh(), - BaseCommand: BaseCommand{ - Flags: FlagSetClientHTTP, - UI: ui, - }, - }, nil + return monitor.New(ui, makeShutdownCh()), nil }, "operator": func() (cli.Command, error) { diff --git a/command/monitor.go b/command/monitor/monitor.go similarity index 60% rename from command/monitor.go rename to command/monitor/monitor.go index c8d4a9f2f..6b488e5be 100644 --- a/command/monitor.go +++ b/command/monitor/monitor.go @@ -1,16 +1,23 @@ -package command +package monitor import ( + "flag" "fmt" "sync" + + "github.com/hashicorp/consul/command/flags" + "github.com/mitchellh/cli" ) -// MonitorCommand is a Command implementation that queries a running +// cmd is a Command implementation that queries a running // Consul agent what members are part of the cluster currently. -type MonitorCommand struct { - BaseCommand +type cmd struct { + UI cli.Ui + usage string + flags *flag.FlagSet + http *flags.HTTPFlags - ShutdownCh <-chan struct{} + shutdownCh <-chan struct{} lock sync.Mutex quitting bool @@ -19,33 +26,33 @@ type MonitorCommand struct { logLevel string } -func (c *MonitorCommand) initFlags() { - c.InitFlagSet() - c.FlagSet.StringVar(&c.logLevel, "log-level", "INFO", +func New(ui cli.Ui, shutdownCh <-chan struct{}) *cmd { + c := &cmd{UI: ui, shutdownCh: shutdownCh} + c.init() + return c +} + +func (c *cmd) init() { + c.flags = flag.NewFlagSet("", flag.ContinueOnError) + c.http = &flags.HTTPFlags{} + flags.Merge(c.flags, c.http.ClientFlags()) + + c.flags.StringVar(&c.logLevel, "log-level", "INFO", "Log level of the agent.") + + c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), nil) } -func (c *MonitorCommand) Help() string { - c.initFlags() - return c.HelpCommand(` -Usage: consul monitor [options] - - Shows recent log messages of a Consul agent, and attaches to the agent, - outputting log messages as they occur in real time. The monitor lets you - listen for log levels that may be filtered out of the Consul agent. For - example your agent may only be logging at INFO level, but with the monitor - you can see the DEBUG level logs. - -`) +func (c *cmd) Help() string { + return c.usage } -func (c *MonitorCommand) Run(args []string) int { - c.initFlags() - if err := c.FlagSet.Parse(args); err != nil { +func (c *cmd) Run(args []string) int { + if err := c.flags.Parse(args); err != nil { return 1 } - client, err := c.HTTPClient() + client, err := c.http.APIClient() if err != nil { c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) return 1 @@ -83,7 +90,7 @@ func (c *MonitorCommand) Run(args []string) int { select { case <-eventDoneCh: return 1 - case <-c.ShutdownCh: + case <-c.shutdownCh: c.lock.Lock() c.quitting = true c.lock.Unlock() @@ -92,6 +99,17 @@ func (c *MonitorCommand) Run(args []string) int { return 0 } -func (c *MonitorCommand) Synopsis() string { +func (c *cmd) Synopsis() string { return "Stream logs from a Consul agent" } + +const usage = ` +Usage: consul monitor [options] + + Shows recent log messages of a Consul agent, and attaches to the agent, + outputting log messages as they occur in real time. The monitor lets you + listen for log levels that may be filtered out of the Consul agent. For + example your agent may only be logging at INFO level, but with the monitor + you can see the DEBUG level logs. + +`