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