2018-10-23 11:05:44 -04:00
|
|
|
package secret
|
2017-03-05 06:11:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-05-15 18:23:20 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-05-15 18:23:20 -04:00
|
|
|
"github.com/docker/cli/cli/command/inspect"
|
2017-03-05 06:11:04 -05:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
units "github.com/docker/go-units"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-10-28 20:48:50 -04:00
|
|
|
defaultSecretTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.CreatedAt}}\t{{.UpdatedAt}}" // #nosec G101
|
2018-10-23 11:05:44 -04:00
|
|
|
secretIDHeader = "ID"
|
|
|
|
secretCreatedHeader = "CREATED"
|
|
|
|
secretUpdatedHeader = "UPDATED"
|
|
|
|
secretInspectPrettyTemplate formatter.Format = `ID: {{.ID}}
|
2017-07-30 07:01:39 -04:00
|
|
|
Name: {{.Name}}
|
2017-05-15 18:23:20 -04:00
|
|
|
{{- if .Labels }}
|
|
|
|
Labels:
|
|
|
|
{{- range $k, $v := .Labels }}
|
|
|
|
- {{ $k }}{{if $v }}={{ $v }}{{ end }}
|
|
|
|
{{- end }}{{ end }}
|
2017-07-30 07:01:39 -04:00
|
|
|
Driver: {{.Driver}}
|
|
|
|
Created at: {{.CreatedAt}}
|
|
|
|
Updated at: {{.UpdatedAt}}`
|
2017-03-05 06:11:04 -05:00
|
|
|
)
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// NewFormat returns a Format for rendering using a secret Context
|
|
|
|
func NewFormat(source string, quiet bool) formatter.Format {
|
2017-03-05 06:11:04 -05:00
|
|
|
switch source {
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.PrettyFormatKey:
|
2017-05-15 18:23:20 -04:00
|
|
|
return secretInspectPrettyTemplate
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.TableFormatKey:
|
2017-03-05 06:11:04 -05:00
|
|
|
if quiet {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.DefaultQuietFormat
|
2017-03-05 06:11:04 -05:00
|
|
|
}
|
|
|
|
return defaultSecretTableFormat
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.Format(source)
|
2017-03-05 06:11:04 -05:00
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// FormatWrite writes the context
|
|
|
|
func FormatWrite(ctx formatter.Context, secrets []swarm.Secret) error {
|
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-03-05 06:11:04 -05:00
|
|
|
for _, secret := range secrets {
|
|
|
|
secretCtx := &secretContext{s: secret}
|
|
|
|
if err := format(secretCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(newSecretContext(), render)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSecretContext() *secretContext {
|
|
|
|
sCtx := &secretContext{}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
sCtx.Header = formatter.SubHeaderContext{
|
2017-03-05 06:11:04 -05:00
|
|
|
"ID": secretIDHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Name": formatter.NameHeader,
|
|
|
|
"Driver": formatter.DriverHeader,
|
2017-03-05 06:11:04 -05:00
|
|
|
"CreatedAt": secretCreatedHeader,
|
|
|
|
"UpdatedAt": secretUpdatedHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Labels": formatter.LabelsHeader,
|
2017-03-05 06:11:04 -05:00
|
|
|
}
|
|
|
|
return sCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
type secretContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2017-03-05 06:11:04 -05:00
|
|
|
s swarm.Secret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *secretContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.MarshalJSON(c)
|
2017-03-05 06:11:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *secretContext) ID() string {
|
|
|
|
return c.s.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *secretContext) Name() string {
|
|
|
|
return c.s.Spec.Annotations.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *secretContext) CreatedAt() string {
|
|
|
|
return units.HumanDuration(time.Now().UTC().Sub(c.s.Meta.CreatedAt)) + " ago"
|
|
|
|
}
|
|
|
|
|
2017-07-30 07:01:39 -04:00
|
|
|
func (c *secretContext) Driver() string {
|
|
|
|
if c.s.Spec.Driver == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.s.Spec.Driver.Name
|
|
|
|
}
|
|
|
|
|
2017-03-05 06:11:04 -05:00
|
|
|
func (c *secretContext) UpdatedAt() string {
|
|
|
|
return units.HumanDuration(time.Now().UTC().Sub(c.s.Meta.UpdatedAt)) + " ago"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *secretContext) Labels() string {
|
|
|
|
mapLabels := c.s.Spec.Annotations.Labels
|
|
|
|
if mapLabels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var joinLabels []string
|
|
|
|
for k, v := range mapLabels {
|
|
|
|
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
return strings.Join(joinLabels, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *secretContext) Label(name string) string {
|
|
|
|
if c.s.Spec.Annotations.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.s.Spec.Annotations.Labels[name]
|
|
|
|
}
|
2017-05-15 18:23:20 -04:00
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// InspectFormatWrite renders the context for a list of secrets
|
|
|
|
func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
|
2017-05-15 18:23:20 -04:00
|
|
|
if ctx.Format != secretInspectPrettyTemplate {
|
|
|
|
return inspect.Inspect(ctx.Output, refs, string(ctx.Format), getRef)
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-05-15 18:23:20 -04:00
|
|
|
for _, ref := range refs {
|
|
|
|
secretI, _, err := getRef(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
secret, ok := secretI.(swarm.Secret)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("got wrong object to inspect :%v", ok)
|
|
|
|
}
|
|
|
|
if err := format(&secretInspectContext{Secret: secret}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(&secretInspectContext{}, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type secretInspectContext struct {
|
|
|
|
swarm.Secret
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.SubContext
|
2017-05-15 18:23:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *secretInspectContext) ID() string {
|
|
|
|
return ctx.Secret.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *secretInspectContext) Name() string {
|
|
|
|
return ctx.Secret.Spec.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *secretInspectContext) Labels() map[string]string {
|
|
|
|
return ctx.Secret.Spec.Labels
|
|
|
|
}
|
|
|
|
|
2017-07-30 07:01:39 -04:00
|
|
|
func (ctx *secretInspectContext) Driver() string {
|
|
|
|
if ctx.Secret.Spec.Driver == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ctx.Secret.Spec.Driver.Name
|
|
|
|
}
|
|
|
|
|
2017-05-15 18:23:20 -04:00
|
|
|
func (ctx *secretInspectContext) CreatedAt() string {
|
|
|
|
return command.PrettyPrint(ctx.Secret.CreatedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *secretInspectContext) UpdatedAt() string {
|
|
|
|
return command.PrettyPrint(ctx.Secret.UpdatedAt)
|
|
|
|
}
|