consul/command/catalog_list_datacenters.go
Frank Schroeder a49711b8bf config: refactor commands to print help for flags (#3536)
This patch refactors the commands that use the mitchellh/cli library to
populate the command line flag set in both the Run() and the Help()
method. Earlier versions of the mitchellh/cli library relied on the
Run() method to populuate the flagset for generating the usage screen.
This has changed in later versions and was previously solved with a
small monkey patch to the library to restore the old behavior.

However, this makes upgrading the library difficult since the patch has
to be restored every time.

This patch addresses this by moving the command line flags into an
initFlags() method where appropriate and also moving all variables for
the flags from the Run() method into the command itself.

Fixes #3536
2017-10-18 00:08:45 +02:00

66 lines
1.4 KiB
Go

package command
import (
"fmt"
"github.com/mitchellh/cli"
)
var _ cli.Command = (*CatalogListDatacentersCommand)(nil)
type CatalogListDatacentersCommand struct {
BaseCommand
}
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.
`)
}
func (c *CatalogListDatacentersCommand) Run(args []string) int {
c.InitFlagSet()
if err := c.FlagSet.Parse(args); err != nil {
return 1
}
if l := len(c.FlagSet.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()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
dcs, err := client.Catalog().Datacenters()
if err != nil {
c.UI.Error(fmt.Sprintf("Error listing datacenters: %s", err))
}
for _, dc := range dcs {
c.UI.Info(dc)
}
return 0
}
func (c *CatalogListDatacentersCommand) Synopsis() string {
return "Lists all known datacenters"
}