From 8e2de60b870b61bf36f9c505af21be375c1722b9 Mon Sep 17 00:00:00 2001 From: Dhia Ayachi Date: Fri, 27 Aug 2021 16:34:45 -0400 Subject: [PATCH] add partition flag to catalog commands (#10949) * add partition flag to catalog commands * add missing files --- command/catalog/helpers.go | 23 ++++++++ command/catalog/helpers_oss.go | 35 ++++++++++++ .../catalog/list/nodes/catalog_list_nodes.go | 55 +++---------------- command/flags/http.go | 5 ++ 4 files changed, 71 insertions(+), 47 deletions(-) create mode 100644 command/catalog/helpers.go create mode 100644 command/catalog/helpers_oss.go diff --git a/command/catalog/helpers.go b/command/catalog/helpers.go new file mode 100644 index 000000000..f0c4cc850 --- /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 000000000..1469bfd3c --- /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 dbe36dbf9..e1dcb9596 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) - } + + result = detailedNodes(nodes, detailed) return columnize.Format(result, &columnize.Config{Delim: string([]byte{0x1f})}), nil } -func detailedNodes(nodes []*api.Node) []string { +func detailedNodes(nodes []*api.Node, detailed bool) []string { result := make([]string, 0, len(nodes)+1) - header := "Node\x1fID\x1fAddress\x1fDC\x1fTaggedAddresses\x1fMeta" + header := catalog.NodesHeader(detailed) result = append(result, header) 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, ", "))) + result = append(result, catalog.NodeRow(node, detailed)) } return result } -func simpleNodes(nodes []*api.Node) []string { - result := make([]string, 0, len(nodes)+1) - header := "Node\x1fID\x1fAddress\x1fDC" - 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)) - } - - 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 a1a60ee40..139ab7ed0 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() }