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