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
|
|
|
// KubernetesStackTableFormat is the default Kubernetes stack format
|
2020-08-28 17:09:27 -04:00
|
|
|
KubernetesStackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}\t{{.Namespace}}"
|
|
|
|
|
2018-04-09 09:11:45 -04:00
|
|
|
// SwarmStackTableFormat is the default Swarm stack format
|
2020-08-28 17:09:27 -04:00
|
|
|
SwarmStackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}"
|
2017-03-05 13:02:03 -05:00
|
|
|
|
2018-03-27 10:38:47 -04:00
|
|
|
stackServicesHeader = "SERVICES"
|
|
|
|
stackOrchestrastorHeader = "ORCHESTRATOR"
|
2018-04-09 09:11:45 -04:00
|
|
|
stackNamespaceHeader = "NAMESPACE"
|
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
|
2018-04-09 09:11:45 -04:00
|
|
|
// Orchestrator is the platform where the stack is deployed
|
2018-03-27 10:38:47 -04:00
|
|
|
Orchestrator string
|
2018-04-09 09:11:45 -04:00
|
|
|
// Namespace is the Kubernetes namespace assigned to the stack
|
|
|
|
Namespace string
|
2017-03-05 13:02:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
"Name": formatter.NameHeader,
|
2018-03-27 10:38:47 -04:00
|
|
|
"Services": stackServicesHeader,
|
|
|
|
"Orchestrator": stackOrchestrastorHeader,
|
2018-04-09 09:11:45 -04:00
|
|
|
"Namespace": stackNamespaceHeader,
|
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)
|
|
|
|
}
|
2018-03-27 10:38:47 -04:00
|
|
|
|
|
|
|
func (s *stackContext) Orchestrator() string {
|
|
|
|
return s.s.Orchestrator
|
|
|
|
}
|
2018-04-09 09:11:45 -04:00
|
|
|
|
|
|
|
func (s *stackContext) Namespace() string {
|
|
|
|
return s.s.Namespace
|
|
|
|
}
|