From 5508b0dff393d0347444720aa3b2ce7120f45712 Mon Sep 17 00:00:00 2001 From: cn Date: Sun, 6 Dec 2020 13:18:48 +0100 Subject: [PATCH] miflorad: fix some linter errors --- cmd/miflorad/main.go | 32 ++++++++++++++------------------ cmd/miflorad/version.go | 12 ++++++++++++ common/misc_test.go | 12 ++++++++++++ 3 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 cmd/miflorad/version.go create mode 100644 common/misc_test.go diff --git a/cmd/miflorad/main.go b/cmd/miflorad/main.go index e2b2dd2..9b0dc89 100644 --- a/cmd/miflorad/main.go +++ b/cmd/miflorad/main.go @@ -21,9 +21,6 @@ import ( const mqttConnectTimeout = 10 * time.Second -// program version, will be populated on build -var version string - var ( scanTimeout = flag.Duration("scantimeout", 10*time.Second, "timeout after that a scan per peripheral will be aborted") readRetries = flag.Int("readretries", 2, "number of times reading will be attempted per peripheral") @@ -94,21 +91,14 @@ func checkTooShortInterval() error { numPeripherals := int64(len(flag.Args())) numReadRetries := int64(*readRetries) if (*scanTimeout).Nanoseconds()*numReadRetries*numPeripherals >= (*interval).Nanoseconds() { - return errors.New(fmt.Sprintf( - "The interval of %s is too short given the scan timeout of %s for %d peripheral(s) with %d retries each! Exiting...\n", - *interval, *scanTimeout, numPeripherals, *readRetries)) + return errors.Errorf( + "The interval of %s is too short given the scan timeout of %s "+ + "for %d peripheral(s) with %d retries each! Exiting...\n", + *interval, *scanTimeout, numPeripherals, *readRetries) } return nil } -func getVersion() string { - if version == "" { - return "dev" - } else { - return version - } -} - func getMQTTOptions() *mqtt.ClientOptions { if *brokerUseTLS { return mqtt.NewClientOptions(). @@ -156,7 +146,7 @@ func connectPeripheral(peripheral *peripheral, send chan mifloraMetric) error { foundAdvertisementChannel := make(chan ble.Advertisement, 1) filter := func(adv ble.Advertisement) bool { - if strings.ToUpper(adv.Addr().String()) == strings.ToUpper(peripheral.id) { + if strings.EqualFold(adv.Addr().String(), peripheral.id) { foundAdvertisementChannel <- adv return true } @@ -194,7 +184,7 @@ func connectPeripheral(peripheral *peripheral, send chan mifloraMetric) error { timeReadoutTook := time.Since(timeReadoutStart).Seconds() - client.CancelConnection() + err3 := client.CancelConnection() <-done @@ -202,6 +192,10 @@ func connectPeripheral(peripheral *peripheral, send chan mifloraMetric) error { return errors.Wrap(err2, "can't read data") } + if err3 != nil { + return errors.Wrap(err3, "can't disconnect after reading data") + } + send <- mifloraDataMetric{ peripheralId: common.MifloraGetAlphaNumericID(peripheral.id), sensorData: sensorData, @@ -253,7 +247,8 @@ func readAllPeripherals(quit chan struct{}, send chan mifloraMetric) { func main() { flag.Parse() if len(flag.Args()) < 1 { - fmt.Fprintf(os.Stderr, "Usage: %s [options] peripheral-id [peripheral-ids...] \n", os.Args[0]) + fmt.Fprintf(os.Stderr, + "Usage: %s [options] peripheral-id [peripheral-ids...] \n", os.Args[0]) flag.PrintDefaults() os.Exit(1) } @@ -270,7 +265,8 @@ func main() { case "influx": format = influxFormat default: - fmt.Fprintf(os.Stderr, "Unrecognized publish format %s! Exiting...\n", *publishFormatFlag) + fmt.Fprintf(os.Stderr, "Unrecognized publish format %s! Exiting...\n", + *publishFormatFlag) os.Exit(1) } diff --git a/cmd/miflorad/version.go b/cmd/miflorad/version.go new file mode 100644 index 0000000..ef0cb2b --- /dev/null +++ b/cmd/miflorad/version.go @@ -0,0 +1,12 @@ +package main + +// program version, will be populated on build +var version string + +func getVersion() string { + if version == "" { + return "dev" + } else { + return version + } +} diff --git a/common/misc_test.go b/common/misc_test.go new file mode 100644 index 0000000..e5e9b38 --- /dev/null +++ b/common/misc_test.go @@ -0,0 +1,12 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMessage(t *testing.T) { + assert.Equal(t, "", MifloraGetAlphaNumericID("")) + assert.Equal(t, "1234567890ab", MifloraGetAlphaNumericID("12:34:56:78:90:ab")) +}