github.com/hernad/nomad@v1.6.112/command/sentinel_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/posener/complete"
    11  )
    12  
    13  type SentinelListCommand struct {
    14  	Meta
    15  }
    16  
    17  func (c *SentinelListCommand) Help() string {
    18  	helpText := `
    19  Usage: nomad sentinel list [options]
    20  
    21    List is used to display all the installed Sentinel policies.
    22  
    23    Sentinel commands are only available when ACLs are enabled. This command
    24    requires a management token.
    25  
    26  General Options:
    27  
    28    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
    29  
    30  `
    31  	return strings.TrimSpace(helpText)
    32  }
    33  
    34  func (c *SentinelListCommand) AutocompleteFlags() complete.Flags {
    35  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    36  		complete.Flags{})
    37  }
    38  
    39  func (c *SentinelListCommand) AutocompleteArgs() complete.Predictor {
    40  	return complete.PredictNothing
    41  }
    42  
    43  func (c *SentinelListCommand) Synopsis() string {
    44  	return "Display all Sentinel policies"
    45  }
    46  
    47  func (c *SentinelListCommand) Name() string { return "sentinel list" }
    48  
    49  func (c *SentinelListCommand) Run(args []string) int {
    50  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    51  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    52  	if err := flags.Parse(args); err != nil {
    53  		return 1
    54  	}
    55  
    56  	if args = flags.Args(); len(args) > 0 {
    57  		c.Ui.Error("This command takes no arguments")
    58  		c.Ui.Error(commandErrorText(c))
    59  	}
    60  	// Get the HTTP client
    61  	client, err := c.Meta.Client()
    62  	if err != nil {
    63  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    64  		return 1
    65  	}
    66  
    67  	// Get the list of policies
    68  	policies, _, err := client.SentinelPolicies().List(nil)
    69  	if err != nil {
    70  		c.Ui.Error(fmt.Sprintf("Error listing Sentinel policies: %s", err))
    71  		return 1
    72  	}
    73  
    74  	if len(policies) == 0 {
    75  		c.Ui.Output("No policies found")
    76  		return 0
    77  	}
    78  
    79  	out := []string{}
    80  	out = append(out, "Name|Scope|Enforcement Level|Description")
    81  	for _, p := range policies {
    82  		line := fmt.Sprintf("%s|%s|%s|%s", p.Name, p.Scope, p.EnforcementLevel, p.Description)
    83  		out = append(out, line)
    84  	}
    85  	c.Ui.Output(formatList(out))
    86  	return 0
    87  }