Update CLI output functions to show scope info and cleanup (#416)
This commit is contained in:
1 parent
38ce9d9eac
commit
03436a73de
15 files changed
+222
-295
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/hashicorp/boundary/api/scopes"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/mitchellh/go-wordwrap"
|
||||
)
|
||||
@@ -16,6 +17,46 @@ import (
|
||||
// This is adapted from the code in the strings package for TrimSpace
|
||||
var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
|
||||
|
||||
func ScopeInfoForOutput(scp *scopes.ScopeInfo, maxLength int) string {
|
||||
vals := map[string]interface{}{
|
||||
"ID": scp.Id,
|
||||
"Type": scp.Type,
|
||||
"Name": scp.Name,
|
||||
}
|
||||
if scp.ParentScopeId != "" {
|
||||
vals["Parent Scope ID"] = scp.ParentScopeId
|
||||
}
|
||||
return WrapMap(4, maxLength, vals)
|
||||
}
|
||||
|
||||
func MaxAttributesLength(nonAttributesMap, attributesMap map[string]interface{}, keySubstMap map[string]string) int {
|
||||
// We always print a scope ID and in some cases this particular key ends up
|
||||
// being the longest key, so start with it as a baseline. It's always
|
||||
// indented by 2 in addition to the normal offset so take that into account.
|
||||
maxLength := len("Parent Scope ID") + 2
|
||||
for k := range nonAttributesMap {
|
||||
if len(k) > maxLength {
|
||||
maxLength = len(k)
|
||||
}
|
||||
}
|
||||
if len(attributesMap) > 0 {
|
||||
for k, v := range attributesMap {
|
||||
if keySubstMap != nil {
|
||||
if keySubstMap[k] != "" {
|
||||
attributesMap[keySubstMap[k]] = v
|
||||
delete(attributesMap, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
for k := range attributesMap {
|
||||
if len(k) > maxLength {
|
||||
maxLength = len(k)
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxLength
|
||||
}
|
||||
|
||||
func trimSpaceRight(in string) string {
|
||||
for stop := len(in); stop > 0; stop-- {
|
||||
c := in[stop-1]
|
||||
@@ -78,6 +119,9 @@ func WrapMap(prefixSpaces, maxLengthOverride int, input map[string]interface{})
|
||||
for _, k := range sortedKeys {
|
||||
v := input[k]
|
||||
spaces := maxKeyLength - len(k)
|
||||
if spaces < 0 {
|
||||
spaces = 0
|
||||
}
|
||||
ret = append(ret, fmt.Sprintf("%s%s%s%s",
|
||||
strings.Repeat(" ", prefixSpaces),
|
||||
fmt.Sprintf("%s: ", k),
|
||||
|
||||
Reference in new issue
Block a user