github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/stack/formatter/formatter.go (about) 1 package formatter 2 3 import ( 4 "strconv" 5 6 "github.com/docker/cli/cli/command/formatter" 7 ) 8 9 const ( 10 // KubernetesStackTableFormat is the default Kubernetes stack format 11 KubernetesStackTableFormat = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}\t{{.Namespace}}" 12 // SwarmStackTableFormat is the default Swarm stack format 13 SwarmStackTableFormat = "table {{.Name}}\t{{.Services}}\t{{.Orchestrator}}" 14 15 stackServicesHeader = "SERVICES" 16 stackOrchestrastorHeader = "ORCHESTRATOR" 17 stackNamespaceHeader = "NAMESPACE" 18 19 // TableFormatKey is an alias for formatter.TableFormatKey 20 TableFormatKey = formatter.TableFormatKey 21 ) 22 23 // Context is an alias for formatter.Context 24 type Context = formatter.Context 25 26 // Format is an alias for formatter.Format 27 type Format = formatter.Format 28 29 // Stack contains deployed stack information. 30 type Stack struct { 31 // Name is the name of the stack 32 Name string 33 // Services is the number of the services 34 Services int 35 // Orchestrator is the platform where the stack is deployed 36 Orchestrator string 37 // Namespace is the Kubernetes namespace assigned to the stack 38 Namespace string 39 } 40 41 // StackWrite writes formatted stacks using the Context 42 func StackWrite(ctx formatter.Context, stacks []*Stack) error { 43 render := func(format func(subContext formatter.SubContext) error) error { 44 for _, stack := range stacks { 45 if err := format(&stackContext{s: stack}); err != nil { 46 return err 47 } 48 } 49 return nil 50 } 51 return ctx.Write(newStackContext(), render) 52 } 53 54 type stackContext struct { 55 formatter.HeaderContext 56 s *Stack 57 } 58 59 func newStackContext() *stackContext { 60 stackCtx := stackContext{} 61 stackCtx.Header = formatter.SubHeaderContext{ 62 "Name": formatter.NameHeader, 63 "Services": stackServicesHeader, 64 "Orchestrator": stackOrchestrastorHeader, 65 "Namespace": stackNamespaceHeader, 66 } 67 return &stackCtx 68 } 69 70 func (s *stackContext) MarshalJSON() ([]byte, error) { 71 return formatter.MarshalJSON(s) 72 } 73 74 func (s *stackContext) Name() string { 75 return s.s.Name 76 } 77 78 func (s *stackContext) Services() string { 79 return strconv.Itoa(s.s.Services) 80 } 81 82 func (s *stackContext) Orchestrator() string { 83 return s.s.Orchestrator 84 } 85 86 func (s *stackContext) Namespace() string { 87 return s.s.Namespace 88 }