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 (
"flag"
"fmt"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
)
var _ cli.Command = (*CatalogListDatacentersCommand)(nil)
type CatalogListDatacentersCommand struct {
BaseCommand
func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
c.initFlags()
return c
}
func (c *CatalogListDatacentersCommand) Help() string {
c.InitFlagSet()
return c.HelpCommand(`
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.
`)
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
}
func (c *CatalogListDatacentersCommand) Run(args []string) int {
c.InitFlagSet()
if err := c.FlagSet.Parse(args); err != nil {
func (c *cmd) initFlags() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
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
}
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))
return 1
}
// Create and test the HTTP client
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
@ -60,6 +56,21 @@ func (c *CatalogListDatacentersCommand) Run(args []string) int {
return 0
}
func (c *CatalogListDatacentersCommand) Synopsis() string {
func (c *cmd) Synopsis() string {
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 (
"strings"
@ -8,24 +8,17 @@ import (
"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) {
t.Parallel()
assertNoTabs(t, new(CatalogListDatacentersCommand))
if strings.ContainsRune(New(nil).Help(), '\t') {
t.Fatal("usage has tabs")
}
}
func TestCatalogListDatacentersCommand_Validation(t *testing.T) {
t.Parallel()
ui, c := testCatalogListDatacentersCommand(t)
ui := cli.NewMockUi()
c := New(ui)
cases := map[string]struct {
args []string
@ -63,7 +56,8 @@ func TestCatalogListDatacentersCommand_Run(t *testing.T) {
a := agent.NewTestAgent(t.Name(), ``)
defer a.Shutdown()
ui, c := testCatalogListDatacentersCommand(t)
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),

View File

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