Move monitor command to its own package

pull/3584/head
Preetha Appan 2017-10-13 10:15:54 -05:00 committed by Frank Schröder
parent f389fe7757
commit 1eec0e757c
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/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) {

View File

@ -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.
`