github.com/jaypipes/ghw@v0.21.1/pkg/product/product.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package product
     8  
     9  import (
    10  	"github.com/jaypipes/ghw/pkg/context"
    11  	"github.com/jaypipes/ghw/pkg/marshal"
    12  	"github.com/jaypipes/ghw/pkg/option"
    13  	"github.com/jaypipes/ghw/pkg/util"
    14  )
    15  
    16  // Info defines product information
    17  type Info struct {
    18  	ctx          *context.Context
    19  	Family       string `json:"family"`
    20  	Name         string `json:"name"`
    21  	Vendor       string `json:"vendor"`
    22  	SerialNumber string `json:"serial_number"`
    23  	UUID         string `json:"uuid"`
    24  	SKU          string `json:"sku"`
    25  	Version      string `json:"version"`
    26  }
    27  
    28  func (i *Info) String() string {
    29  	familyStr := ""
    30  	if i.Family != "" {
    31  		familyStr = " family=" + i.Family
    32  	}
    33  	nameStr := ""
    34  	if i.Name != "" {
    35  		nameStr = " name=" + i.Name
    36  	}
    37  	vendorStr := ""
    38  	if i.Vendor != "" {
    39  		vendorStr = " vendor=" + i.Vendor
    40  	}
    41  	serialStr := ""
    42  	if i.SerialNumber != "" && i.SerialNumber != util.UNKNOWN {
    43  		serialStr = " serial=" + i.SerialNumber
    44  	}
    45  	uuidStr := ""
    46  	if i.UUID != "" && i.UUID != util.UNKNOWN {
    47  		uuidStr = " uuid=" + i.UUID
    48  	}
    49  	skuStr := ""
    50  	if i.SKU != "" {
    51  		skuStr = " sku=" + i.SKU
    52  	}
    53  	versionStr := ""
    54  	if i.Version != "" {
    55  		versionStr = " version=" + i.Version
    56  	}
    57  
    58  	return "product" + util.ConcatStrings(
    59  		familyStr,
    60  		nameStr,
    61  		vendorStr,
    62  		serialStr,
    63  		uuidStr,
    64  		skuStr,
    65  		versionStr,
    66  	)
    67  }
    68  
    69  // New returns a pointer to a Info struct containing information
    70  // about the host's product
    71  func New(opts ...*option.Option) (*Info, error) {
    72  	ctx := context.New(opts...)
    73  	info := &Info{ctx: ctx}
    74  	if err := ctx.Do(info.load); err != nil {
    75  		return nil, err
    76  	}
    77  	return info, nil
    78  }
    79  
    80  // simple private struct used to encapsulate product information in a top-level
    81  // "product" YAML/JSON map/object key
    82  type productPrinter struct {
    83  	Info *Info `json:"product"`
    84  }
    85  
    86  // YAMLString returns a string with the product information formatted as YAML
    87  // under a top-level "dmi:" key
    88  func (info *Info) YAMLString() string {
    89  	return marshal.SafeYAML(info.ctx, productPrinter{info})
    90  }
    91  
    92  // JSONString returns a string with the product information formatted as JSON
    93  // under a top-level "product:" key
    94  func (info *Info) JSONString(indent bool) string {
    95  	return marshal.SafeJSON(info.ctx, productPrinter{info}, indent)
    96  }