forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.go
95 lines (86 loc) · 2.19 KB
/
product.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import "fmt"
// ProductInfo defines product information
type ProductInfo struct {
Family string `json:"family"`
Name string `json:"name"`
Vendor string `json:"vendor"`
SerialNumber string `json:"serial_number"`
UUID string `json:"uuid"`
SKU string `json:"sku"`
Version string `json:"version"`
}
func (i *ProductInfo) String() string {
familyStr := ""
if i.Family != "" {
familyStr = " family=" + i.Family
}
nameStr := ""
if i.Name != "" {
nameStr = " name=" + i.Name
}
vendorStr := ""
if i.Vendor != "" {
vendorStr = " vendor=" + i.Vendor
}
serialStr := ""
if i.SerialNumber != "" && i.SerialNumber != UNKNOWN {
serialStr = " serial=" + i.SerialNumber
}
uuidStr := ""
if i.UUID != "" && i.UUID != UNKNOWN {
uuidStr = " uuid=" + i.UUID
}
skuStr := ""
if i.SKU != "" {
skuStr = " sku=" + i.SKU
}
versionStr := ""
if i.Version != "" {
versionStr = " version=" + i.Version
}
res := fmt.Sprintf(
"product%s%s%s%s%s%s%s",
familyStr,
nameStr,
vendorStr,
serialStr,
uuidStr,
skuStr,
versionStr,
)
return res
}
// Product returns a pointer to a ProductInfo struct containing information
// about the host's product
func Product(opts ...*WithOption) (*ProductInfo, error) {
mergeOpts := mergeOptions(opts...)
ctx := &context{
chroot: *mergeOpts.Chroot,
}
info := &ProductInfo{}
if err := ctx.productFillInfo(info); err != nil {
return nil, err
}
return info, nil
}
// simple private struct used to encapsulate product information in a top-level
// "product" YAML/JSON map/object key
type productPrinter struct {
Info *ProductInfo `json:"product"`
}
// YAMLString returns a string with the product information formatted as YAML
// under a top-level "dmi:" key
func (info *ProductInfo) YAMLString() string {
return safeYAML(productPrinter{info})
}
// JSONString returns a string with the product information formatted as JSON
// under a top-level "product:" key
func (info *ProductInfo) JSONString(indent bool) string {
return safeJSON(productPrinter{info}, indent)
}