github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli/command/formatter/context.go (about)

     1  package formatter
     2  
     3  const (
     4  	// ClientContextTableFormat is the default client context format
     5  	ClientContextTableFormat = "table {{.Name}}{{if .Current}} *{{end}}\t{{.Description}}\t{{.DockerEndpoint}}\t{{.KubernetesEndpoint}}\t{{.StackOrchestrator}}"
     6  
     7  	dockerEndpointHeader     = "DOCKER ENDPOINT"
     8  	kubernetesEndpointHeader = "KUBERNETES ENDPOINT"
     9  	stackOrchestrastorHeader = "ORCHESTRATOR"
    10  	quietContextFormat       = "{{.Name}}"
    11  )
    12  
    13  // NewClientContextFormat returns a Format for rendering using a Context
    14  func NewClientContextFormat(source string, quiet bool) Format {
    15  	if quiet {
    16  		return Format(quietContextFormat)
    17  	}
    18  	if source == TableFormatKey {
    19  		return Format(ClientContextTableFormat)
    20  	}
    21  	return Format(source)
    22  }
    23  
    24  // ClientContext is a context for display
    25  type ClientContext struct {
    26  	Name               string
    27  	Description        string
    28  	DockerEndpoint     string
    29  	KubernetesEndpoint string
    30  	StackOrchestrator  string
    31  	Current            bool
    32  }
    33  
    34  // ClientContextWrite writes formatted contexts using the Context
    35  func ClientContextWrite(ctx Context, contexts []*ClientContext) error {
    36  	render := func(format func(subContext SubContext) error) error {
    37  		for _, context := range contexts {
    38  			if err := format(&clientContextContext{c: context}); err != nil {
    39  				return err
    40  			}
    41  		}
    42  		return nil
    43  	}
    44  	return ctx.Write(newClientContextContext(), render)
    45  }
    46  
    47  type clientContextContext struct {
    48  	HeaderContext
    49  	c *ClientContext
    50  }
    51  
    52  func newClientContextContext() *clientContextContext {
    53  	ctx := clientContextContext{}
    54  	ctx.Header = SubHeaderContext{
    55  		"Name":               NameHeader,
    56  		"Description":        DescriptionHeader,
    57  		"DockerEndpoint":     dockerEndpointHeader,
    58  		"KubernetesEndpoint": kubernetesEndpointHeader,
    59  		"StackOrchestrator":  stackOrchestrastorHeader,
    60  	}
    61  	return &ctx
    62  }
    63  
    64  func (c *clientContextContext) MarshalJSON() ([]byte, error) {
    65  	return MarshalJSON(c)
    66  }
    67  
    68  func (c *clientContextContext) Current() bool {
    69  	return c.c.Current
    70  }
    71  
    72  func (c *clientContextContext) Name() string {
    73  	return c.c.Name
    74  }
    75  
    76  func (c *clientContextContext) Description() string {
    77  	return c.c.Description
    78  }
    79  
    80  func (c *clientContextContext) DockerEndpoint() string {
    81  	return c.c.DockerEndpoint
    82  }
    83  
    84  func (c *clientContextContext) KubernetesEndpoint() string {
    85  	return c.c.KubernetesEndpoint
    86  }
    87  
    88  func (c *clientContextContext) StackOrchestrator() string {
    89  	return c.c.StackOrchestrator
    90  }