github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/registry/formatter_search.go (about)

     1  package registry
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/docker/cli/cli/command/formatter"
     8  	registry "github.com/docker/docker/api/types/registry"
     9  )
    10  
    11  const (
    12  	defaultSearchTableFormat = "table {{.Name}}\t{{.Description}}\t{{.StarCount}}\t{{.IsOfficial}}\t{{.IsAutomated}}"
    13  
    14  	starsHeader     = "STARS"
    15  	officialHeader  = "OFFICIAL"
    16  	automatedHeader = "AUTOMATED"
    17  )
    18  
    19  // NewSearchFormat returns a Format for rendering using a network Context
    20  func NewSearchFormat(source string) formatter.Format {
    21  	switch source {
    22  	case "":
    23  		return defaultSearchTableFormat
    24  	case formatter.TableFormatKey:
    25  		return defaultSearchTableFormat
    26  	}
    27  	return formatter.Format(source)
    28  }
    29  
    30  // SearchWrite writes the context
    31  func SearchWrite(ctx formatter.Context, results []registry.SearchResult) error {
    32  	render := func(format func(subContext formatter.SubContext) error) error {
    33  		for _, result := range results {
    34  			searchCtx := &searchContext{trunc: ctx.Trunc, s: result}
    35  			if err := format(searchCtx); err != nil {
    36  				return err
    37  			}
    38  		}
    39  		return nil
    40  	}
    41  	searchCtx := searchContext{}
    42  	searchCtx.Header = formatter.SubHeaderContext{
    43  		"Name":        formatter.NameHeader,
    44  		"Description": formatter.DescriptionHeader,
    45  		"StarCount":   starsHeader,
    46  		"IsOfficial":  officialHeader,
    47  		"IsAutomated": automatedHeader,
    48  	}
    49  	return ctx.Write(&searchCtx, render)
    50  }
    51  
    52  type searchContext struct {
    53  	formatter.HeaderContext
    54  	trunc bool
    55  	json  bool
    56  	s     registry.SearchResult
    57  }
    58  
    59  func (c *searchContext) MarshalJSON() ([]byte, error) {
    60  	c.json = true
    61  	return formatter.MarshalJSON(c)
    62  }
    63  
    64  func (c *searchContext) Name() string {
    65  	return c.s.Name
    66  }
    67  
    68  func (c *searchContext) Description() string {
    69  	desc := strings.Replace(c.s.Description, "\n", " ", -1)
    70  	desc = strings.Replace(desc, "\r", " ", -1)
    71  	if c.trunc {
    72  		desc = formatter.Ellipsis(desc, 45)
    73  	}
    74  	return desc
    75  }
    76  
    77  func (c *searchContext) StarCount() string {
    78  	return strconv.Itoa(c.s.StarCount)
    79  }
    80  
    81  func (c *searchContext) formatBool(value bool) string {
    82  	switch {
    83  	case value && c.json:
    84  		return "true"
    85  	case value:
    86  		return "[OK]"
    87  	case c.json:
    88  		return "false"
    89  	default:
    90  		return ""
    91  	}
    92  }
    93  
    94  func (c *searchContext) IsOfficial() string {
    95  	return c.formatBool(c.s.IsOfficial)
    96  }
    97  
    98  func (c *searchContext) IsAutomated() string {
    99  	return c.formatBool(c.s.IsAutomated)
   100  }