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

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/docker/cli/cli/command"
     9  	"github.com/docker/cli/cli/command/formatter"
    10  	"github.com/docker/cli/cli/command/inspect"
    11  	"github.com/docker/docker/api/types/swarm"
    12  	units "github.com/docker/go-units"
    13  )
    14  
    15  const (
    16  	defaultConfigTableFormat                     = "table {{.ID}}\t{{.Name}}\t{{.CreatedAt}}\t{{.UpdatedAt}}"
    17  	configIDHeader                               = "ID"
    18  	configCreatedHeader                          = "CREATED"
    19  	configUpdatedHeader                          = "UPDATED"
    20  	configInspectPrettyTemplate formatter.Format = `ID:			{{.ID}}
    21  Name:			{{.Name}}
    22  {{- if .Labels }}
    23  Labels:
    24  {{- range $k, $v := .Labels }}
    25   - {{ $k }}{{if $v }}={{ $v }}{{ end }}
    26  {{- end }}{{ end }}
    27  Created at:            	{{.CreatedAt}}
    28  Updated at:            	{{.UpdatedAt}}
    29  Data:
    30  {{.Data}}`
    31  )
    32  
    33  // NewFormat returns a Format for rendering using a config Context
    34  func NewFormat(source string, quiet bool) formatter.Format {
    35  	switch source {
    36  	case formatter.PrettyFormatKey:
    37  		return configInspectPrettyTemplate
    38  	case formatter.TableFormatKey:
    39  		if quiet {
    40  			return formatter.DefaultQuietFormat
    41  		}
    42  		return defaultConfigTableFormat
    43  	}
    44  	return formatter.Format(source)
    45  }
    46  
    47  // FormatWrite writes the context
    48  func FormatWrite(ctx formatter.Context, configs []swarm.Config) error {
    49  	render := func(format func(subContext formatter.SubContext) error) error {
    50  		for _, config := range configs {
    51  			configCtx := &configContext{c: config}
    52  			if err := format(configCtx); err != nil {
    53  				return err
    54  			}
    55  		}
    56  		return nil
    57  	}
    58  	return ctx.Write(newConfigContext(), render)
    59  }
    60  
    61  func newConfigContext() *configContext {
    62  	cCtx := &configContext{}
    63  
    64  	cCtx.Header = formatter.SubHeaderContext{
    65  		"ID":        configIDHeader,
    66  		"Name":      formatter.NameHeader,
    67  		"CreatedAt": configCreatedHeader,
    68  		"UpdatedAt": configUpdatedHeader,
    69  		"Labels":    formatter.LabelsHeader,
    70  	}
    71  	return cCtx
    72  }
    73  
    74  type configContext struct {
    75  	formatter.HeaderContext
    76  	c swarm.Config
    77  }
    78  
    79  func (c *configContext) MarshalJSON() ([]byte, error) {
    80  	return formatter.MarshalJSON(c)
    81  }
    82  
    83  func (c *configContext) ID() string {
    84  	return c.c.ID
    85  }
    86  
    87  func (c *configContext) Name() string {
    88  	return c.c.Spec.Annotations.Name
    89  }
    90  
    91  func (c *configContext) CreatedAt() string {
    92  	return units.HumanDuration(time.Now().UTC().Sub(c.c.Meta.CreatedAt)) + " ago"
    93  }
    94  
    95  func (c *configContext) UpdatedAt() string {
    96  	return units.HumanDuration(time.Now().UTC().Sub(c.c.Meta.UpdatedAt)) + " ago"
    97  }
    98  
    99  func (c *configContext) Labels() string {
   100  	mapLabels := c.c.Spec.Annotations.Labels
   101  	if mapLabels == nil {
   102  		return ""
   103  	}
   104  	var joinLabels []string
   105  	for k, v := range mapLabels {
   106  		joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
   107  	}
   108  	return strings.Join(joinLabels, ",")
   109  }
   110  
   111  func (c *configContext) Label(name string) string {
   112  	if c.c.Spec.Annotations.Labels == nil {
   113  		return ""
   114  	}
   115  	return c.c.Spec.Annotations.Labels[name]
   116  }
   117  
   118  // InspectFormatWrite renders the context for a list of configs
   119  func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
   120  	if ctx.Format != configInspectPrettyTemplate {
   121  		return inspect.Inspect(ctx.Output, refs, string(ctx.Format), getRef)
   122  	}
   123  	render := func(format func(subContext formatter.SubContext) error) error {
   124  		for _, ref := range refs {
   125  			configI, _, err := getRef(ref)
   126  			if err != nil {
   127  				return err
   128  			}
   129  			config, ok := configI.(swarm.Config)
   130  			if !ok {
   131  				return fmt.Errorf("got wrong object to inspect :%v", ok)
   132  			}
   133  			if err := format(&configInspectContext{Config: config}); err != nil {
   134  				return err
   135  			}
   136  		}
   137  		return nil
   138  	}
   139  	return ctx.Write(&configInspectContext{}, render)
   140  }
   141  
   142  type configInspectContext struct {
   143  	swarm.Config
   144  	formatter.SubContext
   145  }
   146  
   147  func (ctx *configInspectContext) ID() string {
   148  	return ctx.Config.ID
   149  }
   150  
   151  func (ctx *configInspectContext) Name() string {
   152  	return ctx.Config.Spec.Name
   153  }
   154  
   155  func (ctx *configInspectContext) Labels() map[string]string {
   156  	return ctx.Config.Spec.Labels
   157  }
   158  
   159  func (ctx *configInspectContext) CreatedAt() string {
   160  	return command.PrettyPrint(ctx.Config.CreatedAt)
   161  }
   162  
   163  func (ctx *configInspectContext) UpdatedAt() string {
   164  	return command.PrettyPrint(ctx.Config.UpdatedAt)
   165  }
   166  
   167  func (ctx *configInspectContext) Data() string {
   168  	if ctx.Config.Spec.Data == nil {
   169  		return ""
   170  	}
   171  	return string(ctx.Config.Spec.Data)
   172  }