2018-10-23 11:05:44 -04:00
|
|
|
package registry
|
2017-08-15 06:58:49 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-08-15 06:58:49 -04:00
|
|
|
registry "github.com/docker/docker/api/types/registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultSearchTableFormat = "table {{.Name}}\t{{.Description}}\t{{.StarCount}}\t{{.IsOfficial}}\t{{.IsAutomated}}"
|
|
|
|
|
|
|
|
starsHeader = "STARS"
|
|
|
|
officialHeader = "OFFICIAL"
|
|
|
|
automatedHeader = "AUTOMATED"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewSearchFormat returns a Format for rendering using a network Context
|
2018-10-23 11:05:44 -04:00
|
|
|
func NewSearchFormat(source string) formatter.Format {
|
2017-08-15 06:58:49 -04:00
|
|
|
switch source {
|
|
|
|
case "":
|
|
|
|
return defaultSearchTableFormat
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.TableFormatKey:
|
2017-08-15 06:58:49 -04:00
|
|
|
return defaultSearchTableFormat
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.Format(source)
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SearchWrite writes the context
|
2020-02-13 11:18:10 -05:00
|
|
|
func SearchWrite(ctx formatter.Context, results []registry.SearchResult) error {
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-08-15 06:58:49 -04:00
|
|
|
for _, result := range results {
|
|
|
|
searchCtx := &searchContext{trunc: ctx.Trunc, s: result}
|
|
|
|
if err := format(searchCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
searchCtx := searchContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
searchCtx.Header = formatter.SubHeaderContext{
|
|
|
|
"Name": formatter.NameHeader,
|
|
|
|
"Description": formatter.DescriptionHeader,
|
2017-08-15 06:58:49 -04:00
|
|
|
"StarCount": starsHeader,
|
|
|
|
"IsOfficial": officialHeader,
|
|
|
|
"IsAutomated": automatedHeader,
|
|
|
|
}
|
|
|
|
return ctx.Write(&searchCtx, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type searchContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2017-08-15 06:58:49 -04:00
|
|
|
trunc bool
|
|
|
|
json bool
|
|
|
|
s registry.SearchResult
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchContext) MarshalJSON() ([]byte, error) {
|
|
|
|
c.json = true
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.MarshalJSON(c)
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchContext) Name() string {
|
|
|
|
return c.s.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchContext) Description() string {
|
|
|
|
desc := strings.Replace(c.s.Description, "\n", " ", -1)
|
|
|
|
desc = strings.Replace(desc, "\r", " ", -1)
|
|
|
|
if c.trunc {
|
2018-10-23 11:05:44 -04:00
|
|
|
desc = formatter.Ellipsis(desc, 45)
|
2017-08-15 06:58:49 -04:00
|
|
|
}
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchContext) StarCount() string {
|
|
|
|
return strconv.Itoa(c.s.StarCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchContext) formatBool(value bool) string {
|
|
|
|
switch {
|
|
|
|
case value && c.json:
|
|
|
|
return "true"
|
|
|
|
case value:
|
|
|
|
return "[OK]"
|
|
|
|
case c.json:
|
|
|
|
return "false"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchContext) IsOfficial() string {
|
|
|
|
return c.formatBool(c.s.IsOfficial)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchContext) IsAutomated() string {
|
|
|
|
return c.formatBool(c.s.IsAutomated)
|
|
|
|
}
|