-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathlist_formatter.go
More file actions
53 lines (44 loc) · 1.24 KB
/
list_formatter.go
File metadata and controls
53 lines (44 loc) · 1.24 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
package stack
import (
"strconv"
"github.com/docker/cli/cli/command/formatter"
)
// stackTableFormat is the default Swarm stack format
const stackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}"
// stackSummary contains deployed stack information.
type stackSummary struct {
Name string // Name is the name of the stack.
Services int // Services is the number services in the stack.
}
// stackWrite writes formatted stacks using the Context
func stackWrite(fmtCtx formatter.Context, stacks []stackSummary) error {
stackCtx := &stackContext{
HeaderContext: formatter.HeaderContext{
Header: formatter.SubHeaderContext{
"Name": formatter.NameHeader,
"Services": "SERVICES",
},
},
}
return fmtCtx.Write(stackCtx, func(format func(subContext formatter.SubContext) error) error {
for _, stack := range stacks {
if err := format(&stackContext{s: stack}); err != nil {
return err
}
}
return nil
})
}
type stackContext struct {
formatter.HeaderContext
s stackSummary
}
func (s *stackContext) MarshalJSON() ([]byte, error) {
return formatter.MarshalJSON(s)
}
func (s *stackContext) Name() string {
return s.s.Name
}
func (s *stackContext) Services() string {
return strconv.Itoa(s.s.Services)
}