@ -1,7 +1,6 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@ -17,60 +16,45 @@ const (
|
||||
internalHeader = "INTERNAL"
|
||||
)
|
||||
|
||||
// NetworkContext contains network specific information required by the formatter,
|
||||
// encapsulate a Context struct.
|
||||
type NetworkContext struct {
|
||||
Context
|
||||
// Networks
|
||||
Networks []types.NetworkResource
|
||||
// NewNetworkFormat returns a Format for rendering using a network Context
|
||||
func NewNetworkFormat(source string, quiet bool) Format {
|
||||
switch source {
|
||||
case TableFormatKey:
|
||||
if quiet {
|
||||
return defaultQuietFormat
|
||||
}
|
||||
return defaultNetworkTableFormat
|
||||
case RawFormatKey:
|
||||
if quiet {
|
||||
return `network_id: {{.ID}}`
|
||||
}
|
||||
return `network_id: {{.ID}}\nname: {{.Name}}\ndriver: {{.Driver}}\nscope: {{.Scope}}\n`
|
||||
}
|
||||
return Format(source)
|
||||
}
|
||||
|
||||
func (ctx NetworkContext) Write() {
|
||||
switch ctx.Format {
|
||||
case tableFormatKey:
|
||||
if ctx.Quiet {
|
||||
ctx.Format = defaultQuietFormat
|
||||
} else {
|
||||
ctx.Format = defaultNetworkTableFormat
|
||||
}
|
||||
case rawFormatKey:
|
||||
if ctx.Quiet {
|
||||
ctx.Format = `network_id: {{.ID}}`
|
||||
} else {
|
||||
ctx.Format = `network_id: {{.ID}}\nname: {{.Name}}\ndriver: {{.Driver}}\nscope: {{.Scope}}\n`
|
||||
// NetworkWrite writes the context
|
||||
func NetworkWrite(ctx Context, networks []types.NetworkResource) error {
|
||||
render := func(format func(subContext subContext) error) error {
|
||||
for _, network := range networks {
|
||||
networkCtx := &networkContext{trunc: ctx.Trunc, n: network}
|
||||
if err := format(networkCtx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx.buffer = bytes.NewBufferString("")
|
||||
ctx.preformat()
|
||||
|
||||
tmpl, err := ctx.parseFormat()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, network := range ctx.Networks {
|
||||
networkCtx := &networkContext{
|
||||
trunc: ctx.Trunc,
|
||||
n: network,
|
||||
}
|
||||
err = ctx.contextFormat(tmpl, networkCtx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.postformat(tmpl, &networkContext{})
|
||||
return ctx.Write(&networkContext{}, render)
|
||||
}
|
||||
|
||||
type networkContext struct {
|
||||
baseSubContext
|
||||
HeaderContext
|
||||
trunc bool
|
||||
n types.NetworkResource
|
||||
}
|
||||
|
||||
func (c *networkContext) ID() string {
|
||||
c.addHeader(networkIDHeader)
|
||||
c.AddHeader(networkIDHeader)
|
||||
if c.trunc {
|
||||
return stringid.TruncateID(c.n.ID)
|
||||
}
|
||||
@ -78,32 +62,32 @@ func (c *networkContext) ID() string {
|
||||
}
|
||||
|
||||
func (c *networkContext) Name() string {
|
||||
c.addHeader(nameHeader)
|
||||
c.AddHeader(nameHeader)
|
||||
return c.n.Name
|
||||
}
|
||||
|
||||
func (c *networkContext) Driver() string {
|
||||
c.addHeader(driverHeader)
|
||||
c.AddHeader(driverHeader)
|
||||
return c.n.Driver
|
||||
}
|
||||
|
||||
func (c *networkContext) Scope() string {
|
||||
c.addHeader(scopeHeader)
|
||||
c.AddHeader(scopeHeader)
|
||||
return c.n.Scope
|
||||
}
|
||||
|
||||
func (c *networkContext) IPv6() string {
|
||||
c.addHeader(ipv6Header)
|
||||
c.AddHeader(ipv6Header)
|
||||
return fmt.Sprintf("%v", c.n.EnableIPv6)
|
||||
}
|
||||
|
||||
func (c *networkContext) Internal() string {
|
||||
c.addHeader(internalHeader)
|
||||
c.AddHeader(internalHeader)
|
||||
return fmt.Sprintf("%v", c.n.Internal)
|
||||
}
|
||||
|
||||
func (c *networkContext) Labels() string {
|
||||
c.addHeader(labelsHeader)
|
||||
c.AddHeader(labelsHeader)
|
||||
if c.n.Labels == nil {
|
||||
return ""
|
||||
}
|
||||
@ -120,7 +104,7 @@ func (c *networkContext) Label(name string) string {
|
||||
r := strings.NewReplacer("-", " ", "_", " ")
|
||||
h := r.Replace(n[len(n)-1])
|
||||
|
||||
c.addHeader(h)
|
||||
c.AddHeader(h)
|
||||
|
||||
if c.n.Labels == nil {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user