github.com/hernad/nomad@v1.6.112/command/acl_role_info.go (about)

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