github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli/command/network/formatter.go (about) 1 package network 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/docker/cli/cli/command/formatter" 8 "github.com/docker/docker/api/types" 9 "github.com/docker/docker/pkg/stringid" 10 ) 11 12 const ( 13 defaultNetworkTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.Scope}}" 14 15 networkIDHeader = "NETWORK ID" 16 ipv6Header = "IPV6" 17 internalHeader = "INTERNAL" 18 ) 19 20 // NewFormat returns a Format for rendering using a network Context 21 func NewFormat(source string, quiet bool) formatter.Format { 22 switch source { 23 case formatter.TableFormatKey: 24 if quiet { 25 return formatter.DefaultQuietFormat 26 } 27 return defaultNetworkTableFormat 28 case formatter.RawFormatKey: 29 if quiet { 30 return `network_id: {{.ID}}` 31 } 32 return `network_id: {{.ID}}\nname: {{.Name}}\ndriver: {{.Driver}}\nscope: {{.Scope}}\n` 33 } 34 return formatter.Format(source) 35 } 36 37 // FormatWrite writes the context 38 func FormatWrite(ctx formatter.Context, networks []types.NetworkResource) error { 39 render := func(format func(subContext formatter.SubContext) error) error { 40 for _, network := range networks { 41 networkCtx := &networkContext{trunc: ctx.Trunc, n: network} 42 if err := format(networkCtx); err != nil { 43 return err 44 } 45 } 46 return nil 47 } 48 networkCtx := networkContext{} 49 networkCtx.Header = formatter.SubHeaderContext{ 50 "ID": networkIDHeader, 51 "Name": formatter.NameHeader, 52 "Driver": formatter.DriverHeader, 53 "Scope": formatter.ScopeHeader, 54 "IPv6": ipv6Header, 55 "Internal": internalHeader, 56 "Labels": formatter.LabelsHeader, 57 "CreatedAt": formatter.CreatedAtHeader, 58 } 59 return ctx.Write(&networkCtx, render) 60 } 61 62 type networkContext struct { 63 formatter.HeaderContext 64 trunc bool 65 n types.NetworkResource 66 } 67 68 func (c *networkContext) MarshalJSON() ([]byte, error) { 69 return formatter.MarshalJSON(c) 70 } 71 72 func (c *networkContext) ID() string { 73 if c.trunc { 74 return stringid.TruncateID(c.n.ID) 75 } 76 return c.n.ID 77 } 78 79 func (c *networkContext) Name() string { 80 return c.n.Name 81 } 82 83 func (c *networkContext) Driver() string { 84 return c.n.Driver 85 } 86 87 func (c *networkContext) Scope() string { 88 return c.n.Scope 89 } 90 91 func (c *networkContext) IPv6() string { 92 return fmt.Sprintf("%v", c.n.EnableIPv6) 93 } 94 95 func (c *networkContext) Internal() string { 96 return fmt.Sprintf("%v", c.n.Internal) 97 } 98 99 func (c *networkContext) Labels() string { 100 if c.n.Labels == nil { 101 return "" 102 } 103 104 var joinLabels []string 105 for k, v := range c.n.Labels { 106 joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v)) 107 } 108 return strings.Join(joinLabels, ",") 109 } 110 111 func (c *networkContext) Label(name string) string { 112 if c.n.Labels == nil { 113 return "" 114 } 115 return c.n.Labels[name] 116 } 117 118 func (c *networkContext) CreatedAt() string { 119 return c.n.Created.String() 120 }