github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/acl_role_info.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/nomad/api" 8 "github.com/mitchellh/cli" 9 "github.com/posener/complete" 10 ) 11 12 // Ensure ACLRoleInfoCommand satisfies the cli.Command interface. 13 var _ cli.Command = &ACLRoleInfoCommand{} 14 15 // ACLRoleInfoCommand implements cli.Command. 16 type ACLRoleInfoCommand struct { 17 Meta 18 19 byName bool 20 json bool 21 tmpl string 22 } 23 24 // Help satisfies the cli.Command Help function. 25 func (a *ACLRoleInfoCommand) Help() string { 26 helpText := ` 27 Usage: nomad acl role info [options] <acl_role_id> 28 29 Info is used to fetch information on an existing ACL roles. Requires a 30 management token. 31 32 General Options: 33 34 ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + ` 35 36 ACL Info Options: 37 38 -by-name 39 Look up the ACL role using its name as the identifier. The command defaults 40 to expecting the ACL ID as the argument. 41 42 -json 43 Output the ACL role in a JSON format. 44 45 -t 46 Format and display the ACL role using a Go template. 47 ` 48 49 return strings.TrimSpace(helpText) 50 } 51 52 func (a *ACLRoleInfoCommand) AutocompleteFlags() complete.Flags { 53 return mergeAutocompleteFlags(a.Meta.AutocompleteFlags(FlagSetClient), 54 complete.Flags{ 55 "-by-name": complete.PredictNothing, 56 "-json": complete.PredictNothing, 57 "-t": complete.PredictAnything, 58 }) 59 } 60 61 func (a *ACLRoleInfoCommand) AutocompleteArgs() complete.Predictor { return complete.PredictNothing } 62 63 // Synopsis satisfies the cli.Command Synopsis function. 64 func (a *ACLRoleInfoCommand) Synopsis() string { return "Fetch information on an existing ACL role" } 65 66 // Name returns the name of this command. 67 func (a *ACLRoleInfoCommand) Name() string { return "acl role info" } 68 69 // Run satisfies the cli.Command Run function. 70 func (a *ACLRoleInfoCommand) Run(args []string) int { 71 72 flags := a.Meta.FlagSet(a.Name(), FlagSetClient) 73 flags.Usage = func() { a.Ui.Output(a.Help()) } 74 flags.BoolVar(&a.byName, "by-name", false, "") 75 flags.BoolVar(&a.json, "json", false, "") 76 flags.StringVar(&a.tmpl, "t", "", "") 77 78 if err := flags.Parse(args); err != nil { 79 return 1 80 } 81 82 // Check that we have exactly one argument. 83 if len(flags.Args()) != 1 { 84 a.Ui.Error("This command takes one argument: <acl_role_id>") 85 a.Ui.Error(commandErrorText(a)) 86 return 1 87 } 88 89 // Get the HTTP client. 90 client, err := a.Meta.Client() 91 if err != nil { 92 a.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 93 return 1 94 } 95 96 var ( 97 aclRole *api.ACLRole 98 apiErr error 99 ) 100 101 aclRoleID := flags.Args()[0] 102 103 // Use the correct API call depending on whether the lookup is by the name 104 // or the ID. 105 switch a.byName { 106 case true: 107 aclRole, _, apiErr = client.ACLRoles().GetByName(aclRoleID, nil) 108 default: 109 aclRole, _, apiErr = client.ACLRoles().Get(aclRoleID, nil) 110 } 111 112 // Handle any error from the API. 113 if apiErr != nil { 114 a.Ui.Error(fmt.Sprintf("Error reading ACL role: %s", apiErr)) 115 return 1 116 } 117 118 // Format the output. 119 a.Ui.Output(formatACLRole(aclRole)) 120 return 0 121 }