2017-03-05 13:02:03 -05:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2018-10-23 11:05:44 -04:00
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-03-05 13:02:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-04-09 09:11:45 -04:00
|
|
|
// SwarmStackTableFormat is the default Swarm stack format
|
2022-02-22 07:46:35 -05:00
|
|
|
SwarmStackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}"
|
2017-03-05 13:02:03 -05:00
|
|
|
|
2022-02-22 07:46:35 -05:00
|
|
|
stackServicesHeader = "SERVICES"
|
2018-10-23 11:05:44 -04:00
|
|
|
|
|
|
|
// TableFormatKey is an alias for formatter.TableFormatKey
|
|
|
|
TableFormatKey = formatter.TableFormatKey
|
2017-03-05 13:02:03 -05:00
|
|
|
)
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// Context is an alias for formatter.Context
|
|
|
|
type Context = formatter.Context
|
|
|
|
|
|
|
|
// Format is an alias for formatter.Format
|
|
|
|
type Format = formatter.Format
|
|
|
|
|
2017-03-05 13:02:03 -05:00
|
|
|
// Stack contains deployed stack information.
|
|
|
|
type Stack struct {
|
|
|
|
// Name is the name of the stack
|
|
|
|
Name string
|
|
|
|
// Services is the number of the services
|
|
|
|
Services int
|
|
|
|
}
|
|
|
|
|
|
|
|
// StackWrite writes formatted stacks using the Context
|
2018-10-23 11:05:44 -04:00
|
|
|
func StackWrite(ctx formatter.Context, stacks []*Stack) error {
|
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-03-05 13:02:03 -05:00
|
|
|
for _, stack := range stacks {
|
|
|
|
if err := format(&stackContext{s: stack}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(newStackContext(), render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type stackContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2017-03-05 13:02:03 -05:00
|
|
|
s *Stack
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStackContext() *stackContext {
|
|
|
|
stackCtx := stackContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
stackCtx.Header = formatter.SubHeaderContext{
|
2022-02-22 07:46:35 -05:00
|
|
|
"Name": formatter.NameHeader,
|
|
|
|
"Services": stackServicesHeader,
|
2017-03-05 13:02:03 -05:00
|
|
|
}
|
|
|
|
return &stackCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stackContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.MarshalJSON(s)
|
2017-03-05 13:02:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stackContext) Name() string {
|
|
|
|
return s.s.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stackContext) Services() string {
|
|
|
|
return strconv.Itoa(s.s.Services)
|
|
|
|
}
|