github.com/hernad/nomad@v1.6.112/command/operator_gossip_keyring_list.go (about)

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