github.1git.de/docker/cli@v26.1.3+incompatible/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  	registrytypes "github.com/docker/docker/api/types/registry"
     9  )
    10  
    11  const (
    12  	defaultSearchTableFormat = "table {{.Name}}\t{{.Description}}\t{{.StarCount}}\t{{.IsOfficial}}"
    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 "", formatter.TableFormatKey:
    23  		return defaultSearchTableFormat
    24  	}
    25  	return formatter.Format(source)
    26  }
    27  
    28  // SearchWrite writes the context
    29  func SearchWrite(ctx formatter.Context, results []registrytypes.SearchResult) error {
    30  	render := func(format func(subContext formatter.SubContext) error) error {
    31  		for _, result := range results {
    32  			searchCtx := &searchContext{trunc: ctx.Trunc, s: result}
    33  			if err := format(searchCtx); err != nil {
    34  				return err
    35  			}
    36  		}
    37  		return nil
    38  	}
    39  	searchCtx := searchContext{}
    40  	searchCtx.Header = formatter.SubHeaderContext{
    41  		"Name":        formatter.NameHeader,
    42  		"Description": formatter.DescriptionHeader,
    43  		"StarCount":   starsHeader,
    44  		"IsOfficial":  officialHeader,
    45  		"IsAutomated": automatedHeader,
    46  	}
    47  	return ctx.Write(&searchCtx, render)
    48  }
    49  
    50  type searchContext struct {
    51  	formatter.HeaderContext
    52  	trunc bool
    53  	json  bool
    54  	s     registrytypes.SearchResult
    55  }
    56  
    57  func (c *searchContext) MarshalJSON() ([]byte, error) {
    58  	c.json = true
    59  	return formatter.MarshalJSON(c)
    60  }
    61  
    62  func (c *searchContext) Name() string {
    63  	return c.s.Name
    64  }
    65  
    66  func (c *searchContext) Description() string {
    67  	desc := strings.ReplaceAll(c.s.Description, "\n", " ")
    68  	desc = strings.ReplaceAll(desc, "\r", " ")
    69  	if c.trunc {
    70  		desc = formatter.Ellipsis(desc, 45)
    71  	}
    72  	return desc
    73  }
    74  
    75  func (c *searchContext) StarCount() string {
    76  	return strconv.Itoa(c.s.StarCount)
    77  }
    78  
    79  func (c *searchContext) formatBool(value bool) string {
    80  	switch {
    81  	case value && c.json:
    82  		return "true"
    83  	case value:
    84  		return "[OK]"
    85  	case c.json:
    86  		return "false"
    87  	default:
    88  		return ""
    89  	}
    90  }
    91  
    92  func (c *searchContext) IsOfficial() string {
    93  	return c.formatBool(c.s.IsOfficial)
    94  }
    95  
    96  // IsAutomated formats the IsAutomated field for printing.
    97  //
    98  // Deprecated: the "is_automated" field is deprecated and will always be "false" in the future.
    99  func (c *searchContext) IsAutomated() string {
   100  	return c.formatBool(c.s.IsAutomated) //nolint:nolintlint,staticcheck // ignore SA1019 (IsAutomated is deprecated).
   101  }