Convert monitor command to use base.Command

This commit is contained in:
Kyle Havlovitz 2017-02-09 17:31:52 -05:00
parent 14415741a3
commit be17779c42
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
3 changed files with 22 additions and 26 deletions

View File

@ -1,19 +1,19 @@
package command package command
import ( import (
"flag"
"fmt" "fmt"
"github.com/hashicorp/logutils"
"github.com/mitchellh/cli"
"strings" "strings"
"sync" "sync"
"github.com/hashicorp/consul/command/base"
) )
// MonitorCommand is a Command implementation that queries a running // MonitorCommand 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 MonitorCommand struct {
base.Command
ShutdownCh <-chan struct{} ShutdownCh <-chan struct{}
Ui cli.Ui
lock sync.Mutex lock sync.Mutex
quitting bool quitting bool
@ -29,40 +29,34 @@ Usage: consul monitor [options]
example your agent may only be logging at INFO level, but with the monitor example your agent may only be logging at INFO level, but with the monitor
you can see the DEBUG level logs. 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) return strings.TrimSpace(helpText)
} }
func (c *MonitorCommand) Run(args []string) int { func (c *MonitorCommand) Run(args []string) int {
var logLevel string var logLevel string
cmdFlags := flag.NewFlagSet("monitor", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) } f := c.Command.NewFlagSet(c)
cmdFlags.StringVar(&logLevel, "log-level", "INFO", "log level") f.StringVar(&logLevel, "log-level", "INFO", "Log level of the agent.")
rpcAddr := RPCAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil { if err := c.Command.Parse(args); err != nil {
return 1 return 1
} }
client, err := RPCClient(*rpcAddr) client, err := c.Command.HTTPClient()
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
} }
defer client.Close()
logCh := make(chan string, 1024) eventDoneCh := make(chan struct{})
monHandle, err := client.Monitor(logutils.LogLevel(logLevel), logCh) logCh, err := client.Agent().Monitor(logLevel, eventDoneCh, nil)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error starting monitor: %s", err)) c.Ui.Error(fmt.Sprintf("Error starting monitor: %s", err))
return 1 return 1
} }
defer client.Stop(monHandle)
eventDoneCh := make(chan struct{})
go func() { go func() {
defer close(eventDoneCh) defer close(eventDoneCh)
OUTER: OUTER:

View File

@ -155,7 +155,10 @@ func init() {
"monitor": func() (cli.Command, error) { "monitor": func() (cli.Command, error) {
return &command.MonitorCommand{ return &command.MonitorCommand{
ShutdownCh: makeShutdownCh(), ShutdownCh: makeShutdownCh(),
Command: base.Command{
Flags: base.FlagSetClientHTTP,
Ui: ui, Ui: ui,
},
}, nil }, nil
}, },

View File

@ -22,14 +22,13 @@ logs and watch the debug logs if necessary.
Usage: `consul monitor [options]` 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 * `-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 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", configured to run at. Available log levels are "trace", "debug", "info",
"warn", and "err". "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".