diff --git a/command/catalog/helpers.go b/command/catalog/helpers.go new file mode 100644 index 0000000000..f0c4cc8507 --- /dev/null +++ b/command/catalog/helpers.go @@ -0,0 +1,23 @@ +package catalog + +import ( + "fmt" + "sort" + "strings" +) + +// mapToKV converts a map[string]string into a human-friendly key=value list, +// sorted by name. +func mapToKV(m map[string]string, joiner string) string { + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + sort.Strings(keys) + + r := make([]string, len(keys)) + for i, k := range keys { + r[i] = fmt.Sprintf("%s=%s", k, m[k]) + } + return strings.Join(r, joiner) +} diff --git a/command/catalog/helpers_oss.go b/command/catalog/helpers_oss.go new file mode 100644 index 0000000000..1469bfd3c2 --- /dev/null +++ b/command/catalog/helpers_oss.go @@ -0,0 +1,35 @@ +// +build !consulent + +package catalog + +import ( + "fmt" + "strings" + + "github.com/hashicorp/consul/api" +) + +func NodesHeader(isDetailed bool) string { + if isDetailed { + return "Node\x1fID\x1fAddress\x1fDC\x1fTaggedAddresses\x1fMeta" + } else { + return "Node\x1fID\x1fAddress\x1fDC" + } +} + +func NodeRow(node *api.Node, isDetailed bool) string { + if isDetailed { + return fmt.Sprintf("%s\x1f%s\x1f%s\x1f%s\x1f%s\x1f%s", + node.Node, node.ID, node.Address, node.Datacenter, + mapToKV(node.TaggedAddresses, ", "), mapToKV(node.Meta, ", ")) + } else { + // Shorten the ID in non-detailed mode to just the first octet. + id := node.ID + idx := strings.Index(id, "-") + if idx > 0 { + id = id[0:idx] + } + return fmt.Sprintf("%s\x1f%s\x1f%s\x1f%s", + node.Node, id, node.Address, node.Datacenter) + } +} diff --git a/command/catalog/list/nodes/catalog_list_nodes.go b/command/catalog/list/nodes/catalog_list_nodes.go index dbe36dbf9e..e1dcb95964 100644 --- a/command/catalog/list/nodes/catalog_list_nodes.go +++ b/command/catalog/list/nodes/catalog_list_nodes.go @@ -4,8 +4,8 @@ import ( "flag" "fmt" "io" - "sort" - "strings" + + "github.com/hashicorp/consul/command/catalog" "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/command/flags" @@ -54,6 +54,7 @@ func (c *cmd) init() { c.http = &flags.HTTPFlags{} flags.Merge(c.flags, c.http.ClientFlags()) flags.Merge(c.flags, c.http.ServerFlags()) + flags.Merge(c.flags, c.http.PartitionFlag()) c.help = flags.Usage(help, c.flags) } @@ -141,64 +142,24 @@ func (c *cmd) Run(args []string) int { // format about the nodes. func printNodes(nodes []*api.Node, detailed bool) (string, error) { var result []string - if detailed { - result = detailedNodes(nodes) - } else { - result = simpleNodes(nodes) - } - - return columnize.Format(result, &columnize.Config{Delim: string([]byte{0x1f})}), nil -} -func detailedNodes(nodes []*api.Node) []string { - result := make([]string, 0, len(nodes)+1) - header := "Node\x1fID\x1fAddress\x1fDC\x1fTaggedAddresses\x1fMeta" - result = append(result, header) + result = detailedNodes(nodes, detailed) - for _, node := range nodes { - result = append(result, fmt.Sprintf("%s\x1f%s\x1f%s\x1f%s\x1f%s\x1f%s", - node.Node, node.ID, node.Address, node.Datacenter, - mapToKV(node.TaggedAddresses, ", "), mapToKV(node.Meta, ", "))) - } - - return result + return columnize.Format(result, &columnize.Config{Delim: string([]byte{0x1f})}), nil } -func simpleNodes(nodes []*api.Node) []string { +func detailedNodes(nodes []*api.Node, detailed bool) []string { result := make([]string, 0, len(nodes)+1) - header := "Node\x1fID\x1fAddress\x1fDC" + header := catalog.NodesHeader(detailed) result = append(result, header) for _, node := range nodes { - // Shorten the ID in non-detailed mode to just the first octet. - id := node.ID - idx := strings.Index(id, "-") - if idx > 0 { - id = id[0:idx] - } - result = append(result, fmt.Sprintf("%s\x1f%s\x1f%s\x1f%s", - node.Node, id, node.Address, node.Datacenter)) + result = append(result, catalog.NodeRow(node, detailed)) } return result } -// mapToKV converts a map[string]string into a human-friendly key=value list, -// sorted by name. -func mapToKV(m map[string]string, joiner string) string { - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - sort.Strings(keys) - - r := make([]string, len(keys)) - for i, k := range keys { - r[i] = fmt.Sprintf("%s=%s", k, m[k]) - } - return strings.Join(r, joiner) -} - func (c *cmd) Synopsis() string { return synopsis } diff --git a/command/flags/http.go b/command/flags/http.go index a1a60ee406..139ab7ed08 100644 --- a/command/flags/http.go +++ b/command/flags/http.go @@ -85,6 +85,11 @@ func (f *HTTPFlags) MultiTenancyFlags() *flag.FlagSet { return fs } +func (f *HTTPFlags) PartitionFlag() *flag.FlagSet { + fs := flag.NewFlagSet("", flag.ContinueOnError) + f.AddPartitionFlag(fs) + return fs +} func (f *HTTPFlags) Addr() string { return f.address.String() }