mirror of https://github.com/hashicorp/consul
Dhia Ayachi
3 years ago
committed by
GitHub
4 changed files with 71 additions and 47 deletions
@ -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) |
||||
} |
@ -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) |
||||
} |
||||
} |
Loading…
Reference in new issue