github.com/djenriquez/nomad-1@v0.8.1/command/sentinel_read.go (about)

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