open-vault/command/policy_list.go

94 lines
1.8 KiB
Go
Raw Normal View History

2015-04-02 01:45:11 +00:00
package command
import (
"fmt"
"strings"
2016-04-01 17:16:05 +00:00
"github.com/hashicorp/vault/meta"
2015-04-02 01:45:11 +00:00
)
// PolicyListCommand is a Command that enables a new endpoint.
type PolicyListCommand struct {
2016-04-01 17:16:05 +00:00
meta.Meta
2015-04-02 01:45:11 +00:00
}
func (c *PolicyListCommand) Run(args []string) int {
2016-04-01 17:16:05 +00:00
flags := c.Meta.FlagSet("policy-list", meta.FlagSetDefault)
2015-04-02 01:45:11 +00:00
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
2015-04-02 01:50:43 +00:00
if len(args) == 1 {
return c.read(args[0])
} else if len(args) == 0 {
return c.list()
} else {
2015-04-02 01:45:11 +00:00
flags.Usage()
c.Ui.Error(fmt.Sprintf(
"\npolicies expects zero or one arguments"))
2015-04-02 01:45:11 +00:00
return 1
}
2015-04-02 01:50:43 +00:00
}
2015-04-02 01:45:11 +00:00
2015-04-02 01:50:43 +00:00
func (c *PolicyListCommand) list() int {
2015-04-02 01:45:11 +00:00
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
policies, err := client.Sys().ListPolicies()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error: %s", err))
return 1
}
for _, p := range policies {
c.Ui.Output(p)
}
return 0
}
2015-04-02 01:50:43 +00:00
func (c *PolicyListCommand) read(n string) int {
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
rules, err := client.Sys().GetPolicy(n)
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error: %s", err))
return 1
}
c.Ui.Output(rules)
return 0
}
2015-04-02 01:45:11 +00:00
func (c *PolicyListCommand) Synopsis() string {
return "List the policies on the server"
}
func (c *PolicyListCommand) Help() string {
helpText := `
Usage: vault policies [options] [name]
2015-04-02 01:45:11 +00:00
2015-04-02 01:50:43 +00:00
List the policies that are available or read a single policy.
2015-04-02 01:45:11 +00:00
This command lists the policies that are written to the Vault server.
2015-04-02 01:50:43 +00:00
If a name of a policy is specified, that policy is outputted.
2015-04-02 01:45:11 +00:00
General Options:
2016-04-01 17:16:05 +00:00
` + meta.GeneralOptionsUsage()
2015-04-02 01:45:11 +00:00
return strings.TrimSpace(helpText)
}