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