diff --git a/go.mod b/go.mod index bf4b022..1d6ecd3 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,12 @@ module github.com/dkorunic/iSMC -go 1.19 +go 1.20 require ( - github.com/fvbommel/sortorder v1.0.2 + github.com/fvbommel/sortorder v1.1.0 github.com/json-iterator/go v1.1.12 github.com/panotza/gosmc v1.0.0 - github.com/spf13/cobra v1.6.1 + github.com/spf13/cobra v1.7.0 ) require ( @@ -19,10 +19,10 @@ require ( require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/jedib0t/go-pretty/v6 v6.4.4 + github.com/jedib0t/go-pretty/v6 v6.4.6 github.com/mattn/go-runewidth v0.0.14 // indirect - github.com/rivo/uniseg v0.4.3 // indirect + github.com/rivo/uniseg v0.4.4 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/stretchr/testify v1.8.1 - golang.org/x/sys v0.4.0 // indirect + github.com/stretchr/testify v1.8.4 + golang.org/x/sys v0.9.0 // indirect ) diff --git a/go.sum b/go.sum index 4046336..da5b383 100644 --- a/go.sum +++ b/go.sum @@ -2,14 +2,13 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fvbommel/sortorder v1.0.2 h1:mV4o8B2hKboCdkJm+a7uX/SIpZob4JzUpc5GGnM45eo= -github.com/fvbommel/sortorder v1.0.2/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0= +github.com/fvbommel/sortorder v1.1.0 h1:fUmoe+HLsBTctBDoaBwpQo5N+nrCp8g/BjKb/6ZQmYw= +github.com/fvbommel/sortorder v1.1.0/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/jedib0t/go-pretty/v6 v6.4.4 h1:N+gz6UngBPF4M288kiMURPHELDMIhF/Em35aYuKrsSc= -github.com/jedib0t/go-pretty/v6 v6.4.4/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI= +github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw= +github.com/jedib0t/go-pretty/v6 v6.4.6/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= @@ -26,25 +25,23 @@ github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdL github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= -github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= +github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= -github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= -golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= +golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hid/get.go b/hid/get.go index dff1132..ce3d13a 100644 --- a/hid/get.go +++ b/hid/get.go @@ -192,8 +192,8 @@ import ( ) // GetAll returns all detected HID sensor results. -func GetAll() map[string]interface{} { - sensors := make(map[string]interface{}) +func GetAll() map[string]any { + sensors := make(map[string]any) sensors["Current"] = GetCurrent() sensors["Temperature"] = GetTemperature() @@ -203,7 +203,7 @@ func GetAll() map[string]interface{} { } // GetCurrent returns detected HID current sensor results. -func GetCurrent() map[string]interface{} { +func GetCurrent() map[string]any { cStr := C.getCurrents() defer C.free(unsafe.Pointer(cStr)) @@ -211,7 +211,7 @@ func GetCurrent() map[string]interface{} { } // GetVoltage returns detected HID voltage sensor results. -func GetVoltage() map[string]interface{} { +func GetVoltage() map[string]any { cStr := C.getVoltages() defer C.free(unsafe.Pointer(cStr)) @@ -219,7 +219,7 @@ func GetVoltage() map[string]interface{} { } // GetTemp returns detected HID temperature sensor results. -func GetTemperature() map[string]interface{} { +func GetTemperature() map[string]any { cStr := C.getThermals() defer C.free(unsafe.Pointer(cStr)) diff --git a/output/influxoutput.go b/output/influxoutput.go index 6d39f5a..f4d87e6 100644 --- a/output/influxoutput.go +++ b/output/influxoutput.go @@ -49,7 +49,7 @@ func (io InfluxOutput) All() { for _, key := range keys { value := all[key] - if smcdata, ok := value.(map[string]interface{}); ok { + if smcdata, ok := value.(map[string]any); ok { io.print(key, smcdata) } } @@ -80,7 +80,7 @@ func (io InfluxOutput) Voltage() { } func influx_string_convert(s string) string { - s = strings.Replace(s, " ", "_", -1) + s = strings.ReplaceAll(s, " ", "_") s = strings.ToLower(s) return s } @@ -94,7 +94,7 @@ func influx_get_unit(s string) string { s = strings.Trim(s, "value=") if len(strings.Split(s, " ")) > 1 { s = strings.Split(s, " ")[1] - s = strings.Replace(s, "°", "", -1) + s = strings.ReplaceAll(s, "°", "") s = strings.ToLower(s) } else { s = "none" @@ -102,7 +102,7 @@ func influx_get_unit(s string) string { return s } -func (io InfluxOutput) print(name string, smcdata map[string]interface{}) { +func (io InfluxOutput) print(name string, smcdata map[string]any) { if len(smcdata) != 0 { ct := time.Now().UnixNano() @@ -113,7 +113,7 @@ func (io InfluxOutput) print(name string, smcdata map[string]interface{}) { for _, k := range keys { v := smcdata[k] - if value, ok := v.(map[string]interface{}); ok { + if value, ok := v.(map[string]any); ok { key := fmt.Sprint(value["key"]) if len(key) > 0 { key = influx_string_convert(fmt.Sprintf(",key=%s", value["key"])) diff --git a/output/jsonoutput.go b/output/jsonoutput.go index 1529e1a..736054e 100644 --- a/output/jsonoutput.go +++ b/output/jsonoutput.go @@ -17,7 +17,6 @@ package output import ( - "encoding/json" "fmt" "io" "os" @@ -47,59 +46,63 @@ type newstruct struct { } func format(d any) (any, error) { - v := d.(map[string]any) - var err error + v, ok := d.(map[string]any) + if !ok { + return v, fmt.Errorf("not a map") + } + + json := jsoniter.ConfigCompatibleWithStandardLibrary + for key, entry := range v { - t := make([]byte, 0) - buf := new(newstruct) - s := make([]string, 0) - t, err = json.Marshal(entry) + t, err := json.Marshal(entry) if err != nil { - fmt.Println("json.Marshal error:", err) - break + return v, err } - err = json.Unmarshal(t, buf) + + buf := newstruct{} + err = json.Unmarshal(t, &buf) if err != nil { - fmt.Println("json.Unmarshal error:", err) - break + return v, err } + // Process only string values switch buf.Value.(type) { case string: if !strings.Contains(buf.Value.(string), " ") { continue } - s = strings.Split(buf.Value.(string), " ") + s := strings.Split(buf.Value.(string), " ") switch buf.Type { - case "flt", "sp78", "sp87": + case "flt", "sp78", "sp87", "hid": f, err := strconv.ParseFloat(s[0], 64) if err != nil { - break + return v, err } + buf.Quantity = f buf.Unit = s[1] } } + v[key] = buf } - return v, err + return v, nil } func (jo JSONOutput) All() { var err error - data := make(map[string]any) - data = GetAll() + data := GetAll() for key, d := range data { if data[key], err = format(d); err != nil { - fmt.Printf("Convert error:%v\n", err) jo.print(GetAll()) return } } + json := jsoniter.ConfigCompatibleWithStandardLibrary - out, _ := json.MarshalIndent(data, "", " ") - fmt.Fprint(jo.writer, string(out)) + out, _ := json.Marshal(data) + fmt.Println(string(out)) } func (jo JSONOutput) Battery() { @@ -129,10 +132,11 @@ func (jo JSONOutput) Voltage() { func (jo JSONOutput) print(v any) { data, err := format(v) if err != nil { - fmt.Println("Convert error:", v) + fmt.Printf("could not format data: %v\n", err) return } + json := jsoniter.ConfigCompatibleWithStandardLibrary - out, _ := json.MarshalIndent(data, "", " ") - fmt.Fprint(jo.writer, string(out)) + out, _ := json.Marshal(data) + fmt.Println(string(out)) } diff --git a/output/jsonoutput_test.go b/output/jsonoutput_test.go index 0b5b908..537a914 100644 --- a/output/jsonoutput_test.go +++ b/output/jsonoutput_test.go @@ -34,19 +34,19 @@ func TestJSONOutput(t *testing.T) { { "All sensors", func() { - GetAll = func() map[string]interface{} { - return map[string]interface{}{ - "sensor-1": map[string]interface{}{ + GetAll = func() map[string]any { + return map[string]any{ + "sensor-1": map[string]any{ "key": "key", "value": "string", "type": "type", }, - "sensor-2": map[string]interface{}{ + "sensor-2": map[string]any{ "key": "key", "value": true, "type": "type", }, - "sensor-3": map[string]interface{}{ + "sensor-3": map[string]any{ "key": "key", "value": 99, "type": "type", @@ -62,7 +62,7 @@ func TestJSONOutput(t *testing.T) { { "Battery sensor", func() { - GetBattery = func() map[string]interface{} { + GetBattery = func() map[string]any { return getMapForSensor("battery") } }, @@ -74,7 +74,7 @@ func TestJSONOutput(t *testing.T) { { "Current sensor", func() { - GetCurrent = func() map[string]interface{} { + GetCurrent = func() map[string]any { return getMapForSensor("current") } }, @@ -86,7 +86,7 @@ func TestJSONOutput(t *testing.T) { { "Fans sensor", func() { - GetFans = func() map[string]interface{} { + GetFans = func() map[string]any { return getMapForSensor("fans") } }, @@ -98,7 +98,7 @@ func TestJSONOutput(t *testing.T) { { "Power sensor", func() { - GetPower = func() map[string]interface{} { + GetPower = func() map[string]any { return getMapForSensor("power") } }, @@ -110,7 +110,7 @@ func TestJSONOutput(t *testing.T) { { "Temperature sensor", func() { - GetTemperature = func() map[string]interface{} { + GetTemperature = func() map[string]any { return getMapForSensor("temperature") } }, @@ -122,7 +122,7 @@ func TestJSONOutput(t *testing.T) { { "Voltage sensor", func() { - GetVoltage = func() map[string]interface{} { + GetVoltage = func() map[string]any { return getMapForSensor("voltage") } }, diff --git a/output/output.go b/output/output.go index 64ba57b..ebb9d08 100644 --- a/output/output.go +++ b/output/output.go @@ -17,10 +17,9 @@ package output import ( - jsoniter "github.com/json-iterator/go" - "github.com/dkorunic/iSMC/hid" "github.com/dkorunic/iSMC/smc" + jsoniter "github.com/json-iterator/go" ) // monkey patching for testing @@ -51,40 +50,40 @@ type Output interface { Voltage() } -func getAll() map[string]interface{} { +func getAll() map[string]any { return merge(smc.GetAll(), hid.GetAll()) } -func getBattery() map[string]interface{} { +func getBattery() map[string]any { return smc.GetBattery() } -func getCurrent() map[string]interface{} { - merged := make(map[string]interface{}) +func getCurrent() map[string]any { + merged := make(map[string]any) deepCopy(merged, smc.GetCurrent()) deepCopy(merged, hid.GetCurrent()) return merged } -func getFans() map[string]interface{} { +func getFans() map[string]any { return smc.GetFans() } -func getTemperature() map[string]interface{} { - merged := make(map[string]interface{}) +func getTemperature() map[string]any { + merged := make(map[string]any) deepCopy(merged, smc.GetTemperature()) deepCopy(merged, hid.GetTemperature()) return merged } -func getPower() map[string]interface{} { +func getPower() map[string]any { return smc.GetPower() } -func getVoltage() map[string]interface{} { - merged := make(map[string]interface{}) +func getVoltage() map[string]any { + merged := make(map[string]any) deepCopy(merged, smc.GetVoltage()) deepCopy(merged, hid.GetVoltage()) @@ -92,21 +91,21 @@ func getVoltage() map[string]interface{} { } // TODO replace with a variant from an utility package -func deepCopy(dest, src map[string]interface{}) { +func deepCopy(dest, src map[string]any) { json := jsoniter.ConfigCompatibleWithStandardLibrary jsonStr, _ := json.Marshal(src) _ = json.Unmarshal(jsonStr, &dest) } // TODO replace with a variant from an utility package -func merge(a, b map[string]interface{}) map[string]interface{} { - out := make(map[string]interface{}) +func merge(a, b map[string]any) map[string]any { + out := make(map[string]any) deepCopy(out, a) for k, v := range b { - if v, ok := v.(map[string]interface{}); ok { + if v, ok := v.(map[string]any); ok { if bv, ok := out[k]; ok { - if bv, ok := bv.(map[string]interface{}); ok { + if bv, ok := bv.(map[string]any); ok { out[k] = merge(bv, v) continue diff --git a/output/output_test.go b/output/output_test.go index 69dbc90..34ec0df 100644 --- a/output/output_test.go +++ b/output/output_test.go @@ -27,8 +27,8 @@ import ( func Test_deepCopy(t *testing.T) { type args struct { - dest map[string]interface{} - src map[string]interface{} + dest map[string]any + src map[string]any } tests := []struct { name string @@ -38,15 +38,15 @@ func Test_deepCopy(t *testing.T) { { "Verify dest", args{ - dest: map[string]interface{}{ + dest: map[string]any{ "key-1": "value-1", }, - src: map[string]interface{}{ - "key-2": map[string]interface{}{ + src: map[string]any{ + "key-2": map[string]any{ "key-2-1": "value-2-1", }, - "key-3": map[string]interface{}{ - "key-3-1": map[string]interface{}{ + "key-3": map[string]any{ + "key-3-1": map[string]any{ "key-3-1-1": "value-3-1-1", }, }, @@ -57,10 +57,10 @@ func Test_deepCopy(t *testing.T) { { "Verify empty dest", args{ - dest: map[string]interface{}{}, - src: map[string]interface{}{ + dest: map[string]any{}, + src: map[string]any{ "key-1": "value-1", - "key-2": map[string]interface{}{ + "key-2": map[string]any{ "key-2-1": "value-2-1", }, }, @@ -80,42 +80,42 @@ func Test_deepCopy(t *testing.T) { func Test_merge(t *testing.T) { type args struct { - a map[string]interface{} - b map[string]interface{} + a map[string]any + b map[string]any } tests := []struct { name string args args - expected map[string]interface{} + expected map[string]any }{ { "Verify merge", args{ - map[string]interface{}{ + map[string]any{ "key-1": "value-1-a", - "key-3": map[string]interface{}{ - "key-3-2": map[string]interface{}{ + "key-3": map[string]any{ + "key-3-2": map[string]any{ "key-3-2-1": "value-3-2-1-a", }, }, }, - map[string]interface{}{ + map[string]any{ "key-1": "value-2-b", "key-2": "value-2-b", - "key-3": map[string]interface{}{ + "key-3": map[string]any{ "key-3-1": "value-3-1-b", - "key-3-2": map[string]interface{}{ + "key-3-2": map[string]any{ "key-3-2-1": "value-3-2-1-b", }, }, }, }, - map[string]interface{}{ + map[string]any{ "key-1": "value-2-b", "key-2": "value-2-b", - "key-3": map[string]interface{}{ + "key-3": map[string]any{ "key-3-1": "value-3-1-b", - "key-3-2": map[string]interface{}{ + "key-3-2": map[string]any{ "key-3-2-1": "value-3-2-1-b", }, }, @@ -134,15 +134,15 @@ func Test_merge(t *testing.T) { } } -func toJson(src map[string]interface{}) string { +func toJson(src map[string]any) string { json := jsoniter.ConfigCompatibleWithStandardLibrary jsonStr, _ := json.Marshal(src) return string(jsonStr) } -func getMapForSensor(sensorName string) map[string]interface{} { - return map[string]interface{}{ - sensorName: map[string]interface{}{ +func getMapForSensor(sensorName string) map[string]any { + return map[string]any{ + sensorName: map[string]any{ "key": "key", "value": "value", "type": "type", diff --git a/output/outputfactory_test.go b/output/outputfactory_test.go index 7335e06..0903ab1 100644 --- a/output/outputfactory_test.go +++ b/output/outputfactory_test.go @@ -37,7 +37,6 @@ func TestOutputFactory(t *testing.T) { args{outputType: "table"}, NewTableOutput(false), }, { - "Returns JSONOutput for table output type", args{outputType: "json"}, NewJSONOutput(), diff --git a/output/tableoutput.go b/output/tableoutput.go index 51880c4..8058716 100644 --- a/output/tableoutput.go +++ b/output/tableoutput.go @@ -51,7 +51,7 @@ func (to TableOutput) All() { for _, key := range keys { value := all[key] - if smcdata, ok := value.(map[string]interface{}); ok { + if smcdata, ok := value.(map[string]any); ok { to.print(key, smcdata) } } @@ -81,7 +81,7 @@ func (to TableOutput) Voltage() { to.print("Voltage", GetVoltage()) } -func (to TableOutput) print(name string, smcdata map[string]interface{}) { +func (to TableOutput) print(name string, smcdata map[string]any) { if len(smcdata) != 0 { t := table.NewWriter() t.SetOutputMirror(to.writer) @@ -100,8 +100,8 @@ func (to TableOutput) print(name string, smcdata map[string]interface{}) { for _, k := range keys { v := smcdata[k] - if value, ok := v.(map[string]interface{}); ok { - t.AppendRow([]interface{}{ + if value, ok := v.(map[string]any); ok { + t.AppendRow([]any{ fmt.Sprintf("%v", k), value["key"], fmt.Sprintf("%8v", value["value"]), diff --git a/output/tableoutput_test.go b/output/tableoutput_test.go index 15c4072..f772fde 100644 --- a/output/tableoutput_test.go +++ b/output/tableoutput_test.go @@ -52,24 +52,24 @@ func TestTableOutput_ASCII(t *testing.T) { { "All sensors", func() { - GetAll = func() map[string]interface{} { - return map[string]interface{}{ - "battery": map[string]interface{}{ - "sensor": map[string]interface{}{ + GetAll = func() map[string]any { + return map[string]any{ + "battery": map[string]any{ + "sensor": map[string]any{ "key": "key", "value": "value", "type": "type", }, }, - "fans": map[string]interface{}{ - "sensor": map[string]interface{}{ + "fans": map[string]any{ + "sensor": map[string]any{ "key": "key", "value": "value", "type": "type", }, }, - "temperature": map[string]interface{}{ - "sensor": map[string]interface{}{ + "temperature": map[string]any{ + "sensor": map[string]any{ "key": "key", "value": "value", "type": "type", @@ -86,7 +86,7 @@ func TestTableOutput_ASCII(t *testing.T) { { "Battery sensor", func() { - GetBattery = func() map[string]interface{} { + GetBattery = func() map[string]any { return getMapForSensor("sensor") } }, @@ -98,7 +98,7 @@ func TestTableOutput_ASCII(t *testing.T) { { "Current sensor", func() { - GetCurrent = func() map[string]interface{} { + GetCurrent = func() map[string]any { return getMapForSensor("sensor") } }, @@ -110,7 +110,7 @@ func TestTableOutput_ASCII(t *testing.T) { { "Fans sensor", func() { - GetFans = func() map[string]interface{} { + GetFans = func() map[string]any { return getMapForSensor("sensor") } }, @@ -122,7 +122,7 @@ func TestTableOutput_ASCII(t *testing.T) { { "Power sensor", func() { - GetPower = func() map[string]interface{} { + GetPower = func() map[string]any { return getMapForSensor("sensor") } }, @@ -134,7 +134,7 @@ func TestTableOutput_ASCII(t *testing.T) { { "Temperature sensor", func() { - GetTemperature = func() map[string]interface{} { + GetTemperature = func() map[string]any { return getMapForSensor("sensor") } }, @@ -146,7 +146,7 @@ func TestTableOutput_ASCII(t *testing.T) { { "Voltage sensor", func() { - GetVoltage = func() map[string]interface{} { + GetVoltage = func() map[string]any { return getMapForSensor("sensor") } }, @@ -180,17 +180,17 @@ func TestTableOutput_Table(t *testing.T) { { "All sensors", func() { - GetAll = func() map[string]interface{} { - return map[string]interface{}{ - "battery": map[string]interface{}{ - "sensor": map[string]interface{}{ + GetAll = func() map[string]any { + return map[string]any{ + "battery": map[string]any{ + "sensor": map[string]any{ "key": "key", "value": "value", "type": "type", }, }, - "fans": map[string]interface{}{ - "sensor": map[string]interface{}{ + "fans": map[string]any{ + "sensor": map[string]any{ "key": "key", "value": "value", "type": "type", diff --git a/smc/smc.go b/smc/smc.go index dfc65bf..96075a2 100644 --- a/smc/smc.go +++ b/smc/smc.go @@ -45,8 +45,8 @@ type SensorStat struct { //go:generate ./gen-sensors.sh sensors.go -func GetAll() map[string]interface{} { // Get all sensors - sensors := make(map[string]interface{}) +func GetAll() map[string]any { // Get all sensors + sensors := make(map[string]any) sensors["Battery"] = GetBattery() sensors["Current"] = GetCurrent() @@ -58,7 +58,7 @@ func GetAll() map[string]interface{} { // Get all sensors return sensors } -func GetBattery() map[string]interface{} { +func GetBattery() map[string]any { c, res := gosmc.SMCOpen(AppleSMC) if res != gosmc.IOReturnSuccess { fmt.Fprintf(os.Stderr, "Unable to open Apple SMC; return code %v\n", res) @@ -70,18 +70,18 @@ func GetBattery() map[string]interface{} { i, ty2, _ := getKeyUint32(c, BattInf) // Get battery info (needs bit decoding) b, ty3, _ := getKeyBool(c, BattPwr) // Get AC status - battery := map[string]interface{}{ - "Battery Count": map[string]interface{}{ + battery := map[string]any{ + "Battery Count": map[string]any{ "key": BattNum, "value": n, "type": ty1, }, - "Battery Info": map[string]interface{}{ + "Battery Info": map[string]any{ "key": BattInf, "value": i, "type": ty2, }, - "Battery Power": map[string]interface{}{ + "Battery Power": map[string]any{ "key": BattPwr, "value": b, "type": ty3, @@ -91,11 +91,11 @@ func GetBattery() map[string]interface{} { return battery } -func GetCurrent() map[string]interface{} { +func GetCurrent() map[string]any { return getGeneric("Current", "A", AppleCurrent) } -func GetFans() map[string]interface{} { +func GetFans() map[string]any { c, res := gosmc.SMCOpen(AppleSMC) if res != gosmc.IOReturnSuccess { fmt.Fprintf(os.Stderr, "Unable to open Apple SMC; return code %v\n", res) @@ -103,10 +103,10 @@ func GetFans() map[string]interface{} { } defer gosmc.SMCClose(c) - fans := make(map[string]interface{}) + fans := make(map[string]any) val, smcType, _ := getKeyUint32(c, FanNum) // Get number of fans - fans["Fan Count"] = map[string]interface{}{ + fans["Fan Count"] = map[string]any{ "key": FanNum, "value": val, "type": smcType, @@ -126,7 +126,7 @@ func GetFans() map[string]interface{} { if val < 0.0 { val = -val } - fans[desc] = map[string]interface{}{ + fans[desc] = map[string]any{ "key": key, "value": fmt.Sprintf("%4.0f rpm", val), "type": smcType, @@ -138,7 +138,7 @@ func GetFans() map[string]interface{} { return fans } -func getGeneric(desc, unit string, smcSlice []SensorStat) map[string]interface{} { +func getGeneric(desc, unit string, smcSlice []SensorStat) map[string]any { conn, res := gosmc.SMCOpen(AppleSMC) if res != gosmc.IOReturnSuccess { fmt.Fprintf(os.Stderr, "Unable to open Apple SMC; return code %v\n", res) @@ -146,7 +146,7 @@ func getGeneric(desc, unit string, smcSlice []SensorStat) map[string]interface{} } defer gosmc.SMCClose(conn) - generic := make(map[string]interface{}) + generic := make(map[string]any) for _, v := range smcSlice { key := v.Key @@ -168,7 +168,7 @@ func getGeneric(desc, unit string, smcSlice []SensorStat) map[string]interface{} return generic } -func addGeneric(generic map[string]interface{}, conn uint, key, desc, unit string) { +func addGeneric(generic map[string]any, conn uint, key, desc, unit string) { val, smcType, err := getKeyFloat32(conn, key) if err != nil { return @@ -178,7 +178,7 @@ func addGeneric(generic map[string]interface{}, conn uint, key, desc, unit strin if val < 0.0 { val = -val } - generic[desc] = map[string]interface{}{ + generic[desc] = map[string]any{ "key": key, "value": fmt.Sprintf("%.1f %s", val, unit), "type": smcType, @@ -186,14 +186,14 @@ func addGeneric(generic map[string]interface{}, conn uint, key, desc, unit strin } } -func GetPower() map[string]interface{} { +func GetPower() map[string]any { return getGeneric("Power", "W", ApplePower) } -func GetTemperature() map[string]interface{} { +func GetTemperature() map[string]any { return getGeneric("Temperature", "°C", AppleTemp) } -func GetVoltage() map[string]interface{} { +func GetVoltage() map[string]any { return getGeneric("Voltage", "V", AppleVoltage) }