github.com/jaypipes/ghw@v0.21.1/pkg/chassis/chassis.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 chassis 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 var ( 17 chassisTypeDescriptions = map[string]string{ 18 "1": "Other", 19 "2": "Unknown", 20 "3": "Desktop", 21 "4": "Low profile desktop", 22 "5": "Pizza box", 23 "6": "Mini tower", 24 "7": "Tower", 25 "8": "Portable", 26 "9": "Laptop", 27 "10": "Notebook", 28 "11": "Hand held", 29 "12": "Docking station", 30 "13": "All in one", 31 "14": "Sub notebook", 32 "15": "Space-saving", 33 "16": "Lunch box", 34 "17": "Main server chassis", 35 "18": "Expansion chassis", 36 "19": "SubChassis", 37 "20": "Bus Expansion chassis", 38 "21": "Peripheral chassis", 39 "22": "RAID chassis", 40 "23": "Rack mount chassis", 41 "24": "Sealed-case PC", 42 "25": "Multi-system chassis", 43 "26": "Compact PCI", 44 "27": "Advanced TCA", 45 "28": "Blade", 46 "29": "Blade enclosure", 47 "30": "Tablet", 48 "31": "Convertible", 49 "32": "Detachable", 50 "33": "IoT gateway", 51 "34": "Embedded PC", 52 "35": "Mini PC", 53 "36": "Stick PC", 54 } 55 ) 56 57 // Info defines chassis release information 58 type Info struct { 59 ctx *context.Context 60 AssetTag string `json:"asset_tag"` 61 SerialNumber string `json:"serial_number"` 62 Type string `json:"type"` 63 TypeDescription string `json:"type_description"` 64 Vendor string `json:"vendor"` 65 Version string `json:"version"` 66 } 67 68 func (i *Info) String() string { 69 vendorStr := "" 70 if i.Vendor != "" { 71 vendorStr = " vendor=" + i.Vendor 72 } 73 serialStr := "" 74 if i.SerialNumber != "" && i.SerialNumber != util.UNKNOWN { 75 serialStr = " serial=" + i.SerialNumber 76 } 77 versionStr := "" 78 if i.Version != "" { 79 versionStr = " version=" + i.Version 80 } 81 82 return "chassis type=" + util.ConcatStrings( 83 i.TypeDescription, 84 vendorStr, 85 serialStr, 86 versionStr, 87 ) 88 } 89 90 // New returns a pointer to a Info struct containing information 91 // about the host's chassis 92 func New(opts ...*option.Option) (*Info, error) { 93 ctx := context.New(opts...) 94 info := &Info{ctx: ctx} 95 if err := ctx.Do(info.load); err != nil { 96 return nil, err 97 } 98 return info, nil 99 } 100 101 // simple private struct used to encapsulate chassis information in a top-level 102 // "chassis" YAML/JSON map/object key 103 type chassisPrinter struct { 104 Info *Info `json:"chassis"` 105 } 106 107 // YAMLString returns a string with the chassis information formatted as YAML 108 // under a top-level "dmi:" key 109 func (info *Info) YAMLString() string { 110 return marshal.SafeYAML(info.ctx, chassisPrinter{info}) 111 } 112 113 // JSONString returns a string with the chassis information formatted as JSON 114 // under a top-level "chassis:" key 115 func (info *Info) JSONString(indent bool) string { 116 return marshal.SafeJSON(info.ctx, chassisPrinter{info}, indent) 117 }