2015-04-27 21:55:29 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"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 {
|
|
|
|
switch format {
|
|
|
|
case "json":
|
|
|
|
return outputFormatJSON(ui, secret)
|
2015-12-10 10:32:31 +00:00
|
|
|
case "yaml":
|
|
|
|
return outputFormatYAML(ui, secret)
|
2015-04-27 21:55:29 +00:00
|
|
|
case "table":
|
|
|
|
return outputFormatTable(ui, secret, true)
|
2015-12-14 16:13:20 +00:00
|
|
|
default:
|
|
|
|
ui.Error(fmt.Sprintf("Invalid output format: %s", format))
|
|
|
|
return 1
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func outputFormatJSON(ui cli.Ui, s *api.Secret) int {
|
|
|
|
b, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error formatting secret: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
json.Indent(&out, b, "", "\t")
|
|
|
|
ui.Output(out.String())
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-12-10 10:32:31 +00:00
|
|
|
func outputFormatYAML(ui cli.Ui, s *api.Secret) int {
|
|
|
|
b, err := yaml.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error formatting secret: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Output(strings.TrimSpace(string(b)))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:55:29 +00:00
|
|
|
func outputFormatTable(ui cli.Ui, s *api.Secret, whitespace bool) int {
|
|
|
|
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))
|
|
|
|
return 0
|
|
|
|
}
|