open-vault/command/capabilities.go

84 lines
1.8 KiB
Go
Raw Normal View History

2016-03-02 18:42:32 +00:00
package command
import (
"fmt"
"strings"
2016-03-03 16:08:27 +00:00
"github.com/hashicorp/vault/api"
2016-03-02 18:42:32 +00:00
)
// CapabilitiesCommand is a Command that enables a new endpoint.
type CapabilitiesCommand struct {
Meta
}
func (c *CapabilitiesCommand) Run(args []string) int {
flags := c.Meta.FlagSet("capabilities", FlagSetDefault)
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) > 2 {
flags.Usage()
c.Ui.Error(fmt.Sprintf(
"\ncapabilities expects at most two arguments"))
return 1
}
var token string
var path string
switch len(args) {
case 1:
// only path is provided
path = args[0]
case 2:
// both token and path are provided
token = args[0]
path = args[1]
default:
}
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
2016-03-03 16:54:14 +00:00
var resp *api.CapabilitiesResponse
2016-03-02 18:42:32 +00:00
if token == "" {
2016-03-03 16:54:14 +00:00
resp, err = client.Sys().CapabilitiesSelf(path)
2016-03-02 18:42:32 +00:00
} else {
2016-03-03 16:54:14 +00:00
resp, err = client.Sys().Capabilities(token, path)
2016-03-02 18:42:32 +00:00
}
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error retrieving capabilities: %s", err))
return 1
}
2016-03-03 16:54:14 +00:00
c.Ui.Output(fmt.Sprintf("Capabilities: %s", resp.Capabilities))
2016-03-02 18:42:32 +00:00
return 0
}
func (c *CapabilitiesCommand) Synopsis() string {
return "Fetch the capabilities of a given token on a given path"
}
func (c *CapabilitiesCommand) Help() string {
helpText := `
Usage: vault capabilities [options] [token] path
Fetch the capabilities of a token on a given path.
2016-03-03 16:54:14 +00:00
If a token is provided to the command, API '/sys/capabilities' will be invoked
with the given token; otherwise API '/sys/capabilities-self' will be invoked with
the client token.
2016-03-02 18:42:32 +00:00
General Options:
` + generalOptionsUsage()
return strings.TrimSpace(helpText)
}