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