From 03436a73dea3fbb7e06607a1db9c8ba08a140ae3 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 23 Sep 2020 03:01:39 -0400 Subject: [PATCH] Update CLI output functions to show scope info and cleanup (#416) --- internal/cmd/base/format.go | 44 ++++++++++++++++ internal/cmd/commands/accounts/funcs.go | 48 +++++------------ .../cmd/commands/authenticate/password.go | 2 +- internal/cmd/commands/authmethods/funcs.go | 48 +++++------------ internal/cmd/commands/authtokens/funcs.go | 28 +++++----- internal/cmd/commands/groups/funcs.go | 48 +++++++++++------ internal/cmd/commands/hostcatalogs/funcs.go | 45 +++++----------- internal/cmd/commands/hosts/funcs.go | 46 +++++------------ internal/cmd/commands/hostsets/funcs.go | 46 +++++------------ internal/cmd/commands/proxy/funcs.go | 18 ++----- internal/cmd/commands/roles/funcs.go | 20 ++++---- internal/cmd/commands/scopes/funcs.go | 20 ++++---- internal/cmd/commands/sessions/funcs.go | 33 ++++++------ internal/cmd/commands/targets/funcs.go | 51 ++++++------------- internal/cmd/commands/users/funcs.go | 20 ++++---- 15 files changed, 222 insertions(+), 295 deletions(-) diff --git a/internal/cmd/base/format.go b/internal/cmd/base/format.go index 20b2f690b..74aa4f740 100644 --- a/internal/cmd/base/format.go +++ b/internal/cmd/base/format.go @@ -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), diff --git a/internal/cmd/commands/accounts/funcs.go b/internal/cmd/commands/accounts/funcs.go index bc48a7d13..4e490a246 100644 --- a/internal/cmd/commands/accounts/funcs.go +++ b/internal/cmd/commands/accounts/funcs.go @@ -1,7 +1,6 @@ package accounts import ( - "fmt" "time" "github.com/hashicorp/boundary/api/accounts" @@ -9,15 +8,12 @@ import ( ) func generateAccountTableOutput(in *accounts.Account) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, "Type": in.Type, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), "Auth Method ID": in.AuthMethodId, } @@ -28,40 +24,20 @@ func generateAccountTableOutput(in *accounts.Account) string { nonAttributeMap["Description"] = in.Description } - maxLength := 0 - for k := range nonAttributeMap { - if len(k) > maxLength { - maxLength = len(k) - } - } - if len(in.Attributes) > 0 { - for k, v := range in.Attributes { - if attributeMap[k] != "" { - in.Attributes[attributeMap[k]] = v - delete(in.Attributes, k) - } - } - for k := range in.Attributes { - if len(k) > maxLength { - maxLength = len(k) - } - } - } + maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap) - ret = append(ret, "", "Account information:") - - ret = append(ret, - // We do +2 because there is another +2 offset for attributes below + ret := []string{ + "", + "Account information:", base.WrapMap(2, maxLength+2, nonAttributeMap), - ) + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } if len(in.Attributes) > 0 { - if true { - ret = append(ret, - fmt.Sprintf(" Attributes: %s", ""), - ) - } ret = append(ret, + "", + " Attributes:", base.WrapMap(4, maxLength, in.Attributes), ) } @@ -69,6 +45,6 @@ func generateAccountTableOutput(in *accounts.Account) string { return base.WrapForHelpText(ret) } -var attributeMap = map[string]string{ +var keySubstMap = map[string]string{ "login_name": "Login Name", } diff --git a/internal/cmd/commands/authenticate/password.go b/internal/cmd/commands/authenticate/password.go index cf13dd646..790f46049 100644 --- a/internal/cmd/commands/authenticate/password.go +++ b/internal/cmd/commands/authenticate/password.go @@ -141,7 +141,7 @@ func (c *PasswordCommand) Run(args []string) int { c.UI.Output(base.WrapForHelpText([]string{ "", "Authentication information:", - fmt.Sprintf(" Expiration Time: %s", token.ExpirationTime.Local().Format(time.RFC3339)), + fmt.Sprintf(" Expiration Time: %s", token.ExpirationTime.Local().Format(time.RFC1123)), fmt.Sprintf(" Token: %s", token.Token), fmt.Sprintf(" User ID: %s", token.UserId), })) diff --git a/internal/cmd/commands/authmethods/funcs.go b/internal/cmd/commands/authmethods/funcs.go index daf866c38..4a2b14e8f 100644 --- a/internal/cmd/commands/authmethods/funcs.go +++ b/internal/cmd/commands/authmethods/funcs.go @@ -1,7 +1,6 @@ package authmethods import ( - "fmt" "time" "github.com/hashicorp/boundary/api/authmethods" @@ -22,15 +21,12 @@ func addPasswordFlags(c *PasswordCommand, f *base.FlagSet) { } func generateAuthMethodTableOutput(in *authmethods.AuthMethod) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, "Type": in.Type, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), } if in.Name != "" { @@ -40,40 +36,20 @@ func generateAuthMethodTableOutput(in *authmethods.AuthMethod) string { nonAttributeMap["Description"] = in.Description } - maxLength := 0 - for k := range nonAttributeMap { - if len(k) > maxLength { - maxLength = len(k) - } - } - if len(in.Attributes) > 0 { - for k, v := range in.Attributes { - if attributeMap[k] != "" { - in.Attributes[attributeMap[k]] = v - delete(in.Attributes, k) - } - } - for k := range in.Attributes { - if len(k) > maxLength { - maxLength = len(k) - } - } - } + maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap) - ret = append(ret, "", "Auth method information:") - - ret = append(ret, - // We do +2 because there is another +2 offset for attributes below + ret := []string{ + "", + "Auth Method information:", base.WrapMap(2, maxLength+2, nonAttributeMap), - ) + "", + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } if len(in.Attributes) > 0 { - if true { - ret = append(ret, - fmt.Sprintf(" Attributes: %s", ""), - ) - } ret = append(ret, + " Attributes:", base.WrapMap(4, maxLength, in.Attributes), ) } @@ -81,7 +57,7 @@ func generateAuthMethodTableOutput(in *authmethods.AuthMethod) string { return base.WrapForHelpText(ret) } -var attributeMap = map[string]string{ +var keySubstMap = map[string]string{ "min_login_name_length": "Minimum Login Name Length", "min_password_length": "Minimum Password Length", } diff --git a/internal/cmd/commands/authtokens/funcs.go b/internal/cmd/commands/authtokens/funcs.go index 2c9dc3e21..d4069b165 100644 --- a/internal/cmd/commands/authtokens/funcs.go +++ b/internal/cmd/commands/authtokens/funcs.go @@ -10,28 +10,24 @@ import ( func generateAuthTokenTableOutput(in *authtokens.AuthToken) string { nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Auth Method ID": in.AuthMethodId, "User ID": in.UserId, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), - "Expiration Time": in.ExpirationTime.Local().Format(time.RFC3339), - "Approximate Last Used Time": in.ApproximateLastUsedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), + "Expiration Time": in.ExpirationTime.Local().Format(time.RFC1123), + "Approximate Last Used Time": in.ApproximateLastUsedTime.Local().Format(time.RFC1123), } - maxLength := 0 - for k := range nonAttributeMap { - if len(k) > maxLength { - maxLength = len(k) - } - } + maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil) - ret := []string{"", "Auth Token information:"} - - ret = append(ret, - // We do +2 because there is another +2 offset for host sets below + ret := []string{ + "", + "Auth Token information:", base.WrapMap(2, maxLength+2, nonAttributeMap), - ) + "", + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } return base.WrapForHelpText(ret) } diff --git a/internal/cmd/commands/groups/funcs.go b/internal/cmd/commands/groups/funcs.go index 58fee35c0..2c91a3159 100644 --- a/internal/cmd/commands/groups/funcs.go +++ b/internal/cmd/commands/groups/funcs.go @@ -71,14 +71,11 @@ func populateFlags(c *Command, f *base.FlagSet, flagNames []string) { } func generateGroupTableOutput(in *groups.Group) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), } if in.Name != "" { @@ -88,22 +85,41 @@ func generateGroupTableOutput(in *groups.Group) string { nonAttributeMap["Description"] = in.Description } - ret = append(ret, "", "Group information:") + maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil) - ret = append(ret, - base.WrapMap(2, 0, nonAttributeMap), - ) + var groupMaps []map[string]interface{} + if len(in.Members) > 0 { + for _, member := range in.Members { + m := map[string]interface{}{ + "ID": member.Id, + "Scope ID": member.ScopeId, + } + groupMaps = append(groupMaps, m) + } + if l := len("Scope ID"); l > maxLength { + maxLength = l + } + } + + ret := []string{ + "", + "Group information:", + base.WrapMap(2, maxLength+2, nonAttributeMap), + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } if len(in.Members) > 0 { ret = append(ret, - fmt.Sprintf(" Members: %s", ""), - ) - } - for _, member := range in.Members { - ret = append(ret, - fmt.Sprintf(" ID: %s", member.Id), - fmt.Sprintf(" Scope ID: %s", member.ScopeId), + "", + " Members:", ) + for _, m := range groupMaps { + ret = append(ret, + base.WrapMap(4, maxLength, m), + "", + ) + } } return base.WrapForHelpText(ret) } diff --git a/internal/cmd/commands/hostcatalogs/funcs.go b/internal/cmd/commands/hostcatalogs/funcs.go index f76754e63..b095c0c1e 100644 --- a/internal/cmd/commands/hostcatalogs/funcs.go +++ b/internal/cmd/commands/hostcatalogs/funcs.go @@ -1,7 +1,6 @@ package hostcatalogs import ( - "fmt" "time" "github.com/hashicorp/boundary/api/hostcatalogs" @@ -9,15 +8,12 @@ import ( ) func generateHostCatalogTableOutput(in *hostcatalogs.HostCatalog) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, "Type": in.Type, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), } if in.Name != "" { @@ -27,36 +23,21 @@ func generateHostCatalogTableOutput(in *hostcatalogs.HostCatalog) string { nonAttributeMap["Description"] = in.Description } - maxLength := 0 - for k := range nonAttributeMap { - if len(k) > maxLength { - maxLength = len(k) - } - } - if len(in.Attributes) > 0 { - for k, v := range in.Attributes { - if attributeMap[k] != "" { - in.Attributes[attributeMap[k]] = v - delete(in.Attributes, k) - } - } - for k := range in.Attributes { - if len(k) > maxLength { - maxLength = len(k) - } - } - } + maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap) - ret = append(ret, "", "Host catalog information:") - - ret = append(ret, - // We do +2 because there is another +2 offset for attributes below + ret := []string{ + "", + "Host Catalog information:", base.WrapMap(2, maxLength+2, nonAttributeMap), - ) + "", + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } if len(in.Attributes) > 0 { ret = append(ret, - fmt.Sprintf(" Attributes: %s", ""), + "", + " Attributes:", base.WrapMap(4, maxLength, in.Attributes), ) } @@ -64,6 +45,6 @@ func generateHostCatalogTableOutput(in *hostcatalogs.HostCatalog) string { return base.WrapForHelpText(ret) } -var attributeMap = map[string]string{ +var keySubstMap = map[string]string{ "address": "Address", } diff --git a/internal/cmd/commands/hosts/funcs.go b/internal/cmd/commands/hosts/funcs.go index 3f4f197a3..9130cd673 100644 --- a/internal/cmd/commands/hosts/funcs.go +++ b/internal/cmd/commands/hosts/funcs.go @@ -1,7 +1,6 @@ package hosts import ( - "fmt" "time" "github.com/hashicorp/boundary/api/hosts" @@ -9,15 +8,12 @@ import ( ) func generateHostTableOutput(in *hosts.Host) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, "Type": in.Type, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), "Host Catalog ID": in.HostCatalogId, } @@ -28,35 +24,20 @@ func generateHostTableOutput(in *hosts.Host) string { nonAttributeMap["Description"] = in.Description } - maxLength := 0 - for k := range nonAttributeMap { - if len(k) > maxLength { - maxLength = len(k) - } - } - if len(in.Attributes) > 0 { - for k, v := range in.Attributes { - if attributeMap[k] != "" { - in.Attributes[attributeMap[k]] = v - delete(in.Attributes, k) - } - } - for k := range in.Attributes { - if len(k) > maxLength { - maxLength = len(k) - } - } - } + maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap) - ret = append(ret, "", "Host information:") - - ret = append(ret, - // We do +2 because there is another +2 offset for attributes below + ret := []string{ + "", + "Host information:", base.WrapMap(2, maxLength+2, nonAttributeMap), - ) + "", + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } if len(in.HostSetIds) > 0 { ret = append(ret, + "", " Host Set IDs:", base.WrapSlice(4, in.HostSetIds), ) @@ -64,7 +45,8 @@ func generateHostTableOutput(in *hosts.Host) string { if len(in.Attributes) > 0 { ret = append(ret, - fmt.Sprintf(" Attributes: %s", ""), + "", + " Attributes:", base.WrapMap(4, maxLength, in.Attributes), ) } @@ -72,4 +54,4 @@ func generateHostTableOutput(in *hosts.Host) string { return base.WrapForHelpText(ret) } -var attributeMap = map[string]string{} +var keySubstMap = map[string]string{} diff --git a/internal/cmd/commands/hostsets/funcs.go b/internal/cmd/commands/hostsets/funcs.go index a1ebf19a8..47d3bb5d8 100644 --- a/internal/cmd/commands/hostsets/funcs.go +++ b/internal/cmd/commands/hostsets/funcs.go @@ -1,7 +1,6 @@ package hostsets import ( - "fmt" "time" "github.com/hashicorp/boundary/api/hostsets" @@ -9,15 +8,12 @@ import ( ) func generateHostSetTableOutput(in *hostsets.HostSet) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, "Type": in.Type, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), "Host Catalog ID": in.HostCatalogId, } @@ -28,35 +24,20 @@ func generateHostSetTableOutput(in *hostsets.HostSet) string { nonAttributeMap["Description"] = in.Description } - maxLength := 0 - for k := range nonAttributeMap { - if len(k) > maxLength { - maxLength = len(k) - } - } - if len(in.Attributes) > 0 { - for k, v := range in.Attributes { - if attributeMap[k] != "" { - in.Attributes[attributeMap[k]] = v - delete(in.Attributes, k) - } - } - for k := range in.Attributes { - if len(k) > maxLength { - maxLength = len(k) - } - } - } + maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap) - ret = append(ret, "", "Host set information:") - - ret = append(ret, - // We do +2 because there is another +2 offset for attributes below + ret := []string{ + "", + "Host Set information:", base.WrapMap(2, maxLength+2, nonAttributeMap), - ) + "", + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } if len(in.HostIds) > 0 { ret = append(ret, + "", " Host IDs:", base.WrapSlice(4, in.HostIds), ) @@ -64,7 +45,8 @@ func generateHostSetTableOutput(in *hostsets.HostSet) string { if len(in.Attributes) > 0 { ret = append(ret, - fmt.Sprintf(" Attributes: %s", ""), + "", + " Attributes:", base.WrapMap(4, maxLength, in.Attributes), ) } @@ -72,4 +54,4 @@ func generateHostSetTableOutput(in *hostsets.HostSet) string { return base.WrapForHelpText(ret) } -var attributeMap = map[string]string{} +var keySubstMap = map[string]string{} diff --git a/internal/cmd/commands/proxy/funcs.go b/internal/cmd/commands/proxy/funcs.go index 6a33de346..37489c666 100644 --- a/internal/cmd/commands/proxy/funcs.go +++ b/internal/cmd/commands/proxy/funcs.go @@ -7,8 +7,6 @@ import ( ) func generateSessionInfoTableOutput(in SessionInfo) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "Session ID": in.SessionId, "Protocol": in.Protocol, @@ -18,19 +16,13 @@ func generateSessionInfoTableOutput(in SessionInfo) string { "Connection Limit": in.ConnectionLimit, } - maxLength := 0 - for k := range nonAttributeMap { - if len(k) > maxLength { - maxLength = len(k) - } - } + maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil) - ret = append(ret, "", "Proxy listening information:") - - ret = append(ret, - // We do +2 because there is another +2 offset for host sets below + ret := []string{ + "", + "Proxy listening information:", base.WrapMap(2, maxLength+2, nonAttributeMap), - ) + } return base.WrapForHelpText(ret) } diff --git a/internal/cmd/commands/roles/funcs.go b/internal/cmd/commands/roles/funcs.go index e2cef233b..05bd787d3 100644 --- a/internal/cmd/commands/roles/funcs.go +++ b/internal/cmd/commands/roles/funcs.go @@ -87,14 +87,11 @@ func populateFlags(c *Command, f *base.FlagSet, flagNames []string) { } func generateRoleTableOutput(in *roles.Role) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), } if in.Name != "" { @@ -107,11 +104,16 @@ func generateRoleTableOutput(in *roles.Role) string { nonAttributeMap["Grant Scope ID"] = in.GrantScopeId } - ret = append(ret, "", "Role information:") + maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil) - ret = append(ret, - base.WrapMap(2, 0, nonAttributeMap), - ) + ret := []string{ + "", + "Role information:", + base.WrapMap(2, maxLength+2, nonAttributeMap), + "", + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } if len(in.Principals) > 0 { ret = append(ret, diff --git a/internal/cmd/commands/scopes/funcs.go b/internal/cmd/commands/scopes/funcs.go index 94c2039b3..09fac7b15 100644 --- a/internal/cmd/commands/scopes/funcs.go +++ b/internal/cmd/commands/scopes/funcs.go @@ -8,14 +8,11 @@ import ( ) func generateScopeTableOutput(in *scopes.Scope) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), } if in.Name != "" { @@ -25,11 +22,16 @@ func generateScopeTableOutput(in *scopes.Scope) string { nonAttributeMap["Description"] = in.Description } - ret = append(ret, "", "Scope information:") + maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil) - ret = append(ret, - base.WrapMap(2, 0, nonAttributeMap), - ) + ret := []string{ + "", + "Scope information:", + base.WrapMap(2, maxLength+2, nonAttributeMap), + "", + " Scope (parent):", + base.ScopeInfoForOutput(in.Scope, maxLength), + } return base.WrapForHelpText(ret) } diff --git a/internal/cmd/commands/sessions/funcs.go b/internal/cmd/commands/sessions/funcs.go index e532e3442..8100da88e 100644 --- a/internal/cmd/commands/sessions/funcs.go +++ b/internal/cmd/commands/sessions/funcs.go @@ -12,10 +12,9 @@ func generateSessionTableOutput(in *sessions.Session) string { nonAttributeMap := map[string]interface{}{ "ID": in.Id, "Target ID": in.TargetId, - "Scope ID": in.Scope.Id, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), - "Expiration Time": in.ExpirationTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), + "Expiration Time": in.ExpirationTime.Local().Format(time.RFC1123), "Version": in.Version, "Type": in.Type, "Auth Token ID": in.AuthTokenId, @@ -26,20 +25,15 @@ func generateSessionTableOutput(in *sessions.Session) string { "Status": in.Status, } - maxLength := 0 - for k := range nonAttributeMap { - if len(k) > maxLength { - maxLength = len(k) - } - } + maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil) var statesMaps []map[string]interface{} if len(in.States) > 0 { for _, state := range in.States { m := map[string]interface{}{ "Status": state.Status, - "Start Time": state.StartTime.Local().Format(time.RFC3339), - "End Time": state.EndTime.Local().Format(time.RFC3339), + "Start Time": state.StartTime.Local().Format(time.RFC1123), + "End Time": state.EndTime.Local().Format(time.RFC1123), } statesMaps = append(statesMaps, m) } @@ -61,16 +55,19 @@ func generateSessionTableOutput(in *sessions.Session) string { } } - ret := []string{"", "Session information:"} - - ret = append(ret, - // We do +2 because there is another +2 offset for host sets below + ret := []string{ + "", + "Session information:", base.WrapMap(2, maxLength+2, nonAttributeMap), - ) + "", + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } if len(in.States) > 0 { ret = append(ret, - fmt.Sprintf(" States: %s", ""), + "", + " States:", ) for _, m := range statesMaps { ret = append(ret, diff --git a/internal/cmd/commands/targets/funcs.go b/internal/cmd/commands/targets/funcs.go index e6a1f9b02..5b35ebd71 100644 --- a/internal/cmd/commands/targets/funcs.go +++ b/internal/cmd/commands/targets/funcs.go @@ -1,7 +1,6 @@ package targets import ( - "fmt" "time" "github.com/hashicorp/boundary/api/scopes" @@ -10,15 +9,12 @@ import ( ) func generateTargetTableOutput(in *targets.Target) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, "Type": in.Type, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), "Session Connection Limit": in.SessionConnectionLimit, "Session Max Seconds": in.SessionMaxSeconds, } @@ -30,25 +26,7 @@ func generateTargetTableOutput(in *targets.Target) string { nonAttributeMap["Description"] = in.Description } - maxLength := 0 - for k := range nonAttributeMap { - if len(k) > maxLength { - maxLength = len(k) - } - } - if len(in.Attributes) > 0 { - for k, v := range in.Attributes { - if attributeMap[k] != "" { - in.Attributes[attributeMap[k]] = v - delete(in.Attributes, k) - } - } - for k := range in.Attributes { - if len(k) > maxLength { - maxLength = len(k) - } - } - } + maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap) var hostSetMaps []map[string]interface{} if len(in.HostSets) > 0 { @@ -63,16 +41,20 @@ func generateTargetTableOutput(in *targets.Target) string { maxLength = l } } - ret = append(ret, "", "Target information:") - ret = append(ret, - // We do +2 because there is another +2 offset for host sets below + ret := []string{ + "", + "Target information:", base.WrapMap(2, maxLength+2, nonAttributeMap), - ) + "", + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } if len(in.HostSets) > 0 { ret = append(ret, - fmt.Sprintf(" Host Sets: %s", ""), + "", + " Host Sets:", ) for _, m := range hostSetMaps { ret = append(ret, @@ -83,12 +65,9 @@ func generateTargetTableOutput(in *targets.Target) string { } if len(in.Attributes) > 0 { - if true { - ret = append(ret, - fmt.Sprintf(" Attributes: %s", ""), - ) - } ret = append(ret, + "", + " Attributes:", base.WrapMap(4, maxLength, in.Attributes), ) } @@ -125,7 +104,7 @@ func generateAuthorizationTableOutput(in *targets.SessionAuthorization) string { return base.WrapForHelpText(ret) } -var attributeMap = map[string]string{ +var keySubstMap = map[string]string{ "default_port": "Default Port", } diff --git a/internal/cmd/commands/users/funcs.go b/internal/cmd/commands/users/funcs.go index 091b1b710..b2b4dc010 100644 --- a/internal/cmd/commands/users/funcs.go +++ b/internal/cmd/commands/users/funcs.go @@ -8,14 +8,11 @@ import ( ) func generateUserTableOutput(in *users.User) string { - var ret []string - nonAttributeMap := map[string]interface{}{ "ID": in.Id, - "Scope ID": in.Scope.Id, "Version": in.Version, - "Created Time": in.CreatedTime.Local().Format(time.RFC3339), - "Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), + "Created Time": in.CreatedTime.Local().Format(time.RFC1123), + "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123), } if in.Name != "" { @@ -25,11 +22,16 @@ func generateUserTableOutput(in *users.User) string { nonAttributeMap["Description"] = in.Description } - ret = append(ret, "", "User information:") + maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil) - ret = append(ret, - base.WrapMap(2, 0, nonAttributeMap), - ) + ret := []string{ + "", + "User information:", + base.WrapMap(2, maxLength+2, nonAttributeMap), + "", + " Scope:", + base.ScopeInfoForOutput(in.Scope, maxLength), + } return base.WrapForHelpText(ret) }