Convert info command to use base.Command

This commit is contained in:
Kyle Havlovitz 2017-02-08 16:58:04 -05:00
parent 89771b6075
commit a69f2a0faf
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
4 changed files with 33 additions and 27 deletions

View File

@ -1,9 +1,8 @@
package command
import (
"flag"
"fmt"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/command/base"
"sort"
"strings"
)
@ -11,7 +10,7 @@ import (
// InfoCommand is a Command implementation that queries a running
// Consul agent for various debugging statistics for operators
type InfoCommand struct {
Ui cli.Ui
base.Command
}
func (i *InfoCommand) Help() string {
@ -20,33 +19,34 @@ Usage: consul info [options]
Provides debugging information for operators
Options:
` + i.Command.Help()
-rpc-addr=127.0.0.1:8400 RPC address of the Consul agent.
`
return strings.TrimSpace(helpText)
}
func (i *InfoCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("info", flag.ContinueOnError)
cmdFlags.Usage = func() { i.Ui.Output(i.Help()) }
rpcAddr := RPCAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
i.Command.NewFlagSet(i)
if err := i.Command.Parse(args); err != nil {
return 1
}
client, err := RPCClient(*rpcAddr)
client, err := i.Command.HTTPClient()
if err != nil {
i.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
defer client.Close()
stats, err := client.Stats()
self, err := client.Agent().Self()
if err != nil {
i.Ui.Error(fmt.Sprintf("Error querying agent: %s", err))
return 1
}
stats, ok := self["Stats"]
if !ok {
i.Ui.Error(fmt.Sprintf("Agent response did not contain 'Stats' key: %v", self))
return 1
}
// Get the keys in sorted order
keys := make([]string, 0, len(stats))
@ -60,7 +60,11 @@ func (i *InfoCommand) Run(args []string) int {
i.Ui.Output(key + ":")
// Sort the sub-keys
subvals := stats[key]
subvals, ok := stats[key].(map[string]interface{})
if !ok {
i.Ui.Error(fmt.Sprintf("Got invalid subkey in stats: %v", subvals))
return 1
}
subkeys := make([]string, 0, len(subvals))
for k := range subvals {
subkeys = append(subkeys, k)
@ -77,5 +81,5 @@ func (i *InfoCommand) Run(args []string) int {
}
func (i *InfoCommand) Synopsis() string {
return "Provides debugging information for operators"
return "Provides debugging information for operators."
}

View File

@ -1,6 +1,7 @@
package command
import (
"github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli"
"strings"
"testing"
@ -15,8 +16,13 @@ func TestInfoCommandRun(t *testing.T) {
defer a1.Shutdown()
ui := new(cli.MockUi)
c := &InfoCommand{Ui: ui}
args := []string{"-rpc-addr=" + a1.addr}
c := &InfoCommand{
Command: base.Command{
Ui: ui,
Flags: base.FlagSetClientHTTP,
},
}
args := []string{"-http-addr=" + a1.httpAddr}
code := c.Run(args)
if code != 0 {

View File

@ -72,10 +72,6 @@ serf_wan:
Usage: `consul info`
The command-line flags are all optional. The list of available flags are:
* `-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".
#### API Options
<%= partial "docs/commands/http_api_options_client" %>

View File

@ -87,6 +87,10 @@
<a href="/docs/commands/force-leave.html">force-leave</a>
</li>
<li<%= sidebar_current("docs-commands-info") %>>
<a href="/docs/commands/info.html">info</a>
</li>
<li<%= sidebar_current("docs-commands-join") %>>
<a href="/docs/commands/join.html">join</a>
</li>
@ -143,10 +147,6 @@
<a href="/docs/commands/operator.html">operator</a>
</li>
<li<%= sidebar_current("docs-commands-info") %>>
<a href="/docs/commands/info.html">info</a>
</li>
<li<%= sidebar_current("docs-commands-reload") %>>
<a href="/docs/commands/reload.html">reload</a>
</li>