github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/acl_policy_info.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/posener/complete"
     8  )
     9  
    10  type ACLPolicyInfoCommand struct {
    11  	Meta
    12  }
    13  
    14  func (c *ACLPolicyInfoCommand) Help() string {
    15  	helpText := `
    16  Usage: nomad acl policy info <name>
    17  
    18    Info is used to fetch information on an existing ACL policy.
    19  
    20    This command requires a management ACL token or a token that has the
    21    associated policy.
    22  
    23  General Options:
    24  
    25    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
    26  
    27  	return strings.TrimSpace(helpText)
    28  }
    29  
    30  func (c *ACLPolicyInfoCommand) AutocompleteFlags() complete.Flags {
    31  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    32  		complete.Flags{})
    33  }
    34  
    35  func (c *ACLPolicyInfoCommand) AutocompleteArgs() complete.Predictor {
    36  	return complete.PredictNothing
    37  }
    38  
    39  func (c *ACLPolicyInfoCommand) Synopsis() string {
    40  	return "Fetch info on an existing ACL policy"
    41  }
    42  
    43  func (c *ACLPolicyInfoCommand) Name() string { return "acl policy info" }
    44  
    45  func (c *ACLPolicyInfoCommand) Run(args []string) int {
    46  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    47  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    48  	if err := flags.Parse(args); err != nil {
    49  		return 1
    50  	}
    51  
    52  	// Check that we got exactly one argument
    53  	args = flags.Args()
    54  	if l := len(args); l != 1 {
    55  		c.Ui.Error("This command takes one argument: <name>")
    56  		c.Ui.Error(commandErrorText(c))
    57  		return 1
    58  	}
    59  
    60  	// Get the policy name
    61  	policyName := args[0]
    62  
    63  	// Get the HTTP client
    64  	client, err := c.Meta.Client()
    65  	if err != nil {
    66  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    67  		return 1
    68  	}
    69  
    70  	// Fetch info on the policy
    71  	policy, _, err := client.ACLPolicies().Info(policyName, nil)
    72  	if err != nil {
    73  		c.Ui.Error(fmt.Sprintf("Error fetching info on ACL policy: %s", err))
    74  		return 1
    75  	}
    76  
    77  	c.Ui.Output(c.Colorize().Color(formatACLPolicy(policy)))
    78  	return 0
    79  }