-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathfuncs.go
156 lines (140 loc) · 4.03 KB
/
funcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package authtokenscmd
import (
"errors"
"fmt"
"time"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/authtokens"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/go-secure-stdlib/strutil"
)
const selfFlag = "self"
func init() {
extraFlagsHandlingFunc = extraFlagsHandlingFuncImpl
}
func extraFlagsHandlingFuncImpl(c *Command, _ *base.FlagSets, _ *[]authtokens.Option) bool {
if c.Func != "delete" && c.Func != "read" {
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
c.PrintCliError(errors.New("ID is required but not passed in via -id"))
return false
}
return true
}
if c.FlagId == "" {
fmt.Printf("No ID provided; %s the stored token? (Pass an ID of %q to suppress this question.) y/n: ", c.Func, selfFlag)
var yesNo string
fmt.Scanf("%s", &yesNo)
switch yesNo {
case "y", "Y":
c.FlagId = selfFlag
default:
c.PrintCliError(errors.New(`"Y" or "y" not provided, refusing to continue`))
return false
}
}
if c.FlagId == selfFlag {
// We should have already read this and have it cached
client, err := c.Client()
if err != nil {
c.PrintCliError(fmt.Errorf("Error reading cached API client: %w", err))
return false
}
c.FlagId, err = base.TokenIdFromToken(client.Token())
if err != nil {
c.PrintCliError(err)
return false
}
}
return true
}
func (c *Command) printListTable(items []*authtokens.AuthToken) string {
if len(items) == 0 {
return "No auth tokens found"
}
var output []string
output = []string{
"",
"Auth Token information:",
}
for i, t := range items {
if i > 0 {
output = append(output, "")
}
if t.Id != "" {
output = append(output,
fmt.Sprintf(" ID: %s", t.Id),
)
}
if c.FlagRecursive && t.ScopeId != "" {
output = append(output,
fmt.Sprintf(" Scope ID: %s", t.ScopeId),
)
}
if !t.ApproximateLastUsedTime.IsZero() {
output = append(output,
fmt.Sprintf(" Approximate Last Used Time: %s", t.ApproximateLastUsedTime.Local().Format(time.RFC1123)),
)
}
if t.AuthMethodId != "" {
output = append(output,
fmt.Sprintf(" Auth Method ID: %s", t.AuthMethodId),
)
}
if !t.CreatedTime.IsZero() {
output = append(output,
fmt.Sprintf(" Created Time: %s", t.CreatedTime.Local().Format(time.RFC1123)),
)
}
if !t.ExpirationTime.IsZero() {
output = append(output,
fmt.Sprintf(" Expiration Time: %s", t.ExpirationTime.Local().Format(time.RFC1123)),
)
}
if !t.UpdatedTime.IsZero() {
output = append(output,
fmt.Sprintf(" Updated Time: %s", t.UpdatedTime.Local().Format(time.RFC1123)),
)
}
if t.UserId != "" {
output = append(output,
fmt.Sprintf(" User ID: %s", t.UserId),
)
}
if len(t.AuthorizedActions) > 0 {
output = append(output,
" Authorized Actions:",
base.WrapSlice(6, t.AuthorizedActions),
)
}
}
return base.WrapForHelpText(output)
}
func printItemTable(result api.GenericResult) string {
item := result.GetItem().(*authtokens.AuthToken)
nonAttributeMap := map[string]interface{}{
"ID": item.Id,
"Auth Method ID": item.AuthMethodId,
"User ID": item.UserId,
"Created Time": item.CreatedTime.Local().Format(time.RFC1123),
"Updated Time": item.UpdatedTime.Local().Format(time.RFC1123),
"Expiration Time": item.ExpirationTime.Local().Format(time.RFC1123),
"Approximate Last Used Time": item.ApproximateLastUsedTime.Local().Format(time.RFC1123),
}
maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
ret := []string{
"",
"Auth Token information:",
base.WrapMap(2, maxLength+2, nonAttributeMap),
"",
" Scope:",
base.ScopeInfoForOutput(item.Scope, maxLength),
}
if len(item.AuthorizedActions) > 0 {
ret = append(ret,
"",
" Authorized Actions:",
base.WrapSlice(4, item.AuthorizedActions),
)
}
return base.WrapForHelpText(ret)
}