-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathformatter.go
More file actions
199 lines (172 loc) · 5.23 KB
/
formatter.go
File metadata and controls
199 lines (172 loc) · 5.23 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package secret
import (
"fmt"
"strings"
"time"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/inspect"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/go-units"
)
const (
defaultSecretTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.CreatedAt}}\t{{.UpdatedAt}}" // #nosec G101
secretIDHeader = "ID"
secretCreatedHeader = "CREATED"
secretUpdatedHeader = "UPDATED"
secretInspectPrettyTemplate formatter.Format = `ID: {{.ID}}
Name: {{.Name}}
{{- if .Labels }}
Labels:
{{- range $k, $v := .Labels }}
- {{ $k }}{{if $v }}={{ $v }}{{ end }}
{{- end }}{{ end }}
Driver: {{.Driver}}
Created at: {{.CreatedAt}}
Updated at: {{.UpdatedAt}}`
)
// NewFormat returns a Format for rendering using a secret Context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func NewFormat(source string, quiet bool) formatter.Format {
return newFormat(source, quiet)
}
// newFormat returns a Format for rendering using a secretContext.
func newFormat(source string, quiet bool) formatter.Format {
switch source {
case formatter.PrettyFormatKey:
return secretInspectPrettyTemplate
case formatter.TableFormatKey:
if quiet {
return formatter.DefaultQuietFormat
}
return defaultSecretTableFormat
}
return formatter.Format(source)
}
// FormatWrite writes the context
//
// Deprecated: this function was only used internally and will be removed in the next release.
func FormatWrite(fmtCtx formatter.Context, secrets []swarm.Secret) error {
return formatWrite(fmtCtx, secrets)
}
// formatWrite writes the context
func formatWrite(fmtCtx formatter.Context, secrets []swarm.Secret) error {
render := func(format func(subContext formatter.SubContext) error) error {
for _, secret := range secrets {
secretCtx := &secretContext{s: secret}
if err := format(secretCtx); err != nil {
return err
}
}
return nil
}
return fmtCtx.Write(newSecretContext(), render)
}
func newSecretContext() *secretContext {
sCtx := &secretContext{}
sCtx.Header = formatter.SubHeaderContext{
"ID": secretIDHeader,
"Name": formatter.NameHeader,
"Driver": formatter.DriverHeader,
"CreatedAt": secretCreatedHeader,
"UpdatedAt": secretUpdatedHeader,
"Labels": formatter.LabelsHeader,
}
return sCtx
}
type secretContext struct {
formatter.HeaderContext
s swarm.Secret
}
func (c *secretContext) MarshalJSON() ([]byte, error) {
return formatter.MarshalJSON(c)
}
func (c *secretContext) ID() string {
return c.s.ID
}
func (c *secretContext) Name() string {
return c.s.Spec.Annotations.Name
}
func (c *secretContext) CreatedAt() string {
return units.HumanDuration(time.Now().UTC().Sub(c.s.Meta.CreatedAt)) + " ago"
}
func (c *secretContext) Driver() string {
if c.s.Spec.Driver == nil {
return ""
}
return c.s.Spec.Driver.Name
}
func (c *secretContext) UpdatedAt() string {
return units.HumanDuration(time.Now().UTC().Sub(c.s.Meta.UpdatedAt)) + " ago"
}
func (c *secretContext) Labels() string {
mapLabels := c.s.Spec.Annotations.Labels
if mapLabels == nil {
return ""
}
joinLabels := make([]string, 0, len(mapLabels))
for k, v := range mapLabels {
joinLabels = append(joinLabels, k+"="+v)
}
return strings.Join(joinLabels, ",")
}
func (c *secretContext) Label(name string) string {
if c.s.Spec.Annotations.Labels == nil {
return ""
}
return c.s.Spec.Annotations.Labels[name]
}
// InspectFormatWrite renders the context for a list of secrets
//
// Deprecated: this function was only used internally and will be removed in the next release.
func InspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
return inspectFormatWrite(fmtCtx, refs, getRef)
}
// inspectFormatWrite renders the context for a list of secrets.
func inspectFormatWrite(fmtCtx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
if fmtCtx.Format != secretInspectPrettyTemplate {
return inspect.Inspect(fmtCtx.Output, refs, string(fmtCtx.Format), getRef)
}
render := func(format func(subContext formatter.SubContext) error) error {
for _, ref := range refs {
secretI, _, err := getRef(ref)
if err != nil {
return err
}
secret, ok := secretI.(swarm.Secret)
if !ok {
return fmt.Errorf("got wrong object to inspect :%v", ok)
}
if err := format(&secretInspectContext{Secret: secret}); err != nil {
return err
}
}
return nil
}
return fmtCtx.Write(&secretInspectContext{}, render)
}
type secretInspectContext struct {
swarm.Secret
formatter.SubContext
}
func (ctx *secretInspectContext) ID() string {
return ctx.Secret.ID
}
func (ctx *secretInspectContext) Name() string {
return ctx.Secret.Spec.Name
}
func (ctx *secretInspectContext) Labels() map[string]string {
return ctx.Secret.Spec.Labels
}
func (ctx *secretInspectContext) Driver() string {
if ctx.Secret.Spec.Driver == nil {
return ""
}
return ctx.Secret.Spec.Driver.Name
}
func (ctx *secretInspectContext) CreatedAt() string {
return formatter.PrettyPrint(ctx.Secret.CreatedAt)
}
func (ctx *secretInspectContext) UpdatedAt() string {
return formatter.PrettyPrint(ctx.Secret.UpdatedAt)
}