forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
59 lines (49 loc) · 1.28 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package command
import (
"bytes"
"encoding/json"
"fmt"
"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)
case "table":
fallthrough
default:
return outputFormatTable(ui, secret, true)
}
}
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
}
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)
input = append(input, fmt.Sprintf("Key %s Value", config.Delim))
if s.LeaseID != "" && s.LeaseDuration > 0 {
input = append(input, fmt.Sprintf("lease_id %s %s", config.Delim, s.LeaseID))
input = append(input, fmt.Sprintf(
"lease_duration %s %d", config.Delim, s.LeaseDuration))
}
for k, v := range s.Data {
input = append(input, fmt.Sprintf("%s %s %v", k, config.Delim, v))
}
ui.Output(columnize.Format(input, config))
return 0
}