2015-04-27 21:55:29 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-02-12 18:21:42 +00:00
|
|
|
"errors"
|
2015-04-27 21:55:29 +00:00
|
|
|
"fmt"
|
2015-12-10 09:01:22 +00:00
|
|
|
"sort"
|
2015-05-02 20:11:40 +00:00
|
|
|
"strconv"
|
2015-12-10 10:32:31 +00:00
|
|
|
"strings"
|
2015-04-27 21:55:29 +00:00
|
|
|
|
2015-12-10 10:32:31 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2015-04-27 21:55:29 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
|
|
|
func OutputSecret(ui cli.Ui, format string, secret *api.Secret) int {
|
2016-02-25 02:45:14 +00:00
|
|
|
return outputWithFormat(ui, format, secret, secret)
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 19:18:27 +00:00
|
|
|
func OutputList(ui cli.Ui, format string, secret *api.Secret) int {
|
2016-02-25 02:45:14 +00:00
|
|
|
return outputWithFormat(ui, format, secret, secret.Data["keys"])
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func outputWithFormat(ui cli.Ui, format string, secret *api.Secret, data interface{}) int {
|
2016-02-12 18:21:42 +00:00
|
|
|
formatter, ok := Formatters[strings.ToLower(format)]
|
|
|
|
if !ok {
|
2016-01-14 19:18:27 +00:00
|
|
|
ui.Error(fmt.Sprintf("Invalid output format: %s", format))
|
|
|
|
return 1
|
|
|
|
}
|
2016-02-25 02:45:14 +00:00
|
|
|
if err := formatter.Output(ui, secret, data); err != nil {
|
2016-02-12 18:21:42 +00:00
|
|
|
ui.Error(fmt.Sprintf("Could not output secret: %s", err.Error()))
|
2015-04-27 21:55:29 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
type Formatter interface {
|
2016-02-25 02:45:14 +00:00
|
|
|
Output(ui cli.Ui, secret *api.Secret, data interface{}) error
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var Formatters = map[string]Formatter{
|
|
|
|
"json": JsonFormatter{},
|
|
|
|
"table": TableFormatter{},
|
|
|
|
"yaml": YamlFormatter{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// An output formatter for json output of an object
|
|
|
|
type JsonFormatter struct {
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (j JsonFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2016-02-12 18:21:42 +00:00
|
|
|
b, err := json.Marshal(data)
|
|
|
|
if err == nil {
|
|
|
|
var out bytes.Buffer
|
|
|
|
json.Indent(&out, b, "", "\t")
|
|
|
|
ui.Output(out.String())
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
2016-02-12 18:21:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-14 19:18:27 +00:00
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
// An output formatter for yaml output format of an object
|
|
|
|
type YamlFormatter struct {
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (y YamlFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2016-02-12 18:21:42 +00:00
|
|
|
b, err := yaml.Marshal(data)
|
|
|
|
if err == nil {
|
|
|
|
ui.Output(strings.TrimSpace(string(b)))
|
2015-12-10 10:32:31 +00:00
|
|
|
}
|
2016-02-12 18:21:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-12-10 10:32:31 +00:00
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
// An output formatter for table output of an object
|
|
|
|
type TableFormatter struct {
|
2015-12-10 10:32:31 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (t TableFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2016-02-12 18:21:42 +00:00
|
|
|
// TODO: this should really use reflection like the other formatters do
|
|
|
|
if s, ok := data.(*api.Secret); ok {
|
2016-02-25 02:45:14 +00:00
|
|
|
return t.OutputSecret(ui, secret, s)
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
2016-02-12 18:21:42 +00:00
|
|
|
if s, ok := data.([]interface{}); ok {
|
2016-02-25 02:45:14 +00:00
|
|
|
return t.OutputList(ui, secret, s)
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
return errors.New("Cannot use the table formatter for this type")
|
|
|
|
}
|
2016-01-14 19:18:27 +00:00
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (t TableFormatter) OutputList(ui cli.Ui, secret *api.Secret, list []interface{}) error {
|
2016-02-12 18:21:42 +00:00
|
|
|
config := columnize.DefaultConfig()
|
|
|
|
config.Delim = "♨"
|
|
|
|
config.Glue = "\t"
|
|
|
|
config.Prefix = ""
|
|
|
|
|
|
|
|
input := make([]string, 0, 5)
|
|
|
|
|
|
|
|
input = append(input, "Keys")
|
|
|
|
|
|
|
|
keys := make([]string, 0, len(list))
|
|
|
|
for _, k := range list {
|
|
|
|
keys = append(keys, k.(string))
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
input = append(input, fmt.Sprintf("%s", k))
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
if len(secret.Warnings) != 0 {
|
|
|
|
input = append(input, "")
|
|
|
|
input = append(input, "The following warnings were returned from the Vault server:")
|
|
|
|
for _, warning := range secret.Warnings {
|
|
|
|
input = append(input, fmt.Sprintf("* %s", warning))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
ui.Output(columnize.Format(input, config))
|
|
|
|
return nil
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (t TableFormatter) OutputSecret(ui cli.Ui, secret, s *api.Secret) error {
|
2015-04-27 21:55:29 +00:00
|
|
|
config := columnize.DefaultConfig()
|
|
|
|
config.Delim = "♨"
|
|
|
|
config.Glue = "\t"
|
|
|
|
config.Prefix = ""
|
|
|
|
|
|
|
|
input := make([]string, 0, 5)
|
2015-10-07 19:30:54 +00:00
|
|
|
|
2015-04-27 21:55:29 +00:00
|
|
|
input = append(input, fmt.Sprintf("Key %s Value", config.Delim))
|
|
|
|
|
2015-09-20 12:30:08 +00:00
|
|
|
if s.LeaseDuration > 0 {
|
2015-09-20 12:34:34 +00:00
|
|
|
if s.LeaseID != "" {
|
|
|
|
input = append(input, fmt.Sprintf("lease_id %s %s", config.Delim, s.LeaseID))
|
|
|
|
}
|
2015-09-21 20:57:41 +00:00
|
|
|
input = append(input, fmt.Sprintf(
|
|
|
|
"lease_duration %s %d", config.Delim, s.LeaseDuration))
|
2015-09-20 12:34:34 +00:00
|
|
|
if s.LeaseID != "" {
|
|
|
|
input = append(input, fmt.Sprintf(
|
|
|
|
"lease_renewable %s %s", config.Delim, strconv.FormatBool(s.Renewable)))
|
|
|
|
}
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 23:59:50 +00:00
|
|
|
if s.Auth != nil {
|
|
|
|
input = append(input, fmt.Sprintf("token %s %s", config.Delim, s.Auth.ClientToken))
|
|
|
|
input = append(input, fmt.Sprintf("token_duration %s %d", config.Delim, s.Auth.LeaseDuration))
|
|
|
|
input = append(input, fmt.Sprintf("token_renewable %s %v", config.Delim, s.Auth.Renewable))
|
|
|
|
input = append(input, fmt.Sprintf("token_policies %s %v", config.Delim, s.Auth.Policies))
|
|
|
|
for k, v := range s.Auth.Metadata {
|
|
|
|
input = append(input, fmt.Sprintf("token_meta_%s %s %#v", k, config.Delim, v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 09:01:22 +00:00
|
|
|
keys := make([]string, 0, len(s.Data))
|
|
|
|
for k := range s.Data {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
input = append(input, fmt.Sprintf("%s %s %v", k, config.Delim, s.Data[k]))
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 19:30:54 +00:00
|
|
|
if len(s.Warnings) != 0 {
|
|
|
|
input = append(input, "")
|
2015-10-07 19:42:57 +00:00
|
|
|
input = append(input, "The following warnings were returned from the Vault server:")
|
2015-10-07 19:30:54 +00:00
|
|
|
for _, warning := range s.Warnings {
|
|
|
|
input = append(input, fmt.Sprintf("* %s", warning))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:55:29 +00:00
|
|
|
ui.Output(columnize.Format(input, config))
|
2016-02-12 18:21:42 +00:00
|
|
|
return nil
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|