-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
formatter.go
140 lines (118 loc) · 4.13 KB
/
formatter.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package authmethod
import (
"bytes"
"encoding/json"
"fmt"
"github.com/hashicorp/consul/api"
)
const (
PrettyFormat string = "pretty"
JSONFormat string = "json"
)
// Formatter defines methods provided by authmethod command output formatter
type Formatter interface {
FormatAuthMethod(method *api.ACLAuthMethod) (string, error)
FormatAuthMethodList(methods []*api.ACLAuthMethodListEntry) (string, error)
}
// GetSupportedFormats returns supported formats
func GetSupportedFormats() []string {
return []string{PrettyFormat, JSONFormat}
}
// NewFormatter returns Formatter implementation
func NewFormatter(format string, showMeta bool) (formatter Formatter, err error) {
switch format {
case PrettyFormat:
formatter = newPrettyFormatter(showMeta)
case JSONFormat:
formatter = newJSONFormatter(showMeta)
default:
err = fmt.Errorf("Unknown format: %s", format)
}
return formatter, err
}
func newPrettyFormatter(showMeta bool) Formatter {
return &prettyFormatter{showMeta}
}
type prettyFormatter struct {
showMeta bool
}
func (f *prettyFormatter) FormatAuthMethod(method *api.ACLAuthMethod) (string, error) {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("Name: %s\n", method.Name))
buffer.WriteString(fmt.Sprintf("Type: %s\n", method.Type))
if method.Namespace != "" {
buffer.WriteString(fmt.Sprintf("Namespace: %s\n", method.Namespace))
}
if method.DisplayName != "" {
buffer.WriteString(fmt.Sprintf("DisplayName: %s\n", method.DisplayName))
}
buffer.WriteString(fmt.Sprintf("Description: %s\n", method.Description))
if method.MaxTokenTTL > 0 {
buffer.WriteString(fmt.Sprintf("MaxTokenTTL: %s\n", method.MaxTokenTTL))
}
if method.TokenLocality != "" {
buffer.WriteString(fmt.Sprintf("TokenLocality: %s\n", method.TokenLocality))
}
if len(method.NamespaceRules) > 0 {
buffer.WriteString(fmt.Sprintln("NamespaceRules:"))
for _, rule := range method.NamespaceRules {
buffer.WriteString(fmt.Sprintf(" Selector: %s\n", rule.Selector))
buffer.WriteString(fmt.Sprintf(" BindNamespace: %s\n", rule.BindNamespace))
}
}
if f.showMeta {
buffer.WriteString(fmt.Sprintf("Create Index: %d\n", method.CreateIndex))
buffer.WriteString(fmt.Sprintf("Modify Index: %d\n", method.ModifyIndex))
}
buffer.WriteString(fmt.Sprintln("Config:"))
output, err := json.MarshalIndent(method.Config, "", " ")
if err != nil {
return "", fmt.Errorf("Error formatting auth method configuration: %s", err)
}
buffer.WriteString(string(output))
return buffer.String(), nil
}
func (f *prettyFormatter) FormatAuthMethodList(methods []*api.ACLAuthMethodListEntry) (string, error) {
var buffer bytes.Buffer
for _, method := range methods {
buffer.WriteString(f.formatAuthMethodListEntry(method))
}
return buffer.String(), nil
}
func (f *prettyFormatter) formatAuthMethodListEntry(method *api.ACLAuthMethodListEntry) string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("%s:\n", method.Name))
buffer.WriteString(fmt.Sprintf(" Type: %s\n", method.Type))
if method.Namespace != "" {
buffer.WriteString(fmt.Sprintf(" Namespace: %s\n", method.Namespace))
}
if method.DisplayName != "" {
buffer.WriteString(fmt.Sprintf(" DisplayName: %s\n", method.DisplayName))
}
buffer.WriteString(fmt.Sprintf(" Description: %s\n", method.Description))
if f.showMeta {
buffer.WriteString(fmt.Sprintf(" Create Index: %d\n", method.CreateIndex))
buffer.WriteString(fmt.Sprintf(" Modify Index: %d\n", method.ModifyIndex))
}
return buffer.String()
}
func newJSONFormatter(showMeta bool) Formatter {
return &jsonFormatter{showMeta}
}
type jsonFormatter struct {
showMeta bool
}
func (f *jsonFormatter) FormatAuthMethod(method *api.ACLAuthMethod) (string, error) {
b, err := json.MarshalIndent(method, "", " ")
if err != nil {
return "", fmt.Errorf("Failed to marshal authmethod:, %v", err)
}
return string(b), nil
}
func (f *jsonFormatter) FormatAuthMethodList(methods []*api.ACLAuthMethodListEntry) (string, error) {
b, err := json.MarshalIndent(methods, "", " ")
if err != nil {
return "", fmt.Errorf("Failed to marshal authmethods:, %v", err)
}
return string(b), nil
}