github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/engine/licenses.go (about)

     1  package engine
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/docker/cli/cli/command/formatter"
     7  	"github.com/docker/cli/internal/licenseutils"
     8  	"github.com/docker/licensing/model"
     9  )
    10  
    11  const (
    12  	defaultSubscriptionsTableFormat = "table {{.Num}}\t{{.Owner}}\t{{.ProductID}}\t{{.Expires}}\t{{.ComponentsString}}"
    13  	defaultSubscriptionsQuietFormat = "{{.Num}}:{{.Summary}}"
    14  
    15  	numHeader               = "NUM"
    16  	ownerHeader             = "OWNER"
    17  	licenseNameHeader       = "NAME"
    18  	idHeader                = "ID"
    19  	dockerIDHeader          = "DOCKER ID"
    20  	productIDHeader         = "PRODUCT ID"
    21  	productRatePlanHeader   = "PRODUCT RATE PLAN"
    22  	productRatePlanIDHeader = "PRODUCT RATE PLAN ID"
    23  	startHeader             = "START"
    24  	expiresHeader           = "EXPIRES"
    25  	stateHeader             = "STATE"
    26  	eusaHeader              = "EUSA"
    27  	pricingComponentsHeader = "PRICING COMPONENTS"
    28  )
    29  
    30  // NewSubscriptionsFormat returns a Format for rendering using a license Context
    31  func NewSubscriptionsFormat(source string, quiet bool) formatter.Format {
    32  	switch source {
    33  	case formatter.TableFormatKey:
    34  		if quiet {
    35  			return defaultSubscriptionsQuietFormat
    36  		}
    37  		return defaultSubscriptionsTableFormat
    38  	case formatter.RawFormatKey:
    39  		if quiet {
    40  			return `license: {{.ID}}`
    41  		}
    42  		return `license: {{.ID}}\nname: {{.Name}}\nowner: {{.Owner}}\ncomponents: {{.ComponentsString}}\n`
    43  	}
    44  	return formatter.Format(source)
    45  }
    46  
    47  // SubscriptionsWrite writes the context
    48  func SubscriptionsWrite(ctx formatter.Context, subs []licenseutils.LicenseDisplay) error {
    49  	render := func(format func(subContext formatter.SubContext) error) error {
    50  		for _, sub := range subs {
    51  			licenseCtx := &licenseContext{trunc: ctx.Trunc, l: sub}
    52  			if err := format(licenseCtx); err != nil {
    53  				return err
    54  			}
    55  		}
    56  		return nil
    57  	}
    58  	licenseCtx := licenseContext{}
    59  	licenseCtx.Header = map[string]string{
    60  		"Num":               numHeader,
    61  		"Owner":             ownerHeader,
    62  		"Name":              licenseNameHeader,
    63  		"ID":                idHeader,
    64  		"DockerID":          dockerIDHeader,
    65  		"ProductID":         productIDHeader,
    66  		"ProductRatePlan":   productRatePlanHeader,
    67  		"ProductRatePlanID": productRatePlanIDHeader,
    68  		"Start":             startHeader,
    69  		"Expires":           expiresHeader,
    70  		"State":             stateHeader,
    71  		"Eusa":              eusaHeader,
    72  		"ComponentsString":  pricingComponentsHeader,
    73  	}
    74  	return ctx.Write(&licenseCtx, render)
    75  }
    76  
    77  type licenseContext struct {
    78  	formatter.HeaderContext
    79  	trunc bool
    80  	l     licenseutils.LicenseDisplay
    81  }
    82  
    83  func (c *licenseContext) MarshalJSON() ([]byte, error) {
    84  	return formatter.MarshalJSON(c)
    85  }
    86  
    87  func (c *licenseContext) Num() int {
    88  	return c.l.Num
    89  }
    90  
    91  func (c *licenseContext) Owner() string {
    92  	return c.l.Owner
    93  }
    94  
    95  func (c *licenseContext) ComponentsString() string {
    96  	return c.l.ComponentsString
    97  }
    98  
    99  func (c *licenseContext) Summary() string {
   100  	return c.l.String()
   101  }
   102  
   103  func (c *licenseContext) Name() string {
   104  	return c.l.Name
   105  }
   106  
   107  func (c *licenseContext) ID() string {
   108  	return c.l.ID
   109  }
   110  
   111  func (c *licenseContext) DockerID() string {
   112  	return c.l.DockerID
   113  }
   114  
   115  func (c *licenseContext) ProductID() string {
   116  	return c.l.ProductID
   117  }
   118  
   119  func (c *licenseContext) ProductRatePlan() string {
   120  	return c.l.ProductRatePlan
   121  }
   122  
   123  func (c *licenseContext) ProductRatePlanID() string {
   124  	return c.l.ProductRatePlanID
   125  }
   126  
   127  func (c *licenseContext) Start() *time.Time {
   128  	return c.l.Start
   129  }
   130  
   131  func (c *licenseContext) Expires() *time.Time {
   132  	return c.l.Expires
   133  }
   134  
   135  func (c *licenseContext) State() string {
   136  	return c.l.State
   137  }
   138  
   139  func (c *licenseContext) Eusa() *model.EusaState {
   140  	return c.l.Eusa
   141  }
   142  
   143  func (c *licenseContext) PricingComponents() []model.SubscriptionPricingComponent {
   144  	// Dereference the pricing component pointers in the pricing components
   145  	// so it can be rendered properly with the template formatter
   146  
   147  	var ret []model.SubscriptionPricingComponent
   148  	for _, spc := range c.l.PricingComponents {
   149  		if spc == nil {
   150  			continue
   151  		}
   152  		ret = append(ret, *spc)
   153  	}
   154  	return ret
   155  }