use 'c' as command receiver for simple commands

This commit is contained in:
Frank Schroeder 2017-06-09 10:37:13 +02:00 committed by Frank Schröder
parent a56d0cc95f
commit 0d9471d659
1 changed files with 15 additions and 14 deletions

View File

@ -2,9 +2,10 @@ package command
import (
"fmt"
"github.com/hashicorp/consul/command/base"
"sort"
"strings"
"github.com/hashicorp/consul/command/base"
)
// InfoCommand is a Command implementation that queries a running
@ -13,38 +14,38 @@ type InfoCommand struct {
base.Command
}
func (i *InfoCommand) Help() string {
func (c *InfoCommand) Help() string {
helpText := `
Usage: consul info [options]
Provides debugging information for operators
` + i.Command.Help()
` + c.Command.Help()
return strings.TrimSpace(helpText)
}
func (i *InfoCommand) Run(args []string) int {
i.Command.NewFlagSet(i)
func (c *InfoCommand) Run(args []string) int {
c.Command.NewFlagSet(c)
if err := i.Command.Parse(args); err != nil {
if err := c.Command.Parse(args); err != nil {
return 1
}
client, err := i.Command.HTTPClient()
client, err := c.Command.HTTPClient()
if err != nil {
i.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
}
self, err := client.Agent().Self()
if err != nil {
i.UI.Error(fmt.Sprintf("Error querying agent: %s", err))
c.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))
c.UI.Error(fmt.Sprintf("Agent response did not contain 'Stats' key: %v", self))
return 1
}
@ -57,12 +58,12 @@ func (i *InfoCommand) Run(args []string) int {
// Iterate over each top-level key
for _, key := range keys {
i.UI.Output(key + ":")
c.UI.Output(key + ":")
// Sort the sub-keys
subvals, ok := stats[key].(map[string]interface{})
if !ok {
i.UI.Error(fmt.Sprintf("Got invalid subkey in stats: %v", subvals))
c.UI.Error(fmt.Sprintf("Got invalid subkey in stats: %v", subvals))
return 1
}
subkeys := make([]string, 0, len(subvals))
@ -74,12 +75,12 @@ func (i *InfoCommand) Run(args []string) int {
// Iterate over the subkeys
for _, subkey := range subkeys {
val := subvals[subkey]
i.UI.Output(fmt.Sprintf("\t%s = %s", subkey, val))
c.UI.Output(fmt.Sprintf("\t%s = %s", subkey, val))
}
}
return 0
}
func (i *InfoCommand) Synopsis() string {
func (c *InfoCommand) Synopsis() string {
return "Provides debugging information for operators."
}