github.com/manicqin/nomad@v0.9.5/command/acl_token_self.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/posener/complete"
     8  )
     9  
    10  type ACLTokenSelfCommand struct {
    11  	Meta
    12  }
    13  
    14  func (c *ACLTokenSelfCommand) Help() string {
    15  	helpText := `
    16  Usage: nomad acl token self
    17  
    18    Self is used to fetch information about the currently set ACL token.
    19  
    20  General Options:
    21  
    22    ` + generalOptionsUsage()
    23  
    24  	return strings.TrimSpace(helpText)
    25  }
    26  
    27  func (c *ACLTokenSelfCommand) AutocompleteFlags() complete.Flags {
    28  	return c.Meta.AutocompleteFlags(FlagSetClient)
    29  }
    30  
    31  func (c *ACLTokenSelfCommand) AutocompleteArgs() complete.Predictor {
    32  	return complete.PredictNothing
    33  }
    34  
    35  func (c *ACLTokenSelfCommand) Synopsis() string {
    36  	return "Lookup self ACL token"
    37  }
    38  
    39  func (c *ACLTokenSelfCommand) Name() string { return "acl token self" }
    40  
    41  func (c *ACLTokenSelfCommand) Run(args []string) int {
    42  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    43  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    44  	if err := flags.Parse(args); err != nil {
    45  		return 1
    46  	}
    47  
    48  	// Check that we have no arguments
    49  	args = flags.Args()
    50  	if l := len(args); l != 0 {
    51  		c.Ui.Error("This command takes no arguments")
    52  		c.Ui.Error(commandErrorText(c))
    53  		return 1
    54  	}
    55  
    56  	// Get the HTTP client
    57  	client, err := c.Meta.Client()
    58  	if err != nil {
    59  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    60  		return 1
    61  	}
    62  
    63  	// Get the specified token information
    64  	token, _, err := client.ACLTokens().Self(nil)
    65  	if err != nil {
    66  		c.Ui.Error(fmt.Sprintf("Error fetching self token: %s", err))
    67  		return 1
    68  	}
    69  
    70  	// Format the output
    71  	c.Ui.Output(formatKVACLToken(token))
    72  	return 0
    73  }