github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/plugin/formatter.go (about)

     1  package plugin
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/cli/cli/command/formatter"
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/pkg/stringid"
     9  )
    10  
    11  const (
    12  	defaultPluginTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Description}}\t{{.Enabled}}"
    13  
    14  	enabledHeader  = "ENABLED"
    15  	pluginIDHeader = "ID"
    16  )
    17  
    18  // NewFormat returns a Format for rendering using a plugin Context
    19  func NewFormat(source string, quiet bool) formatter.Format {
    20  	switch source {
    21  	case formatter.TableFormatKey:
    22  		if quiet {
    23  			return formatter.DefaultQuietFormat
    24  		}
    25  		return defaultPluginTableFormat
    26  	case formatter.RawFormatKey:
    27  		if quiet {
    28  			return `plugin_id: {{.ID}}`
    29  		}
    30  		return `plugin_id: {{.ID}}\nname: {{.Name}}\ndescription: {{.Description}}\nenabled: {{.Enabled}}\n`
    31  	}
    32  	return formatter.Format(source)
    33  }
    34  
    35  // FormatWrite writes the context
    36  func FormatWrite(ctx formatter.Context, plugins []*types.Plugin) error {
    37  	render := func(format func(subContext formatter.SubContext) error) error {
    38  		for _, plugin := range plugins {
    39  			pluginCtx := &pluginContext{trunc: ctx.Trunc, p: *plugin}
    40  			if err := format(pluginCtx); err != nil {
    41  				return err
    42  			}
    43  		}
    44  		return nil
    45  	}
    46  	pluginCtx := pluginContext{}
    47  	pluginCtx.Header = formatter.SubHeaderContext{
    48  		"ID":              pluginIDHeader,
    49  		"Name":            formatter.NameHeader,
    50  		"Description":     formatter.DescriptionHeader,
    51  		"Enabled":         enabledHeader,
    52  		"PluginReference": formatter.ImageHeader,
    53  	}
    54  	return ctx.Write(&pluginCtx, render)
    55  }
    56  
    57  type pluginContext struct {
    58  	formatter.HeaderContext
    59  	trunc bool
    60  	p     types.Plugin
    61  }
    62  
    63  func (c *pluginContext) MarshalJSON() ([]byte, error) {
    64  	return formatter.MarshalJSON(c)
    65  }
    66  
    67  func (c *pluginContext) ID() string {
    68  	if c.trunc {
    69  		return stringid.TruncateID(c.p.ID)
    70  	}
    71  	return c.p.ID
    72  }
    73  
    74  func (c *pluginContext) Name() string {
    75  	return c.p.Name
    76  }
    77  
    78  func (c *pluginContext) Description() string {
    79  	desc := strings.Replace(c.p.Config.Description, "\n", "", -1)
    80  	desc = strings.Replace(desc, "\r", "", -1)
    81  	if c.trunc {
    82  		desc = formatter.Ellipsis(desc, 45)
    83  	}
    84  
    85  	return desc
    86  }
    87  
    88  func (c *pluginContext) Enabled() bool {
    89  	return c.p.Enabled
    90  }
    91  
    92  func (c *pluginContext) PluginReference() string {
    93  	return c.p.PluginReference
    94  }