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 902f4caff2
commit d2ee591fbf
1 changed files with 15 additions and 14 deletions

View File

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