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

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/mitchellh/cli"
     9  	"github.com/posener/complete"
    10  )
    11  
    12  // OperatorGossipKeyringListCommand is a Command implementation
    13  // that handles removing a gossip encryption key from a keyring
    14  type OperatorGossipKeyringListCommand struct {
    15  	Meta
    16  }
    17  
    18  func (c *OperatorGossipKeyringListCommand) Help() string {
    19  	helpText := `
    20  Usage: nomad operator gossip keyring list [options]
    21  
    22    List all gossip keys currently in use within the cluster.
    23  
    24    This command can only be run against server nodes. It returns 0 if all nodes
    25    reply and there are no errors. If any node fails to reply or reports failure,
    26    the exit code will be 1.
    27  
    28    If ACLs are enabled, this command requires a token with the 'agent:write'
    29    capability.
    30  
    31  General Options:
    32  
    33    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
    34  
    35  	return strings.TrimSpace(helpText)
    36  }
    37  
    38  func (c *OperatorGossipKeyringListCommand) Synopsis() string {
    39  	return "List gossip encryption keys"
    40  }
    41  
    42  func (c *OperatorGossipKeyringListCommand) AutocompleteFlags() complete.Flags {
    43  	return c.Meta.AutocompleteFlags(FlagSetClient)
    44  }
    45  
    46  func (c *OperatorGossipKeyringListCommand) AutocompleteArgs() complete.Predictor {
    47  	return complete.PredictAnything
    48  }
    49  
    50  func (c *OperatorGossipKeyringListCommand) Name() string { return "operator gossip keyring list" }
    51  
    52  func (c *OperatorGossipKeyringListCommand) Run(args []string) int {
    53  	flags := c.Meta.FlagSet("operator-gossip-keyring-list", FlagSetClient)
    54  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    55  	if err := flags.Parse(args); err != nil {
    56  		return 1
    57  	}
    58  
    59  	c.Ui = &cli.PrefixedUi{
    60  		OutputPrefix: "",
    61  		InfoPrefix:   "==> ",
    62  		ErrorPrefix:  "",
    63  		Ui:           c.Ui,
    64  	}
    65  
    66  	args = flags.Args()
    67  	if len(args) != 0 {
    68  		c.Ui.Error("This command requires no arguments")
    69  		c.Ui.Error(commandErrorText(c))
    70  		return 1
    71  	}
    72  
    73  	client, err := c.Meta.Client()
    74  	if err != nil {
    75  		c.Ui.Error(fmt.Sprintf("Error creating nomad cli client: %s", err))
    76  		return 1
    77  	}
    78  
    79  	c.Ui.Output("Gathering installed encryption keys...")
    80  	r, err := client.Agent().ListKeys()
    81  	if err != nil {
    82  		c.Ui.Error(fmt.Sprintf("error: %s", err))
    83  		return 1
    84  	}
    85  	c.handleKeyResponse(r)
    86  	return 0
    87  }
    88  
    89  func (c *OperatorGossipKeyringListCommand) handleKeyResponse(resp *api.KeyringResponse) {
    90  	out := make([]string, len(resp.Keys)+1)
    91  	out[0] = "Key"
    92  	i := 1
    93  	for k := range resp.Keys {
    94  		out[i] = k
    95  		i = i + 1
    96  	}
    97  	c.Ui.Output(formatList(out))
    98  }