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