-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
acl_role_info.go
121 lines (93 loc) · 2.9 KB
/
acl_role_info.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
package command
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
// Ensure ACLRoleInfoCommand satisfies the cli.Command interface.
var _ cli.Command = &ACLRoleInfoCommand{}
// ACLRoleInfoCommand implements cli.Command.
type ACLRoleInfoCommand struct {
Meta
byName bool
json bool
tmpl string
}
// Help satisfies the cli.Command Help function.
func (a *ACLRoleInfoCommand) Help() string {
helpText := `
Usage: nomad acl role info [options] <acl_role_id>
Info is used to fetch information on an existing ACL roles. Requires a
management token.
General Options:
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
ACL Info Options:
-by-name
Look up the ACL role using its name as the identifier. The command defaults
to expecting the ACL ID as the argument.
-json
Output the ACL role in a JSON format.
-t
Format and display the ACL role using a Go template.
`
return strings.TrimSpace(helpText)
}
func (a *ACLRoleInfoCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(a.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-by-name": complete.PredictNothing,
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
})
}
func (a *ACLRoleInfoCommand) AutocompleteArgs() complete.Predictor { return complete.PredictNothing }
// Synopsis satisfies the cli.Command Synopsis function.
func (a *ACLRoleInfoCommand) Synopsis() string { return "Fetch information on an existing ACL role" }
// Name returns the name of this command.
func (a *ACLRoleInfoCommand) Name() string { return "acl role info" }
// Run satisfies the cli.Command Run function.
func (a *ACLRoleInfoCommand) Run(args []string) int {
flags := a.Meta.FlagSet(a.Name(), FlagSetClient)
flags.Usage = func() { a.Ui.Output(a.Help()) }
flags.BoolVar(&a.byName, "by-name", false, "")
flags.BoolVar(&a.json, "json", false, "")
flags.StringVar(&a.tmpl, "t", "", "")
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we have exactly one argument.
if len(flags.Args()) != 1 {
a.Ui.Error("This command takes one argument: <acl_role_id>")
a.Ui.Error(commandErrorText(a))
return 1
}
// Get the HTTP client.
client, err := a.Meta.Client()
if err != nil {
a.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
var (
aclRole *api.ACLRole
apiErr error
)
aclRoleID := flags.Args()[0]
// Use the correct API call depending on whether the lookup is by the name
// or the ID.
switch a.byName {
case true:
aclRole, _, apiErr = client.ACLRoles().GetByName(aclRoleID, nil)
default:
aclRole, _, apiErr = client.ACLRoles().Get(aclRoleID, nil)
}
// Handle any error from the API.
if apiErr != nil {
a.Ui.Error(fmt.Sprintf("Error reading ACL role: %s", apiErr))
return 1
}
// Format the output.
a.Ui.Output(formatACLRole(aclRole))
return 0
}