open-nomad/command/sentinel_read.go

99 lines
2 KiB
Go
Raw Normal View History

2017-09-19 14:47:10 +00:00
package command
import (
"fmt"
"strings"
"github.com/posener/complete"
)
type SentinelReadCommand struct {
Meta
}
func (c *SentinelReadCommand) Help() string {
helpText := `
Usage: nomad sentinel read [options] <name>
2017-10-13 21:36:02 +00:00
Read is used to inspect a Sentinel policy.
2017-09-19 14:47:10 +00:00
General Options:
` + generalOptionsUsage() + `
Read Options:
-raw
Prints only the raw policy
`
return strings.TrimSpace(helpText)
}
func (c *SentinelReadCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-raw": complete.PredictNothing,
})
}
func (c *SentinelReadCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (c *SentinelReadCommand) Synopsis() string {
return "Inspects an existing Sentinel policies"
}
func (c *SentinelReadCommand) Run(args []string) int {
var raw bool
flags := c.Meta.FlagSet("sentinel read", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&raw, "raw", false, "")
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got exactly one arguments
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error(c.Help())
return 1
}
// Get the name and file
policyName := args[0]
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
// Query the policy
policy, _, err := client.SentinelPolicies().Info(policyName, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying Sentinel policy: %s", err))
return 1
}
// Check for only the raw policy
if raw {
c.Ui.Output(policy.Policy)
return 0
}
// Output the base information
info := []string{
fmt.Sprintf("Name|%s", policy.Name),
fmt.Sprintf("Scope|%s", policy.Scope),
fmt.Sprintf("Enforcement Level|%s", policy.EnforcementLevel),
fmt.Sprintf("Description|%s", policy.Description),
}
c.Ui.Output(formatKV(info))
c.Ui.Output("Policy:")
c.Ui.Output(policy.Policy)
return 0
}