Move monitor command to its own package

This commit is contained in:
Preetha Appan 2017-10-13 10:15:54 -05:00 committed by Frank Schröder
parent 28490d6dcb
commit 684362b1d0
2 changed files with 46 additions and 33 deletions

View File

@ -26,6 +26,7 @@ import (
"github.com/hashicorp/consul/command/lock" "github.com/hashicorp/consul/command/lock"
"github.com/hashicorp/consul/command/maint" "github.com/hashicorp/consul/command/maint"
"github.com/hashicorp/consul/command/members" "github.com/hashicorp/consul/command/members"
"github.com/hashicorp/consul/command/monitor"
"github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/command/validate"
"github.com/hashicorp/consul/version" "github.com/hashicorp/consul/version"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -137,13 +138,7 @@ func init() {
}, },
"monitor": func() (cli.Command, error) { "monitor": func() (cli.Command, error) {
return &MonitorCommand{ return monitor.New(ui, makeShutdownCh()), nil
ShutdownCh: makeShutdownCh(),
BaseCommand: BaseCommand{
Flags: FlagSetClientHTTP,
UI: ui,
},
}, nil
}, },
"operator": func() (cli.Command, error) { "operator": func() (cli.Command, error) {

View File

@ -1,16 +1,23 @@
package command package monitor
import ( import (
"flag"
"fmt" "fmt"
"sync" "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. // Consul agent what members are part of the cluster currently.
type MonitorCommand struct { type cmd struct {
BaseCommand UI cli.Ui
usage string
flags *flag.FlagSet
http *flags.HTTPFlags
ShutdownCh <-chan struct{} shutdownCh <-chan struct{}
lock sync.Mutex lock sync.Mutex
quitting bool quitting bool
@ -19,33 +26,33 @@ type MonitorCommand struct {
logLevel string logLevel string
} }
func (c *MonitorCommand) initFlags() { func New(ui cli.Ui, shutdownCh <-chan struct{}) *cmd {
c.InitFlagSet() c := &cmd{UI: ui, shutdownCh: shutdownCh}
c.FlagSet.StringVar(&c.logLevel, "log-level", "INFO", 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.") "Log level of the agent.")
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), nil)
} }
func (c *MonitorCommand) Help() string { func (c *cmd) Help() string {
c.initFlags() return c.usage
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 *MonitorCommand) Run(args []string) int { func (c *cmd) Run(args []string) int {
c.initFlags() if err := c.flags.Parse(args); err != nil {
if err := c.FlagSet.Parse(args); err != nil {
return 1 return 1
} }
client, err := c.HTTPClient() client, err := c.http.APIClient()
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1 return 1
@ -83,7 +90,7 @@ func (c *MonitorCommand) Run(args []string) int {
select { select {
case <-eventDoneCh: case <-eventDoneCh:
return 1 return 1
case <-c.ShutdownCh: case <-c.shutdownCh:
c.lock.Lock() c.lock.Lock()
c.quitting = true c.quitting = true
c.lock.Unlock() c.lock.Unlock()
@ -92,6 +99,17 @@ func (c *MonitorCommand) Run(args []string) int {
return 0 return 0
} }
func (c *MonitorCommand) Synopsis() string { func (c *cmd) Synopsis() string {
return "Stream logs from a Consul agent" 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.
`