-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (86 loc) · 2.06 KB
/
main.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
package main
import (
"fmt"
"os"
"strings"
"github.com/jessevdk/go-flags"
"github.com/mt-inside/go-lmsensors"
)
func main() {
var opts struct {
Type bool `short:"t" long:"type" description:"Print the type of the sensor"`
TypeSymbol bool `short:"T" long:"type-symbol" description:"Print the type of the sensor, as a unicode symbol (font-dependant)"`
Unit bool `short:"u" long:"unit" description:"Print the unit of the reading"`
Name bool `short:"n" long:"name" description:"Print the name of the sensor"`
}
args, err := flags.Parse(&opts)
if err != nil {
panic(err)
}
if err := lmsensors.Init(); err != nil {
panic(err)
}
sensors, err := lmsensors.Get()
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
output := []string{}
for _, arg := range args {
var label string
labels := strings.Split(arg, "=")
if len(labels) > 2 {
fmt.Println("Error: invalid sensor address")
os.Exit(1)
}
addrs := strings.Split(labels[0], "/")
if len(addrs) > 2 {
fmt.Println("Error: invalid sensor address")
os.Exit(1)
}
if len(labels) == 2 {
label = labels[1]
} else {
label = addrs[1]
}
reading := sensors.Chips[addrs[0]].Sensors[addrs[1]]
if reading == nil {
fmt.Println("Error: can't find Sensor")
os.Exit(1)
}
var sb strings.Builder
if opts.TypeSymbol {
switch reading.(type) {
case *lmsensors.TempSensor:
sb.WriteString(" ")
case *lmsensors.FanSensor:
sb.WriteString(" ")
case *lmsensors.VoltageSensor:
sb.WriteString("⚡ ")
default:
sb.WriteString("TODO ")
}
}
if opts.Type {
switch reading.(type) {
case *lmsensors.TempSensor:
sb.WriteString("temp ")
case *lmsensors.FanSensor:
sb.WriteString("fan ")
case *lmsensors.VoltageSensor:
sb.WriteString("volt ")
default:
sb.WriteString("TODO ")
}
}
if opts.Name {
sb.WriteString(label + " ")
}
sb.WriteString(reading.Rendered())
if opts.Unit {
sb.WriteString(reading.Unit())
}
output = append(output, sb.String())
}
fmt.Print(strings.Join(output, ", "))
}