github.com/hernad/nomad@v1.6.112/command/sentinel_read.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 SentinelReadCommand struct {
    14  	Meta
    15  }
    16  
    17  func (c *SentinelReadCommand) Help() string {
    18  	helpText := `
    19  Usage: nomad sentinel read [options] <name>
    20  
    21    Read is used to inspect a Sentinel policy.
    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  Read Options:
    31  
    32    -raw
    33      Prints only the raw policy
    34  
    35  `
    36  	return strings.TrimSpace(helpText)
    37  }
    38  
    39  func (c *SentinelReadCommand) AutocompleteFlags() complete.Flags {
    40  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    41  		complete.Flags{
    42  			"-raw": complete.PredictNothing,
    43  		})
    44  }
    45  
    46  func (c *SentinelReadCommand) AutocompleteArgs() complete.Predictor {
    47  	return complete.PredictNothing
    48  }
    49  
    50  func (c *SentinelReadCommand) Synopsis() string {
    51  	return "Inspects an existing Sentinel policies"
    52  }
    53  
    54  func (c *SentinelReadCommand) Name() string { return "sentinel read" }
    55  
    56  func (c *SentinelReadCommand) Run(args []string) int {
    57  	var raw bool
    58  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    59  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    60  	flags.BoolVar(&raw, "raw", false, "")
    61  	if err := flags.Parse(args); err != nil {
    62  		return 1
    63  	}
    64  
    65  	// Check that we got exactly one arguments
    66  	args = flags.Args()
    67  	if l := len(args); l != 1 {
    68  		c.Ui.Error("This command takes one argument: <name>")
    69  		c.Ui.Error(commandErrorText(c))
    70  		return 1
    71  	}
    72  
    73  	// Get the name and file
    74  	policyName := args[0]
    75  
    76  	// Get the HTTP client
    77  	client, err := c.Meta.Client()
    78  	if err != nil {
    79  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    80  		return 1
    81  	}
    82  
    83  	// Query the policy
    84  	policy, _, err := client.SentinelPolicies().Info(policyName, nil)
    85  	if err != nil {
    86  		c.Ui.Error(fmt.Sprintf("Error querying Sentinel policy: %s", err))
    87  		return 1
    88  	}
    89  
    90  	// Check for only the raw policy
    91  	if raw {
    92  		c.Ui.Output(policy.Policy)
    93  		return 0
    94  	}
    95  
    96  	// Output the base information
    97  	info := []string{
    98  		fmt.Sprintf("Name|%s", policy.Name),
    99  		fmt.Sprintf("Scope|%s", policy.Scope),
   100  		fmt.Sprintf("Enforcement Level|%s", policy.EnforcementLevel),
   101  		fmt.Sprintf("Description|%s", policy.Description),
   102  	}
   103  	c.Ui.Output(formatKV(info))
   104  	c.Ui.Output("Policy:")
   105  	c.Ui.Output(policy.Policy)
   106  	return 0
   107  }