github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/acl_policy_list.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/posener/complete"
     9  )
    10  
    11  type ACLPolicyListCommand struct {
    12  	Meta
    13  }
    14  
    15  func (c *ACLPolicyListCommand) Help() string {
    16  	helpText := `
    17  Usage: nomad acl policy list
    18  
    19    List is used to list available ACL policies.
    20  
    21    This command requires a management ACL token to view all policies. A
    22    non-management token can query its own policies.
    23  
    24  General Options:
    25  
    26    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
    27  
    28  List Options:
    29  
    30    -json
    31      Output the ACL policies in a JSON format.
    32  
    33    -t
    34      Format and display the ACL policies using a Go template.
    35  `
    36  
    37  	return strings.TrimSpace(helpText)
    38  }
    39  
    40  func (c *ACLPolicyListCommand) AutocompleteFlags() complete.Flags {
    41  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    42  		complete.Flags{
    43  			"-json": complete.PredictNothing,
    44  			"-t":    complete.PredictAnything,
    45  		})
    46  }
    47  
    48  func (c *ACLPolicyListCommand) AutocompleteArgs() complete.Predictor {
    49  	return complete.PredictNothing
    50  }
    51  
    52  func (c *ACLPolicyListCommand) Synopsis() string {
    53  	return "List ACL policies"
    54  }
    55  
    56  func (c *ACLPolicyListCommand) Name() string { return "acl policy list" }
    57  
    58  func (c *ACLPolicyListCommand) Run(args []string) int {
    59  	var json bool
    60  	var tmpl string
    61  
    62  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    63  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    64  	flags.BoolVar(&json, "json", false, "")
    65  	flags.StringVar(&tmpl, "t", "", "")
    66  
    67  	if err := flags.Parse(args); err != nil {
    68  		return 1
    69  	}
    70  
    71  	// Check that we got no arguments
    72  	args = flags.Args()
    73  	if l := len(args); l != 0 {
    74  		c.Ui.Error("This command takes no arguments")
    75  		c.Ui.Error(commandErrorText(c))
    76  		return 1
    77  	}
    78  
    79  	// Get the HTTP client
    80  	client, err := c.Meta.Client()
    81  	if err != nil {
    82  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    83  		return 1
    84  	}
    85  
    86  	// Fetch info on the policy
    87  	policies, _, err := client.ACLPolicies().List(nil)
    88  	if err != nil {
    89  		c.Ui.Error(fmt.Sprintf("Error listing ACL policies: %s", err))
    90  		return 1
    91  	}
    92  
    93  	if json || len(tmpl) > 0 {
    94  		out, err := Format(json, tmpl, policies)
    95  		if err != nil {
    96  			c.Ui.Error(err.Error())
    97  			return 1
    98  		}
    99  
   100  		c.Ui.Output(out)
   101  		return 0
   102  	}
   103  
   104  	c.Ui.Output(formatPolicies(policies))
   105  	return 0
   106  }
   107  
   108  func formatPolicies(policies []*api.ACLPolicyListStub) string {
   109  	if len(policies) == 0 {
   110  		return "No policies found"
   111  	}
   112  
   113  	output := make([]string, 0, len(policies)+1)
   114  	output = append(output, "Name|Description")
   115  	for _, p := range policies {
   116  		output = append(output, fmt.Sprintf("%s|%s", p.Name, p.Description))
   117  	}
   118  
   119  	return formatList(output)
   120  }