commands: move info command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-11 14:51:24 +02:00 committed by Frank Schröder
parent c1c883957f
commit 74cf5c1c2c
3 changed files with 42 additions and 37 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/hashicorp/consul/command/event"
execmd "github.com/hashicorp/consul/command/exec"
"github.com/hashicorp/consul/command/info"
"github.com/hashicorp/consul/command/join"
"github.com/hashicorp/consul/command/validate"
"github.com/hashicorp/consul/version"
@ -91,12 +92,7 @@ func init() {
},
"info": func() (cli.Command, error) {
return &InfoCommand{
BaseCommand: BaseCommand{
Flags: FlagSetClientHTTP,
UI: ui,
},
}, nil
return info.New(ui), nil
},
"join": func() (cli.Command, error) {

View File

@ -1,23 +1,38 @@
package command
package info
import (
"flag"
"fmt"
"sort"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
)
// InfoCommand is a Command implementation that queries a running
// Consul agent for various debugging statistics for operators
type InfoCommand struct {
BaseCommand
func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
c.initFlags()
return c
}
func (c *InfoCommand) Run(args []string) int {
c.InitFlagSet()
if err := c.FlagSet.Parse(args); err != nil {
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
}
func (c *cmd) initFlags() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
}
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1
}
client, err := c.HTTPClient()
client, err := c.http.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
@ -66,16 +81,14 @@ func (c *InfoCommand) Run(args []string) int {
return 0
}
func (c *InfoCommand) Help() string {
c.InitFlagSet()
return c.HelpCommand(`
Usage: consul info [options]
Provides debugging information for operators
`)
}
func (c *InfoCommand) Synopsis() string {
func (c *cmd) Synopsis() string {
return "Provides debugging information for operators."
}
func (c *cmd) Help() string {
s := `Usage: consul info [options]
Provides debugging information for operators`
return flags.Usage(s, c.flags, c.http.ClientFlags(), nil)
}

View File

@ -1,4 +1,4 @@
package command
package info
import (
"strings"
@ -8,9 +8,10 @@ import (
"github.com/mitchellh/cli"
)
func TestInfoCommand_implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &InfoCommand{}
func TestInfoCommand_noTabs(t *testing.T) {
if strings.ContainsRune(New(nil).Help(), '\t') {
t.Fatal("usage has tabs")
}
}
func TestInfoCommandRun(t *testing.T) {
@ -19,15 +20,10 @@ func TestInfoCommandRun(t *testing.T) {
defer a1.Shutdown()
ui := cli.NewMockUi()
c := &InfoCommand{
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetClientHTTP,
},
}
cmd := New(ui)
args := []string{"-http-addr=" + a1.HTTPAddr()}
code := c.Run(args)
code := cmd.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}