-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
formatter.go
169 lines (146 loc) · 4.99 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package role
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/hashicorp/consul/api"
)
const (
PrettyFormat string = "pretty"
JSONFormat string = "json"
)
// Formatter defines methods provided by role command output formatter
type Formatter interface {
FormatRole(role *api.ACLRole) (string, error)
FormatRoleList(roles []*api.ACLRole) (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) FormatRole(role *api.ACLRole) (string, error) {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("ID: %s\n", role.ID))
buffer.WriteString(fmt.Sprintf("Name: %s\n", role.Name))
if role.Partition != "" {
buffer.WriteString(fmt.Sprintf("Partition: %s\n", role.Partition))
}
if role.Namespace != "" {
buffer.WriteString(fmt.Sprintf("Namespace: %s\n", role.Namespace))
}
buffer.WriteString(fmt.Sprintf("Description: %s\n", role.Description))
if f.showMeta {
buffer.WriteString(fmt.Sprintf("Hash: %x\n", role.Hash))
buffer.WriteString(fmt.Sprintf("Create Index: %d\n", role.CreateIndex))
buffer.WriteString(fmt.Sprintf("Modify Index: %d\n", role.ModifyIndex))
}
if len(role.Policies) > 0 {
buffer.WriteString(fmt.Sprintln("Policies:"))
for _, policy := range role.Policies {
buffer.WriteString(fmt.Sprintf(" %s - %s\n", policy.ID, policy.Name))
}
}
if len(role.ServiceIdentities) > 0 {
buffer.WriteString(fmt.Sprintln("Service Identities:"))
for _, svcid := range role.ServiceIdentities {
if len(svcid.Datacenters) > 0 {
buffer.WriteString(fmt.Sprintf(" %s (Datacenters: %s)\n", svcid.ServiceName, strings.Join(svcid.Datacenters, ", ")))
} else {
buffer.WriteString(fmt.Sprintf(" %s (Datacenters: all)\n", svcid.ServiceName))
}
}
}
if len(role.NodeIdentities) > 0 {
buffer.WriteString(fmt.Sprintln("Node Identities:"))
for _, nodeid := range role.NodeIdentities {
buffer.WriteString(fmt.Sprintf(" %s (Datacenter: %s)\n", nodeid.NodeName, nodeid.Datacenter))
}
}
return buffer.String(), nil
}
func (f *prettyFormatter) FormatRoleList(roles []*api.ACLRole) (string, error) {
var buffer bytes.Buffer
for _, role := range roles {
buffer.WriteString(f.formatRoleListEntry(role))
}
return buffer.String(), nil
}
func (f *prettyFormatter) formatRoleListEntry(role *api.ACLRole) string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("%s:\n", role.Name))
buffer.WriteString(fmt.Sprintf(" ID: %s\n", role.ID))
if role.Partition != "" {
buffer.WriteString(fmt.Sprintf(" Partition: %s\n", role.Partition))
}
if role.Namespace != "" {
buffer.WriteString(fmt.Sprintf(" Namespace: %s\n", role.Namespace))
}
buffer.WriteString(fmt.Sprintf(" Description: %s\n", role.Description))
if f.showMeta {
buffer.WriteString(fmt.Sprintf(" Hash: %x\n", role.Hash))
buffer.WriteString(fmt.Sprintf(" Create Index: %d\n", role.CreateIndex))
buffer.WriteString(fmt.Sprintf(" Modify Index: %d\n", role.ModifyIndex))
}
if len(role.Policies) > 0 {
buffer.WriteString(fmt.Sprintln(" Policies:"))
for _, policy := range role.Policies {
buffer.WriteString(fmt.Sprintf(" %s - %s\n", policy.ID, policy.Name))
}
}
if len(role.ServiceIdentities) > 0 {
buffer.WriteString(fmt.Sprintln(" Service Identities:"))
for _, svcid := range role.ServiceIdentities {
if len(svcid.Datacenters) > 0 {
buffer.WriteString(fmt.Sprintf(" %s (Datacenters: %s)\n", svcid.ServiceName, strings.Join(svcid.Datacenters, ", ")))
} else {
buffer.WriteString(fmt.Sprintf(" %s (Datacenters: all)\n", svcid.ServiceName))
}
}
}
if len(role.NodeIdentities) > 0 {
buffer.WriteString(fmt.Sprintln(" Node Identities:"))
for _, nodeid := range role.NodeIdentities {
buffer.WriteString(fmt.Sprintf(" %s (Datacenter: %s)\n", nodeid.NodeName, nodeid.Datacenter))
}
}
return buffer.String()
}
func newJSONFormatter(showMeta bool) Formatter {
return &jsonFormatter{showMeta}
}
type jsonFormatter struct {
showMeta bool
}
func (f *jsonFormatter) FormatRole(role *api.ACLRole) (string, error) {
b, err := json.MarshalIndent(role, "", " ")
if err != nil {
return "", fmt.Errorf("Failed to marshal role: %v", err)
}
return string(b), nil
}
func (f *jsonFormatter) FormatRoleList(roles []*api.ACLRole) (string, error) {
b, err := json.MarshalIndent(roles, "", " ")
if err != nil {
return "", fmt.Errorf("Failed to marshal roles: %v", err)
}
return string(b), nil
}