Update CLI output functions to show scope info and cleanup (#416)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-09-23 03:01:39 -04:00
1 parent 38ce9d9eac
commit 03436a73de
15 files changed
+222 -295

No files matched your search

+44
View File
@@ -9,6 +9,7 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/hashicorp/boundary/api/scopes"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/mitchellh/go-wordwrap" "github.com/mitchellh/go-wordwrap"
) )
@@ -16,6 +17,46 @@ import (
// This is adapted from the code in the strings package for TrimSpace // 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} 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 { func trimSpaceRight(in string) string {
for stop := len(in); stop > 0; stop-- { for stop := len(in); stop > 0; stop-- {
c := in[stop-1] c := in[stop-1]
@@ -78,6 +119,9 @@ func WrapMap(prefixSpaces, maxLengthOverride int, input map[string]interface{})
for _, k := range sortedKeys { for _, k := range sortedKeys {
v := input[k] v := input[k]
spaces := maxKeyLength - len(k) spaces := maxKeyLength - len(k)
if spaces < 0 {
spaces = 0
}
ret = append(ret, fmt.Sprintf("%s%s%s%s", ret = append(ret, fmt.Sprintf("%s%s%s%s",
strings.Repeat(" ", prefixSpaces), strings.Repeat(" ", prefixSpaces),
fmt.Sprintf("%s: ", k), fmt.Sprintf("%s: ", k),
+12 -36
View File
@@ -1,7 +1,6 @@
package accounts package accounts
import ( import (
"fmt"
"time" "time"
"github.com/hashicorp/boundary/api/accounts" "github.com/hashicorp/boundary/api/accounts"
@@ -9,15 +8,12 @@ import (
) )
func generateAccountTableOutput(in *accounts.Account) string { func generateAccountTableOutput(in *accounts.Account) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Type": in.Type, "Type": in.Type,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
"Auth Method ID": in.AuthMethodId, "Auth Method ID": in.AuthMethodId,
} }
@@ -28,40 +24,20 @@ func generateAccountTableOutput(in *accounts.Account) string {
nonAttributeMap["Description"] = in.Description nonAttributeMap["Description"] = in.Description
} }
maxLength := 0 maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap)
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)
}
}
}
ret = append(ret, "", "Account information:") ret := []string{
"",
ret = append(ret, "Account information:",
// We do +2 because there is another +2 offset for attributes below
base.WrapMap(2, maxLength+2, nonAttributeMap), base.WrapMap(2, maxLength+2, nonAttributeMap),
) " Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
if len(in.Attributes) > 0 { if len(in.Attributes) > 0 {
if true {
ret = append(ret,
fmt.Sprintf(" Attributes: %s", ""),
)
}
ret = append(ret, ret = append(ret,
"",
" Attributes:",
base.WrapMap(4, maxLength, in.Attributes), base.WrapMap(4, maxLength, in.Attributes),
) )
} }
@@ -69,6 +45,6 @@ func generateAccountTableOutput(in *accounts.Account) string {
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
var attributeMap = map[string]string{ var keySubstMap = map[string]string{
"login_name": "Login Name", "login_name": "Login Name",
} }
@@ -141,7 +141,7 @@ func (c *PasswordCommand) Run(args []string) int {
c.UI.Output(base.WrapForHelpText([]string{ c.UI.Output(base.WrapForHelpText([]string{
"", "",
"Authentication information:", "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(" Token: %s", token.Token),
fmt.Sprintf(" User ID: %s", token.UserId), fmt.Sprintf(" User ID: %s", token.UserId),
})) }))
+12 -36
View File
@@ -1,7 +1,6 @@
package authmethods package authmethods
import ( import (
"fmt"
"time" "time"
"github.com/hashicorp/boundary/api/authmethods" "github.com/hashicorp/boundary/api/authmethods"
@@ -22,15 +21,12 @@ func addPasswordFlags(c *PasswordCommand, f *base.FlagSet) {
} }
func generateAuthMethodTableOutput(in *authmethods.AuthMethod) string { func generateAuthMethodTableOutput(in *authmethods.AuthMethod) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Type": in.Type, "Type": in.Type,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
} }
if in.Name != "" { if in.Name != "" {
@@ -40,40 +36,20 @@ func generateAuthMethodTableOutput(in *authmethods.AuthMethod) string {
nonAttributeMap["Description"] = in.Description nonAttributeMap["Description"] = in.Description
} }
maxLength := 0 maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap)
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)
}
}
}
ret = append(ret, "", "Auth method information:") ret := []string{
"",
ret = append(ret, "Auth Method information:",
// We do +2 because there is another +2 offset for attributes below
base.WrapMap(2, maxLength+2, nonAttributeMap), base.WrapMap(2, maxLength+2, nonAttributeMap),
) "",
" Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
if len(in.Attributes) > 0 { if len(in.Attributes) > 0 {
if true {
ret = append(ret,
fmt.Sprintf(" Attributes: %s", ""),
)
}
ret = append(ret, ret = append(ret,
" Attributes:",
base.WrapMap(4, maxLength, in.Attributes), base.WrapMap(4, maxLength, in.Attributes),
) )
} }
@@ -81,7 +57,7 @@ func generateAuthMethodTableOutput(in *authmethods.AuthMethod) string {
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
var attributeMap = map[string]string{ var keySubstMap = map[string]string{
"min_login_name_length": "Minimum Login Name Length", "min_login_name_length": "Minimum Login Name Length",
"min_password_length": "Minimum Password Length", "min_password_length": "Minimum Password Length",
} }
+12 -16
View File
@@ -10,28 +10,24 @@ import (
func generateAuthTokenTableOutput(in *authtokens.AuthToken) string { func generateAuthTokenTableOutput(in *authtokens.AuthToken) string {
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Auth Method ID": in.AuthMethodId, "Auth Method ID": in.AuthMethodId,
"User ID": in.UserId, "User ID": in.UserId,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
"Expiration Time": in.ExpirationTime.Local().Format(time.RFC3339), "Expiration Time": in.ExpirationTime.Local().Format(time.RFC1123),
"Approximate Last Used Time": in.ApproximateLastUsedTime.Local().Format(time.RFC3339), "Approximate Last Used Time": in.ApproximateLastUsedTime.Local().Format(time.RFC1123),
} }
maxLength := 0 maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
for k := range nonAttributeMap {
if len(k) > maxLength {
maxLength = len(k)
}
}
ret := []string{"", "Auth Token information:"} ret := []string{
"",
ret = append(ret, "Auth Token information:",
// We do +2 because there is another +2 offset for host sets below
base.WrapMap(2, maxLength+2, nonAttributeMap), base.WrapMap(2, maxLength+2, nonAttributeMap),
) "",
" Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
+32 -16
View File
@@ -71,14 +71,11 @@ func populateFlags(c *Command, f *base.FlagSet, flagNames []string) {
} }
func generateGroupTableOutput(in *groups.Group) string { func generateGroupTableOutput(in *groups.Group) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
} }
if in.Name != "" { if in.Name != "" {
@@ -88,22 +85,41 @@ func generateGroupTableOutput(in *groups.Group) string {
nonAttributeMap["Description"] = in.Description nonAttributeMap["Description"] = in.Description
} }
ret = append(ret, "", "Group information:") maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
ret = append(ret, var groupMaps []map[string]interface{}
base.WrapMap(2, 0, nonAttributeMap), 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 { if len(in.Members) > 0 {
ret = append(ret, ret = append(ret,
fmt.Sprintf(" Members: %s", ""), "",
) " Members:",
}
for _, member := range in.Members {
ret = append(ret,
fmt.Sprintf(" ID: %s", member.Id),
fmt.Sprintf(" Scope ID: %s", member.ScopeId),
) )
for _, m := range groupMaps {
ret = append(ret,
base.WrapMap(4, maxLength, m),
"",
)
}
} }
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
+13 -32
View File
@@ -1,7 +1,6 @@
package hostcatalogs package hostcatalogs
import ( import (
"fmt"
"time" "time"
"github.com/hashicorp/boundary/api/hostcatalogs" "github.com/hashicorp/boundary/api/hostcatalogs"
@@ -9,15 +8,12 @@ import (
) )
func generateHostCatalogTableOutput(in *hostcatalogs.HostCatalog) string { func generateHostCatalogTableOutput(in *hostcatalogs.HostCatalog) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Type": in.Type, "Type": in.Type,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
} }
if in.Name != "" { if in.Name != "" {
@@ -27,36 +23,21 @@ func generateHostCatalogTableOutput(in *hostcatalogs.HostCatalog) string {
nonAttributeMap["Description"] = in.Description nonAttributeMap["Description"] = in.Description
} }
maxLength := 0 maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap)
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)
}
}
}
ret = append(ret, "", "Host catalog information:") ret := []string{
"",
ret = append(ret, "Host Catalog information:",
// We do +2 because there is another +2 offset for attributes below
base.WrapMap(2, maxLength+2, nonAttributeMap), base.WrapMap(2, maxLength+2, nonAttributeMap),
) "",
" Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
if len(in.Attributes) > 0 { if len(in.Attributes) > 0 {
ret = append(ret, ret = append(ret,
fmt.Sprintf(" Attributes: %s", ""), "",
" Attributes:",
base.WrapMap(4, maxLength, in.Attributes), base.WrapMap(4, maxLength, in.Attributes),
) )
} }
@@ -64,6 +45,6 @@ func generateHostCatalogTableOutput(in *hostcatalogs.HostCatalog) string {
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
var attributeMap = map[string]string{ var keySubstMap = map[string]string{
"address": "Address", "address": "Address",
} }
+14 -32
View File
@@ -1,7 +1,6 @@
package hosts package hosts
import ( import (
"fmt"
"time" "time"
"github.com/hashicorp/boundary/api/hosts" "github.com/hashicorp/boundary/api/hosts"
@@ -9,15 +8,12 @@ import (
) )
func generateHostTableOutput(in *hosts.Host) string { func generateHostTableOutput(in *hosts.Host) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Type": in.Type, "Type": in.Type,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
"Host Catalog ID": in.HostCatalogId, "Host Catalog ID": in.HostCatalogId,
} }
@@ -28,35 +24,20 @@ func generateHostTableOutput(in *hosts.Host) string {
nonAttributeMap["Description"] = in.Description nonAttributeMap["Description"] = in.Description
} }
maxLength := 0 maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap)
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)
}
}
}
ret = append(ret, "", "Host information:") ret := []string{
"",
ret = append(ret, "Host information:",
// We do +2 because there is another +2 offset for attributes below
base.WrapMap(2, maxLength+2, nonAttributeMap), base.WrapMap(2, maxLength+2, nonAttributeMap),
) "",
" Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
if len(in.HostSetIds) > 0 { if len(in.HostSetIds) > 0 {
ret = append(ret, ret = append(ret,
"",
" Host Set IDs:", " Host Set IDs:",
base.WrapSlice(4, in.HostSetIds), base.WrapSlice(4, in.HostSetIds),
) )
@@ -64,7 +45,8 @@ func generateHostTableOutput(in *hosts.Host) string {
if len(in.Attributes) > 0 { if len(in.Attributes) > 0 {
ret = append(ret, ret = append(ret,
fmt.Sprintf(" Attributes: %s", ""), "",
" Attributes:",
base.WrapMap(4, maxLength, in.Attributes), base.WrapMap(4, maxLength, in.Attributes),
) )
} }
@@ -72,4 +54,4 @@ func generateHostTableOutput(in *hosts.Host) string {
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
var attributeMap = map[string]string{} var keySubstMap = map[string]string{}
+14 -32
View File
@@ -1,7 +1,6 @@
package hostsets package hostsets
import ( import (
"fmt"
"time" "time"
"github.com/hashicorp/boundary/api/hostsets" "github.com/hashicorp/boundary/api/hostsets"
@@ -9,15 +8,12 @@ import (
) )
func generateHostSetTableOutput(in *hostsets.HostSet) string { func generateHostSetTableOutput(in *hostsets.HostSet) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Type": in.Type, "Type": in.Type,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
"Host Catalog ID": in.HostCatalogId, "Host Catalog ID": in.HostCatalogId,
} }
@@ -28,35 +24,20 @@ func generateHostSetTableOutput(in *hostsets.HostSet) string {
nonAttributeMap["Description"] = in.Description nonAttributeMap["Description"] = in.Description
} }
maxLength := 0 maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap)
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)
}
}
}
ret = append(ret, "", "Host set information:") ret := []string{
"",
ret = append(ret, "Host Set information:",
// We do +2 because there is another +2 offset for attributes below
base.WrapMap(2, maxLength+2, nonAttributeMap), base.WrapMap(2, maxLength+2, nonAttributeMap),
) "",
" Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
if len(in.HostIds) > 0 { if len(in.HostIds) > 0 {
ret = append(ret, ret = append(ret,
"",
" Host IDs:", " Host IDs:",
base.WrapSlice(4, in.HostIds), base.WrapSlice(4, in.HostIds),
) )
@@ -64,7 +45,8 @@ func generateHostSetTableOutput(in *hostsets.HostSet) string {
if len(in.Attributes) > 0 { if len(in.Attributes) > 0 {
ret = append(ret, ret = append(ret,
fmt.Sprintf(" Attributes: %s", ""), "",
" Attributes:",
base.WrapMap(4, maxLength, in.Attributes), base.WrapMap(4, maxLength, in.Attributes),
) )
} }
@@ -72,4 +54,4 @@ func generateHostSetTableOutput(in *hostsets.HostSet) string {
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
var attributeMap = map[string]string{} var keySubstMap = map[string]string{}
+5 -13
View File
@@ -7,8 +7,6 @@ import (
) )
func generateSessionInfoTableOutput(in SessionInfo) string { func generateSessionInfoTableOutput(in SessionInfo) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"Session ID": in.SessionId, "Session ID": in.SessionId,
"Protocol": in.Protocol, "Protocol": in.Protocol,
@@ -18,19 +16,13 @@ func generateSessionInfoTableOutput(in SessionInfo) string {
"Connection Limit": in.ConnectionLimit, "Connection Limit": in.ConnectionLimit,
} }
maxLength := 0 maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
for k := range nonAttributeMap {
if len(k) > maxLength {
maxLength = len(k)
}
}
ret = append(ret, "", "Proxy listening information:") ret := []string{
"",
ret = append(ret, "Proxy listening information:",
// We do +2 because there is another +2 offset for host sets below
base.WrapMap(2, maxLength+2, nonAttributeMap), base.WrapMap(2, maxLength+2, nonAttributeMap),
) }
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
+11 -9
View File
@@ -87,14 +87,11 @@ func populateFlags(c *Command, f *base.FlagSet, flagNames []string) {
} }
func generateRoleTableOutput(in *roles.Role) string { func generateRoleTableOutput(in *roles.Role) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
} }
if in.Name != "" { if in.Name != "" {
@@ -107,11 +104,16 @@ func generateRoleTableOutput(in *roles.Role) string {
nonAttributeMap["Grant Scope ID"] = in.GrantScopeId nonAttributeMap["Grant Scope ID"] = in.GrantScopeId
} }
ret = append(ret, "", "Role information:") maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
ret = append(ret, ret := []string{
base.WrapMap(2, 0, nonAttributeMap), "",
) "Role information:",
base.WrapMap(2, maxLength+2, nonAttributeMap),
"",
" Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
if len(in.Principals) > 0 { if len(in.Principals) > 0 {
ret = append(ret, ret = append(ret,
+11 -9
View File
@@ -8,14 +8,11 @@ import (
) )
func generateScopeTableOutput(in *scopes.Scope) string { func generateScopeTableOutput(in *scopes.Scope) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
} }
if in.Name != "" { if in.Name != "" {
@@ -25,11 +22,16 @@ func generateScopeTableOutput(in *scopes.Scope) string {
nonAttributeMap["Description"] = in.Description nonAttributeMap["Description"] = in.Description
} }
ret = append(ret, "", "Scope information:") maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
ret = append(ret, ret := []string{
base.WrapMap(2, 0, nonAttributeMap), "",
) "Scope information:",
base.WrapMap(2, maxLength+2, nonAttributeMap),
"",
" Scope (parent):",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
+15 -18
View File
@@ -12,10 +12,9 @@ func generateSessionTableOutput(in *sessions.Session) string {
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Target ID": in.TargetId, "Target ID": in.TargetId,
"Scope ID": in.Scope.Id, "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Expiration Time": in.ExpirationTime.Local().Format(time.RFC1123),
"Expiration Time": in.ExpirationTime.Local().Format(time.RFC3339),
"Version": in.Version, "Version": in.Version,
"Type": in.Type, "Type": in.Type,
"Auth Token ID": in.AuthTokenId, "Auth Token ID": in.AuthTokenId,
@@ -26,20 +25,15 @@ func generateSessionTableOutput(in *sessions.Session) string {
"Status": in.Status, "Status": in.Status,
} }
maxLength := 0 maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
for k := range nonAttributeMap {
if len(k) > maxLength {
maxLength = len(k)
}
}
var statesMaps []map[string]interface{} var statesMaps []map[string]interface{}
if len(in.States) > 0 { if len(in.States) > 0 {
for _, state := range in.States { for _, state := range in.States {
m := map[string]interface{}{ m := map[string]interface{}{
"Status": state.Status, "Status": state.Status,
"Start Time": state.StartTime.Local().Format(time.RFC3339), "Start Time": state.StartTime.Local().Format(time.RFC1123),
"End Time": state.EndTime.Local().Format(time.RFC3339), "End Time": state.EndTime.Local().Format(time.RFC1123),
} }
statesMaps = append(statesMaps, m) statesMaps = append(statesMaps, m)
} }
@@ -61,16 +55,19 @@ func generateSessionTableOutput(in *sessions.Session) string {
} }
} }
ret := []string{"", "Session information:"} ret := []string{
"",
ret = append(ret, "Session information:",
// We do +2 because there is another +2 offset for host sets below
base.WrapMap(2, maxLength+2, nonAttributeMap), base.WrapMap(2, maxLength+2, nonAttributeMap),
) "",
" Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
if len(in.States) > 0 { if len(in.States) > 0 {
ret = append(ret, ret = append(ret,
fmt.Sprintf(" States: %s", ""), "",
" States:",
) )
for _, m := range statesMaps { for _, m := range statesMaps {
ret = append(ret, ret = append(ret,
+15 -36
View File
@@ -1,7 +1,6 @@
package targets package targets
import ( import (
"fmt"
"time" "time"
"github.com/hashicorp/boundary/api/scopes" "github.com/hashicorp/boundary/api/scopes"
@@ -10,15 +9,12 @@ import (
) )
func generateTargetTableOutput(in *targets.Target) string { func generateTargetTableOutput(in *targets.Target) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Type": in.Type, "Type": in.Type,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
"Session Connection Limit": in.SessionConnectionLimit, "Session Connection Limit": in.SessionConnectionLimit,
"Session Max Seconds": in.SessionMaxSeconds, "Session Max Seconds": in.SessionMaxSeconds,
} }
@@ -30,25 +26,7 @@ func generateTargetTableOutput(in *targets.Target) string {
nonAttributeMap["Description"] = in.Description nonAttributeMap["Description"] = in.Description
} }
maxLength := 0 maxLength := base.MaxAttributesLength(nonAttributeMap, in.Attributes, keySubstMap)
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)
}
}
}
var hostSetMaps []map[string]interface{} var hostSetMaps []map[string]interface{}
if len(in.HostSets) > 0 { if len(in.HostSets) > 0 {
@@ -63,16 +41,20 @@ func generateTargetTableOutput(in *targets.Target) string {
maxLength = l maxLength = l
} }
} }
ret = append(ret, "", "Target information:")
ret = append(ret, ret := []string{
// We do +2 because there is another +2 offset for host sets below "",
"Target information:",
base.WrapMap(2, maxLength+2, nonAttributeMap), base.WrapMap(2, maxLength+2, nonAttributeMap),
) "",
" Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
if len(in.HostSets) > 0 { if len(in.HostSets) > 0 {
ret = append(ret, ret = append(ret,
fmt.Sprintf(" Host Sets: %s", ""), "",
" Host Sets:",
) )
for _, m := range hostSetMaps { for _, m := range hostSetMaps {
ret = append(ret, ret = append(ret,
@@ -83,12 +65,9 @@ func generateTargetTableOutput(in *targets.Target) string {
} }
if len(in.Attributes) > 0 { if len(in.Attributes) > 0 {
if true {
ret = append(ret,
fmt.Sprintf(" Attributes: %s", ""),
)
}
ret = append(ret, ret = append(ret,
"",
" Attributes:",
base.WrapMap(4, maxLength, in.Attributes), base.WrapMap(4, maxLength, in.Attributes),
) )
} }
@@ -125,7 +104,7 @@ func generateAuthorizationTableOutput(in *targets.SessionAuthorization) string {
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }
var attributeMap = map[string]string{ var keySubstMap = map[string]string{
"default_port": "Default Port", "default_port": "Default Port",
} }
+11 -9
View File
@@ -8,14 +8,11 @@ import (
) )
func generateUserTableOutput(in *users.User) string { func generateUserTableOutput(in *users.User) string {
var ret []string
nonAttributeMap := map[string]interface{}{ nonAttributeMap := map[string]interface{}{
"ID": in.Id, "ID": in.Id,
"Scope ID": in.Scope.Id,
"Version": in.Version, "Version": in.Version,
"Created Time": in.CreatedTime.Local().Format(time.RFC3339), "Created Time": in.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339), "Updated Time": in.UpdatedTime.Local().Format(time.RFC1123),
} }
if in.Name != "" { if in.Name != "" {
@@ -25,11 +22,16 @@ func generateUserTableOutput(in *users.User) string {
nonAttributeMap["Description"] = in.Description nonAttributeMap["Description"] = in.Description
} }
ret = append(ret, "", "User information:") maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
ret = append(ret, ret := []string{
base.WrapMap(2, 0, nonAttributeMap), "",
) "User information:",
base.WrapMap(2, maxLength+2, nonAttributeMap),
"",
" Scope:",
base.ScopeInfoForOutput(in.Scope, maxLength),
}
return base.WrapForHelpText(ret) return base.WrapForHelpText(ret)
} }