github.com/hernad/nomad@v1.6.112/command/acl_role_list.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 ACLRoleListCommand satisfies the cli.Command interface.
    16  var _ cli.Command = &ACLRoleListCommand{}
    17  
    18  // ACLRoleListCommand implements cli.Command.
    19  type ACLRoleListCommand struct {
    20  	Meta
    21  }
    22  
    23  // Help satisfies the cli.Command Help function.
    24  func (a *ACLRoleListCommand) Help() string {
    25  	helpText := `
    26  Usage: nomad acl role list [options]
    27  
    28    List is used to list existing ACL roles.
    29  
    30  General Options:
    31  
    32    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
    33  
    34  ACL List Options:
    35  
    36    -json
    37      Output the ACL roles in a JSON format.
    38  
    39    -t
    40      Format and display the ACL roles using a Go template.
    41  `
    42  
    43  	return strings.TrimSpace(helpText)
    44  }
    45  
    46  func (a *ACLRoleListCommand) AutocompleteFlags() complete.Flags {
    47  	return mergeAutocompleteFlags(a.Meta.AutocompleteFlags(FlagSetClient),
    48  		complete.Flags{
    49  			"-json": complete.PredictNothing,
    50  			"-t":    complete.PredictAnything,
    51  		})
    52  }
    53  
    54  func (a *ACLRoleListCommand) AutocompleteArgs() complete.Predictor { return complete.PredictNothing }
    55  
    56  // Synopsis satisfies the cli.Command Synopsis function.
    57  func (a *ACLRoleListCommand) Synopsis() string { return "List ACL roles" }
    58  
    59  // Name returns the name of this command.
    60  func (a *ACLRoleListCommand) Name() string { return "acl role list" }
    61  
    62  // Run satisfies the cli.Command Run function.
    63  func (a *ACLRoleListCommand) Run(args []string) int {
    64  	var json bool
    65  	var tmpl string
    66  
    67  	flags := a.Meta.FlagSet(a.Name(), FlagSetClient)
    68  	flags.Usage = func() { a.Ui.Output(a.Help()) }
    69  	flags.BoolVar(&json, "json", false, "")
    70  	flags.StringVar(&tmpl, "t", "", "")
    71  
    72  	if err := flags.Parse(args); err != nil {
    73  		return 1
    74  	}
    75  
    76  	// Check that we got no arguments
    77  	if len(flags.Args()) != 0 {
    78  		a.Ui.Error("This command takes no arguments")
    79  		a.Ui.Error(commandErrorText(a))
    80  		return 1
    81  	}
    82  
    83  	// Get the HTTP client
    84  	client, err := a.Meta.Client()
    85  	if err != nil {
    86  		a.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    87  		return 1
    88  	}
    89  
    90  	// Fetch info on the policy
    91  	roles, _, err := client.ACLRoles().List(nil)
    92  	if err != nil {
    93  		a.Ui.Error(fmt.Sprintf("Error listing ACL roles: %s", err))
    94  		return 1
    95  	}
    96  
    97  	if json || len(tmpl) > 0 {
    98  		out, err := Format(json, tmpl, roles)
    99  		if err != nil {
   100  			a.Ui.Error(err.Error())
   101  			return 1
   102  		}
   103  
   104  		a.Ui.Output(out)
   105  		return 0
   106  	}
   107  
   108  	a.Ui.Output(formatACLRoles(roles))
   109  	return 0
   110  }
   111  
   112  func formatACLRoles(roles []*api.ACLRoleListStub) string {
   113  	if len(roles) == 0 {
   114  		return "No ACL roles found"
   115  	}
   116  
   117  	output := make([]string, 0, len(roles)+1)
   118  	output = append(output, "ID|Name|Description|Policies")
   119  	for _, role := range roles {
   120  		output = append(output, fmt.Sprintf(
   121  			"%s|%s|%s|%s",
   122  			role.ID, role.Name, role.Description, strings.Join(aclRolePolicyLinkToStringList(role.Policies), ",")))
   123  	}
   124  
   125  	return formatList(output)
   126  }