2017-03-05 13:02:03 -05:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-04-09 09:11:45 -04:00
|
|
|
// KubernetesStackTableFormat is the default Kubernetes stack format
|
|
|
|
KubernetesStackTableFormat = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}\t{{.Namespace}}"
|
|
|
|
// SwarmStackTableFormat is the default Swarm stack format
|
|
|
|
SwarmStackTableFormat = "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"
|
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
|
|
|
|
func StackWrite(ctx Context, stacks []*Stack) error {
|
|
|
|
render := func(format func(subContext subContext) error) error {
|
|
|
|
for _, stack := range stacks {
|
|
|
|
if err := format(&stackContext{s: stack}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(newStackContext(), render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type stackContext struct {
|
|
|
|
HeaderContext
|
|
|
|
s *Stack
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStackContext() *stackContext {
|
|
|
|
stackCtx := stackContext{}
|
|
|
|
stackCtx.header = map[string]string{
|
2018-03-27 10:38:47 -04:00
|
|
|
"Name": nameHeader,
|
|
|
|
"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) {
|
|
|
|
return marshalJSON(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|