Convert info command to use base.Command

This commit is contained in:
Kyle Havlovitz 2017-02-08 16:58:04 -05:00
parent aa1c464961
commit a3d02a4cbc
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 package command
import ( import (
"flag"
"fmt" "fmt"
"github.com/mitchellh/cli" "github.com/hashicorp/consul/command/base"
"sort" "sort"
"strings" "strings"
) )
@ -11,7 +10,7 @@ import (
// InfoCommand is a Command implementation that queries a running // InfoCommand is a Command implementation that queries a running
// Consul agent for various debugging statistics for operators // Consul agent for various debugging statistics for operators
type InfoCommand struct { type InfoCommand struct {
Ui cli.Ui base.Command
} }
func (i *InfoCommand) Help() string { func (i *InfoCommand) Help() string {
@ -20,33 +19,34 @@ Usage: consul info [options]
Provides debugging information for operators 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) return strings.TrimSpace(helpText)
} }
func (i *InfoCommand) Run(args []string) int { func (i *InfoCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("info", flag.ContinueOnError) i.Command.NewFlagSet(i)
cmdFlags.Usage = func() { i.Ui.Output(i.Help()) }
rpcAddr := RPCAddrFlag(cmdFlags) if err := i.Command.Parse(args); err != nil {
if err := cmdFlags.Parse(args); err != nil {
return 1 return 1
} }
client, err := RPCClient(*rpcAddr) client, err := i.Command.HTTPClient()
if err != nil { if err != nil {
i.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) i.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1 return 1
} }
defer client.Close()
stats, err := client.Stats() self, err := client.Agent().Self()
if err != nil { if err != nil {
i.Ui.Error(fmt.Sprintf("Error querying agent: %s", err)) i.Ui.Error(fmt.Sprintf("Error querying agent: %s", err))
return 1 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 // Get the keys in sorted order
keys := make([]string, 0, len(stats)) keys := make([]string, 0, len(stats))
@ -60,7 +60,11 @@ func (i *InfoCommand) Run(args []string) int {
i.Ui.Output(key + ":") i.Ui.Output(key + ":")
// Sort the sub-keys // 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)) subkeys := make([]string, 0, len(subvals))
for k := range subvals { for k := range subvals {
subkeys = append(subkeys, k) subkeys = append(subkeys, k)
@ -77,5 +81,5 @@ func (i *InfoCommand) Run(args []string) int {
} }
func (i *InfoCommand) Synopsis() string { 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 package command
import ( import (
"github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"strings" "strings"
"testing" "testing"
@ -15,8 +16,13 @@ func TestInfoCommandRun(t *testing.T) {
defer a1.Shutdown() defer a1.Shutdown()
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &InfoCommand{Ui: ui} c := &InfoCommand{
args := []string{"-rpc-addr=" + a1.addr} Command: base.Command{
Ui: ui,
Flags: base.FlagSetClientHTTP,
},
}
args := []string{"-http-addr=" + a1.httpAddr}
code := c.Run(args) code := c.Run(args)
if code != 0 { if code != 0 {

View File

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

View File

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