github.com/jaypipes/ghw@v0.21.1/pkg/baseboard/baseboard.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 baseboard
     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 baseboard release information
    17  type Info struct {
    18  	ctx          *context.Context
    19  	AssetTag     string `json:"asset_tag"`
    20  	SerialNumber string `json:"serial_number"`
    21  	Vendor       string `json:"vendor"`
    22  	Version      string `json:"version"`
    23  	Product      string `json:"product"`
    24  }
    25  
    26  func (i *Info) String() string {
    27  	vendorStr := ""
    28  	if i.Vendor != "" {
    29  		vendorStr = " vendor=" + i.Vendor
    30  	}
    31  	serialStr := ""
    32  	if i.SerialNumber != "" && i.SerialNumber != util.UNKNOWN {
    33  		serialStr = " serial=" + i.SerialNumber
    34  	}
    35  	versionStr := ""
    36  	if i.Version != "" {
    37  		versionStr = " version=" + i.Version
    38  	}
    39  
    40  	productStr := ""
    41  	if i.Product != "" {
    42  		productStr = " product=" + i.Product
    43  	}
    44  
    45  	return "baseboard" + util.ConcatStrings(
    46  		vendorStr,
    47  		serialStr,
    48  		versionStr,
    49  		productStr,
    50  	)
    51  }
    52  
    53  // New returns a pointer to an Info struct containing information about the
    54  // host's baseboard
    55  func New(opts ...*option.Option) (*Info, error) {
    56  	ctx := context.New(opts...)
    57  	info := &Info{ctx: ctx}
    58  	if err := ctx.Do(info.load); err != nil {
    59  		return nil, err
    60  	}
    61  	return info, nil
    62  }
    63  
    64  // simple private struct used to encapsulate baseboard information in a top-level
    65  // "baseboard" YAML/JSON map/object key
    66  type baseboardPrinter struct {
    67  	Info *Info `json:"baseboard"`
    68  }
    69  
    70  // YAMLString returns a string with the baseboard information formatted as YAML
    71  // under a top-level "dmi:" key
    72  func (info *Info) YAMLString() string {
    73  	return marshal.SafeYAML(info.ctx, baseboardPrinter{info})
    74  }
    75  
    76  // JSONString returns a string with the baseboard information formatted as JSON
    77  // under a top-level "baseboard:" key
    78  func (info *Info) JSONString(indent bool) string {
    79  	return marshal.SafeJSON(info.ctx, baseboardPrinter{info}, indent)
    80  }