github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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  General Options:
    21  
    22    ` + generalOptionsUsage()
    23  
    24  	return strings.TrimSpace(helpText)
    25  }
    26  
    27  func (c *ACLPolicyInfoCommand) AutocompleteFlags() complete.Flags {
    28  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    29  		complete.Flags{})
    30  }
    31  
    32  func (c *ACLPolicyInfoCommand) AutocompleteArgs() complete.Predictor {
    33  	return complete.PredictNothing
    34  }
    35  
    36  func (c *ACLPolicyInfoCommand) Synopsis() string {
    37  	return "Fetch info on an existing ACL policy"
    38  }
    39  
    40  func (c *ACLPolicyInfoCommand) Run(args []string) int {
    41  	flags := c.Meta.FlagSet("acl policy info", FlagSetClient)
    42  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    43  	if err := flags.Parse(args); err != nil {
    44  		return 1
    45  	}
    46  
    47  	// Check that we got exactly one argument
    48  	args = flags.Args()
    49  	if l := len(args); l != 1 {
    50  		c.Ui.Error(c.Help())
    51  		return 1
    52  	}
    53  
    54  	// Get the policy name
    55  	policyName := args[0]
    56  
    57  	// Get the HTTP client
    58  	client, err := c.Meta.Client()
    59  	if err != nil {
    60  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    61  		return 1
    62  	}
    63  
    64  	// Fetch info on the policy
    65  	policy, _, err := client.ACLPolicies().Info(policyName, nil)
    66  	if err != nil {
    67  		c.Ui.Error(fmt.Sprintf("Error fetching info on ACL policy: %s", err))
    68  		return 1
    69  	}
    70  
    71  	c.Ui.Output(formatKVPolicy(policy))
    72  	return 0
    73  }