commands: move catalog list datacenters command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-11 16:44:43 +02:00 committed by Frank Schröder
parent 675e727224
commit bd73c4cecf
3 changed files with 48 additions and 47 deletions

View File

@ -1,48 +1,44 @@
package command package catlistdc
import ( import (
"flag"
"fmt" "fmt"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
var _ cli.Command = (*CatalogListDatacentersCommand)(nil) func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
type CatalogListDatacentersCommand struct { c.initFlags()
BaseCommand return c
} }
func (c *CatalogListDatacentersCommand) Help() string { type cmd struct {
c.InitFlagSet() UI cli.Ui
return c.HelpCommand(` flags *flag.FlagSet
Usage: consul catalog datacenters [options] http *flags.HTTPFlags
Retrieves the list of all known datacenters. This datacenters are sorted in
ascending order based on the estimated median round trip time from the servers
in this datacenter to the servers in the other datacenters.
To retrieve the list of datacenters:
$ consul catalog datacenters
For a full list of options and examples, please see the Consul documentation.
`)
} }
func (c *CatalogListDatacentersCommand) Run(args []string) int { func (c *cmd) initFlags() {
c.InitFlagSet() c.flags = flag.NewFlagSet("", flag.ContinueOnError)
if err := c.FlagSet.Parse(args); err != nil { c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
}
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1 return 1
} }
if l := len(c.FlagSet.Args()); l > 0 { if l := len(c.flags.Args()); l > 0 {
c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", l)) c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", l))
return 1 return 1
} }
// Create and test the HTTP client // Create and test the HTTP client
client, err := c.HTTPClient() client, err := c.http.APIClient()
if err != nil { if err != nil {
c.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
@ -60,6 +56,21 @@ func (c *CatalogListDatacentersCommand) Run(args []string) int {
return 0 return 0
} }
func (c *CatalogListDatacentersCommand) Synopsis() string { func (c *cmd) Synopsis() string {
return "Lists all known datacenters" return "Lists all known datacenters"
} }
func (c *cmd) Help() string {
s := `Usage: consul catalog datacenters [options]
Retrieves the list of all known datacenters. This datacenters are sorted in
ascending order based on the estimated median round trip time from the servers
in this datacenter to the servers in the other datacenters.
To retrieve the list of datacenters:
$ consul catalog datacenters
For a full list of options and examples, please see the Consul documentation.`
return flags.Usage(s, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
}

View File

@ -1,4 +1,4 @@
package command package catlistdc
import ( import (
"strings" "strings"
@ -8,24 +8,17 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func testCatalogListDatacentersCommand(t *testing.T) (*cli.MockUi, *CatalogListDatacentersCommand) {
ui := cli.NewMockUi()
return ui, &CatalogListDatacentersCommand{
BaseCommand: BaseCommand{
Flags: FlagSetHTTP,
UI: ui,
},
}
}
func TestCatalogListDatacentersCommand_noTabs(t *testing.T) { func TestCatalogListDatacentersCommand_noTabs(t *testing.T) {
t.Parallel() t.Parallel()
assertNoTabs(t, new(CatalogListDatacentersCommand)) if strings.ContainsRune(New(nil).Help(), '\t') {
t.Fatal("usage has tabs")
}
} }
func TestCatalogListDatacentersCommand_Validation(t *testing.T) { func TestCatalogListDatacentersCommand_Validation(t *testing.T) {
t.Parallel() t.Parallel()
ui, c := testCatalogListDatacentersCommand(t) ui := cli.NewMockUi()
c := New(ui)
cases := map[string]struct { cases := map[string]struct {
args []string args []string
@ -63,7 +56,8 @@ func TestCatalogListDatacentersCommand_Run(t *testing.T) {
a := agent.NewTestAgent(t.Name(), ``) a := agent.NewTestAgent(t.Name(), ``)
defer a.Shutdown() defer a.Shutdown()
ui, c := testCatalogListDatacentersCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{ args := []string{
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),

View File

@ -6,6 +6,7 @@ import (
"syscall" "syscall"
"github.com/hashicorp/consul/command/cat" "github.com/hashicorp/consul/command/cat"
"github.com/hashicorp/consul/command/catlistdc"
"github.com/hashicorp/consul/command/event" "github.com/hashicorp/consul/command/event"
execmd "github.com/hashicorp/consul/command/exec" execmd "github.com/hashicorp/consul/command/exec"
"github.com/hashicorp/consul/command/forceleave" "github.com/hashicorp/consul/command/forceleave"
@ -51,12 +52,7 @@ func init() {
}, },
"catalog datacenters": func() (cli.Command, error) { "catalog datacenters": func() (cli.Command, error) {
return &CatalogListDatacentersCommand{ return catlistdc.New(ui), nil
BaseCommand: BaseCommand{
Flags: FlagSetHTTP,
UI: ui,
},
}, nil
}, },
"catalog nodes": func() (cli.Command, error) { "catalog nodes": func() (cli.Command, error) {