forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chassis.go
117 lines (106 loc) · 2.76 KB
/
chassis.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// 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"
var (
chassisTypeDescriptions = map[string]string{
"1": "Other",
"2": "Unknown",
"3": "Desktop",
"4": "Low profile desktop",
"5": "Pizza box",
"6": "Mini tower",
"7": "Tower",
"8": "Portable",
"9": "Laptop",
"10": "Notebook",
"11": "Hand held",
"12": "Docking station",
"13": "All in one",
"14": "Sub notebook",
"15": "Space-saving",
"16": "Lunch box",
"17": "Main server chassis",
"18": "Expansion chassis",
"19": "SubChassis",
"20": "Bus Expansion chassis",
"21": "Peripheral chassis",
"22": "RAID chassis",
"23": "Rack mount chassis",
"24": "Sealed-case PC",
"25": "Multi-system chassis",
"26": "Compact PCI",
"27": "Advanced TCA",
"28": "Blade",
"29": "Blade enclosure",
"30": "Tablet",
"31": "Convertible",
"32": "Detachable",
"33": "IoT gateway",
"34": "Embedded PC",
"35": "Mini PC",
"36": "Stick PC",
}
)
// ChassisInfo defines chassis release information
type ChassisInfo struct {
AssetTag string `json:"asset_tag"`
SerialNumber string `json:"serial_number"`
Type string `json:"type"`
TypeDescription string `json:"type_description"`
Vendor string `json:"vendor"`
Version string `json:"version"`
}
func (i *ChassisInfo) String() string {
vendorStr := ""
if i.Vendor != "" {
vendorStr = " vendor=" + i.Vendor
}
serialStr := ""
if i.SerialNumber != "" && i.SerialNumber != UNKNOWN {
serialStr = " serial=" + i.SerialNumber
}
versionStr := ""
if i.Version != "" {
versionStr = " version=" + i.Version
}
res := fmt.Sprintf(
"chassis type=%s%s%s%s",
i.TypeDescription,
vendorStr,
serialStr,
versionStr,
)
return res
}
// Chassis returns a pointer to a ChassisInfo struct containing information
// about the host's chassis
func Chassis(opts ...*WithOption) (*ChassisInfo, error) {
mergeOpts := mergeOptions(opts...)
ctx := &context{
chroot: *mergeOpts.Chroot,
}
info := &ChassisInfo{}
if err := ctx.chassisFillInfo(info); err != nil {
return nil, err
}
return info, nil
}
// simple private struct used to encapsulate chassis information in a top-level
// "chassis" YAML/JSON map/object key
type chassisPrinter struct {
Info *ChassisInfo `json:"chassis"`
}
// YAMLString returns a string with the chassis information formatted as YAML
// under a top-level "dmi:" key
func (info *ChassisInfo) YAMLString() string {
return safeYAML(chassisPrinter{info})
}
// JSONString returns a string with the chassis information formatted as JSON
// under a top-level "chassis:" key
func (info *ChassisInfo) JSONString(indent bool) string {
return safeJSON(chassisPrinter{info}, indent)
}