mirror of https://github.com/status-im/consul.git
commands: move catalog list services to separate pkg
This commit is contained in:
parent
117305eb4f
commit
0ec520582d
|
@ -1,7 +1,8 @@
|
||||||
package command
|
package catlistsvc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -12,12 +13,16 @@ import (
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ cli.Command = (*CatalogListServicesCommand)(nil)
|
func New(ui cli.Ui) *cmd {
|
||||||
|
c := &cmd{UI: ui}
|
||||||
|
c.initFlags()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
// CatalogListServicesCommand is a Command implementation that is used to fetch all the
|
type cmd struct {
|
||||||
// datacenters the agent knows about.
|
UI cli.Ui
|
||||||
type CatalogListServicesCommand struct {
|
flags *flag.FlagSet
|
||||||
BaseCommand
|
http *flags.HTTPFlags
|
||||||
|
|
||||||
// flags
|
// flags
|
||||||
node string
|
node string
|
||||||
|
@ -25,61 +30,36 @@ type CatalogListServicesCommand struct {
|
||||||
tags bool
|
tags bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CatalogListServicesCommand) initFlags() {
|
func (c *cmd) initFlags() {
|
||||||
c.InitFlagSet()
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
c.FlagSet.StringVar(&c.node, "node", "",
|
c.flags.StringVar(&c.node, "node", "",
|
||||||
"Node `id or name` for which to list services.")
|
"Node `id or name` for which to list services.")
|
||||||
c.FlagSet.Var((*flags.FlagMapValue)(&c.nodeMeta), "node-meta", "Metadata to "+
|
c.flags.Var((*flags.FlagMapValue)(&c.nodeMeta), "node-meta", "Metadata to "+
|
||||||
"filter nodes with the given `key=value` pairs. If specified, only "+
|
"filter nodes with the given `key=value` pairs. If specified, only "+
|
||||||
"services running on nodes matching the given metadata will be returned. "+
|
"services running on nodes matching the given metadata will be returned. "+
|
||||||
"This flag may be specified multiple times to filter on multiple sources "+
|
"This flag may be specified multiple times to filter on multiple sources "+
|
||||||
"of metadata.")
|
"of metadata.")
|
||||||
c.FlagSet.BoolVar(&c.tags, "tags", false, "Display each service's tags as a "+
|
c.flags.BoolVar(&c.tags, "tags", false, "Display each service's tags as a "+
|
||||||
"comma-separated list beside each service entry.")
|
"comma-separated list beside each service entry.")
|
||||||
|
|
||||||
|
c.http = &flags.HTTPFlags{}
|
||||||
|
flags.Merge(c.flags, c.http.ClientFlags())
|
||||||
|
flags.Merge(c.flags, c.http.ServerFlags())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CatalogListServicesCommand) Help() string {
|
func (c *cmd) Run(args []string) int {
|
||||||
c.initFlags()
|
c.initFlags()
|
||||||
return c.HelpCommand(`
|
if err := c.flags.Parse(args); err != nil {
|
||||||
Usage: consul catalog services [options]
|
|
||||||
|
|
||||||
Retrieves the list services registered in a given datacenter. By default, the
|
|
||||||
datacenter of the local agent is queried.
|
|
||||||
|
|
||||||
To retrieve the list of services:
|
|
||||||
|
|
||||||
$ consul catalog services
|
|
||||||
|
|
||||||
To include the services' tags in the output:
|
|
||||||
|
|
||||||
$ consul catalog services -tags
|
|
||||||
|
|
||||||
To list services which run on a particular node:
|
|
||||||
|
|
||||||
$ consul catalog services -node=web
|
|
||||||
|
|
||||||
To filter services on node metadata:
|
|
||||||
|
|
||||||
$ consul catalog services -node-meta="foo=bar"
|
|
||||||
|
|
||||||
For a full list of options and examples, please see the Consul documentation.
|
|
||||||
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CatalogListServicesCommand) Run(args []string) int {
|
|
||||||
c.initFlags()
|
|
||||||
if err := c.FlagSet.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
|
||||||
|
@ -144,6 +124,32 @@ func (c *CatalogListServicesCommand) Run(args []string) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CatalogListServicesCommand) Synopsis() string {
|
func (c *cmd) Synopsis() string {
|
||||||
return "Lists all registered services in a datacenter"
|
return "Lists all registered services in a datacenter"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Help() string {
|
||||||
|
s := `Usage: consul catalog services [options]
|
||||||
|
|
||||||
|
Retrieves the list services registered in a given datacenter. By default, the
|
||||||
|
datacenter of the local agent is queried.
|
||||||
|
|
||||||
|
To retrieve the list of services:
|
||||||
|
|
||||||
|
$ consul catalog services
|
||||||
|
|
||||||
|
To include the services' tags in the output:
|
||||||
|
|
||||||
|
$ consul catalog services -tags
|
||||||
|
|
||||||
|
To list services which run on a particular node:
|
||||||
|
|
||||||
|
$ consul catalog services -node=web
|
||||||
|
|
||||||
|
To filter services on node metadata:
|
||||||
|
|
||||||
|
$ consul catalog services -node-meta="foo=bar"
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package command
|
package catlistsvc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -9,24 +9,17 @@ import (
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testCatalogListServicesCommand(t *testing.T) (*cli.MockUi, *CatalogListServicesCommand) {
|
|
||||||
ui := cli.NewMockUi()
|
|
||||||
return ui, &CatalogListServicesCommand{
|
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
Flags: FlagSetHTTP,
|
|
||||||
UI: ui,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCatalogListServicesCommand_noTabs(t *testing.T) {
|
func TestCatalogListServicesCommand_noTabs(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
assertNoTabs(t, new(CatalogListServicesCommand))
|
if strings.ContainsRune(New(nil).Help(), '\t') {
|
||||||
|
t.Fatal("usage has tabs")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCatalogListServicesCommand_Validation(t *testing.T) {
|
func TestCatalogListServicesCommand_Validation(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
ui, c := testCatalogListServicesCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
|
||||||
cases := map[string]struct {
|
cases := map[string]struct {
|
||||||
args []string
|
args []string
|
||||||
|
@ -75,7 +68,8 @@ func TestCatalogListServicesCommand_Run(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("simple", func(t *testing.T) {
|
t.Run("simple", func(t *testing.T) {
|
||||||
ui, c := testCatalogListServicesCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a.HTTPAddr(),
|
"-http-addr=" + a.HTTPAddr(),
|
||||||
}
|
}
|
||||||
|
@ -91,7 +85,8 @@ func TestCatalogListServicesCommand_Run(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("tags", func(t *testing.T) {
|
t.Run("tags", func(t *testing.T) {
|
||||||
ui, c := testCatalogListServicesCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a.HTTPAddr(),
|
"-http-addr=" + a.HTTPAddr(),
|
||||||
"-tags",
|
"-tags",
|
||||||
|
@ -108,7 +103,8 @@ func TestCatalogListServicesCommand_Run(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("node_missing", func(t *testing.T) {
|
t.Run("node_missing", func(t *testing.T) {
|
||||||
ui, c := testCatalogListServicesCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a.HTTPAddr(),
|
"-http-addr=" + a.HTTPAddr(),
|
||||||
"-node", "not-a-real-node",
|
"-node", "not-a-real-node",
|
||||||
|
@ -125,7 +121,8 @@ func TestCatalogListServicesCommand_Run(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("node_present", func(t *testing.T) {
|
t.Run("node_present", func(t *testing.T) {
|
||||||
ui, c := testCatalogListServicesCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a.HTTPAddr(),
|
"-http-addr=" + a.HTTPAddr(),
|
||||||
"-node", a.Config.NodeName,
|
"-node", a.Config.NodeName,
|
||||||
|
@ -142,7 +139,8 @@ func TestCatalogListServicesCommand_Run(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("node-meta", func(t *testing.T) {
|
t.Run("node-meta", func(t *testing.T) {
|
||||||
ui, c := testCatalogListServicesCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a.HTTPAddr(),
|
"-http-addr=" + a.HTTPAddr(),
|
||||||
"-node-meta", "foo=bar",
|
"-node-meta", "foo=bar",
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/hashicorp/consul/command/cat"
|
"github.com/hashicorp/consul/command/cat"
|
||||||
"github.com/hashicorp/consul/command/catlistdc"
|
"github.com/hashicorp/consul/command/catlistdc"
|
||||||
"github.com/hashicorp/consul/command/catlistnodes"
|
"github.com/hashicorp/consul/command/catlistnodes"
|
||||||
|
"github.com/hashicorp/consul/command/catlistsvc"
|
||||||
"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"
|
||||||
|
@ -61,12 +62,7 @@ func init() {
|
||||||
},
|
},
|
||||||
|
|
||||||
"catalog services": func() (cli.Command, error) {
|
"catalog services": func() (cli.Command, error) {
|
||||||
return &CatalogListServicesCommand{
|
return catlistsvc.New(ui), nil
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
Flags: FlagSetHTTP,
|
|
||||||
UI: ui,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"event": func() (cli.Command, error) {
|
"event": func() (cli.Command, error) {
|
||||||
|
|
Loading…
Reference in New Issue