diff --git a/database/graphite/database.go b/database/graphite/database.go index c567c62..396bd72 100644 --- a/database/graphite/database.go +++ b/database/graphite/database.go @@ -35,9 +35,7 @@ func (c Config) Prefix() string { } func Connect(configuration map[string]interface{}) (database.Connection, error) { - var config Config - - config = configuration + config := Config(configuration) con := &Connection{ client: graphigo.Client{ diff --git a/database/graphite/utils.go b/database/graphite/utils.go index 7e678ca..72d65ca 100644 --- a/database/graphite/utils.go +++ b/database/graphite/utils.go @@ -4,7 +4,7 @@ import ( "regexp" ) -var reInvalidChars = regexp.MustCompile("(?i)[^a-z0-9\\-]") +var reInvalidChars = regexp.MustCompile(`(?i)[^a-z0-9\\-]`) func replaceInvalidChars(name string) string { return reInvalidChars.ReplaceAllString(name, "_") diff --git a/database/influxdb/database.go b/database/influxdb/database.go index 3feb153..25cb132 100644 --- a/database/influxdb/database.go +++ b/database/influxdb/database.go @@ -6,7 +6,7 @@ import ( "github.com/bdlm/log" "github.com/influxdata/influxdb1-client/models" - "github.com/influxdata/influxdb1-client/v2" + client "github.com/influxdata/influxdb1-client/v2" "github.com/FreifunkBremen/yanic/database" ) @@ -62,8 +62,7 @@ func init() { database.RegisterAdapter("influxdb", Connect) } func Connect(configuration map[string]interface{}) (database.Connection, error) { - var config Config - config = configuration + config := Config(configuration) // Make client c, err := client.NewHTTPClient(client.HTTPConfig{ diff --git a/database/influxdb/node.go b/database/influxdb/node.go index 73e3dee..0657263 100644 --- a/database/influxdb/node.go +++ b/database/influxdb/node.go @@ -2,6 +2,7 @@ package influxdb import ( "fmt" + "log" "strconv" "time" @@ -15,7 +16,9 @@ import ( func (conn *Connection) PruneNodes(deleteAfter time.Duration) { for _, measurement := range []string{MeasurementNode, MeasurementLink} { query := fmt.Sprintf("delete from %s where time < now() - %ds", measurement, deleteAfter/time.Second) - conn.client.Query(client.NewQuery(query, conn.config.Database(), "m")) + if _, err := conn.client.Query(client.NewQuery(query, conn.config.Database(), "m")); err != nil { + log.Println(err) + } } } @@ -197,6 +200,4 @@ func (conn *Connection) InsertNode(node *runtime.Node) { conn.addPoint(MeasurementDHCP, tags, fields, time) } - - return } diff --git a/database/logging/file.go b/database/logging/file.go index c9745b0..31167d9 100644 --- a/database/logging/file.go +++ b/database/logging/file.go @@ -7,6 +7,7 @@ package logging */ import ( "fmt" + "log" "os" "time" @@ -31,8 +32,7 @@ func init() { } func Connect(configuration map[string]interface{}) (database.Connection, error) { - var config Config - config = configuration + config := Config(configuration) file, err := os.OpenFile(config.Path(), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) if err != nil { @@ -63,6 +63,8 @@ func (conn *Connection) Close() { } func (conn *Connection) log(v ...interface{}) { - fmt.Println(v...) - conn.file.WriteString(fmt.Sprintln("[", time.Now().String(), "]", v)) + _, err := fmt.Fprintf(conn.file, "[%s] %v", time.Now().String(), v) + if err != nil { + log.Println(err) + } } diff --git a/database/respondd/main.go b/database/respondd/main.go index 1f38c2c..f47ab7a 100644 --- a/database/respondd/main.go +++ b/database/respondd/main.go @@ -38,8 +38,7 @@ func init() { } func Connect(configuration map[string]interface{}) (database.Connection, error) { - var config Config - config = configuration + config := Config(configuration) conn, err := net.Dial(config.Type(), config.Address()) if err != nil { diff --git a/output/filter/blocklist/blocklist_test.go b/output/filter/blocklist/blocklist_test.go index 89b5d38..f2253f0 100644 --- a/output/filter/blocklist/blocklist_test.go +++ b/output/filter/blocklist/blocklist_test.go @@ -14,9 +14,11 @@ func TestFilterBlocklist(t *testing.T) { // invalid config filter, err := build(3) assert.Error(err) + assert.Nil(filter) filter, err = build([]interface{}{2, "a"}) assert.Error(err) + assert.Nil(filter) // tests with empty list filter, err = build([]interface{}{}) diff --git a/output/filter/domainappendsite/domainappendsite_test.go b/output/filter/domainappendsite/domainappendsite_test.go index c99e8f7..3cadbe2 100644 --- a/output/filter/domainappendsite/domainappendsite_test.go +++ b/output/filter/domainappendsite/domainappendsite_test.go @@ -12,11 +12,12 @@ func TestFilter(t *testing.T) { assert := assert.New(t) // invalid config - filter, err := build("nope") + _, err := build("nope") assert.Error(err) // delete owner by configuration - filter, _ = build(true) + filter, err := build(true) + assert.NoError(err) n := filter.Apply(&runtime.Node{Nodeinfo: &data.Nodeinfo{ System: data.System{ SiteCode: "ffhb", diff --git a/output/filter/domainassite/domainassite_test.go b/output/filter/domainassite/domainassite_test.go index c17e8bf..fe5a903 100644 --- a/output/filter/domainassite/domainassite_test.go +++ b/output/filter/domainassite/domainassite_test.go @@ -12,11 +12,12 @@ func TestFilter(t *testing.T) { assert := assert.New(t) // invalid config - filter, err := build("nope") + _, err := build("nope") assert.Error(err) // delete owner by configuration - filter, _ = build(true) + filter, err := build(true) + assert.NoError(err) n := filter.Apply(&runtime.Node{Nodeinfo: &data.Nodeinfo{ System: data.System{ SiteCode: "ffhb", diff --git a/output/filter/filter.go b/output/filter/filter.go index c83ec7c..f18385e 100644 --- a/output/filter/filter.go +++ b/output/filter/filter.go @@ -38,7 +38,7 @@ func New(configs map[string]interface{}) (set Set, errs []error) { return } - f, _ := filters[name] + f := filters[name] if f == nil { errs = append(errs, fmt.Errorf("unknown filter: %s", name)) } else if filter, err := f(config); err != nil { diff --git a/output/filter/haslocation/haslocation_test.go b/output/filter/haslocation/haslocation_test.go index 2f37b28..3770c07 100644 --- a/output/filter/haslocation/haslocation_test.go +++ b/output/filter/haslocation/haslocation_test.go @@ -12,11 +12,11 @@ func TestFilterHasLocation(t *testing.T) { assert := assert.New(t) // invalid config - filter, err := build(3) + _, err := build(3) assert.Error(err) // test to drop nodes without location - filter, err = build(true) + filter, err := build(true) assert.NoError(err) // node has location (with 0,0) -> keep it diff --git a/output/filter/noowner/noowner_test.go b/output/filter/noowner/noowner_test.go index 73a1f5f..b5efc86 100644 --- a/output/filter/noowner/noowner_test.go +++ b/output/filter/noowner/noowner_test.go @@ -12,11 +12,12 @@ func TestFilter(t *testing.T) { assert := assert.New(t) // invalid config - filter, err := build("nope") + _, err := build("nope") assert.Error(err) // delete owner by configuration - filter, _ = build(true) + filter, err := build(true) + assert.NoError(err) n := filter.Apply(&runtime.Node{Nodeinfo: &data.Nodeinfo{ Owner: &data.Owner{ Contact: "blub", diff --git a/output/filter/site/site_test.go b/output/filter/site/site_test.go index 51344ef..903a2b8 100644 --- a/output/filter/site/site_test.go +++ b/output/filter/site/site_test.go @@ -12,13 +12,13 @@ func TestFilterSite(t *testing.T) { assert := assert.New(t) // invalid config - filter, err := build("ffhb") + _, err := build("ffhb") assert.Error(err) - filter, err = build([]interface{}{3, "ffhb"}) + _, err = build([]interface{}{3, "ffhb"}) assert.Error(err) - filter, err = build([]interface{}{"ffhb"}) + filter, err := build([]interface{}{"ffhb"}) assert.NoError(err) // wronge node diff --git a/output/geojson/geojson_test.go b/output/geojson/geojson_test.go index b73196b..b9a1382 100644 --- a/output/geojson/geojson_test.go +++ b/output/geojson/geojson_test.go @@ -9,11 +9,6 @@ import ( "github.com/FreifunkBremen/yanic/runtime" ) -const ( - testNodeDescription string = "Online\nClients: 42\nModel: TP-Link 841\n" + - "Site: mysite\nDomain: domain_42\n" -) - func TestTransform(t *testing.T) { testNodes := createTestNodes() nodes := transform(testNodes) @@ -41,7 +36,7 @@ func TestTransform(t *testing.T) { nodePoint.Properties["clients"], ) assert.Equal( - testNodeDescription, + "Online\nClients: 42\nModel: TP-Link 841\nSite: mysite\nDomain: domain_42\n", nodePoint.Properties["description"], ) } diff --git a/output/geojson/output.go b/output/geojson/output.go index d3c1068..620ba6b 100644 --- a/output/geojson/output.go +++ b/output/geojson/output.go @@ -26,8 +26,7 @@ func init() { } func Register(configuration map[string]interface{}) (output.Output, error) { - var config Config - config = configuration + config := Config(configuration) if path := config.Path(); path != "" { return &Output{ diff --git a/output/meshviewer-ffrgb/meshviewer_test.go b/output/meshviewer-ffrgb/meshviewer_test.go index b73ddb6..2715aea 100644 --- a/output/meshviewer-ffrgb/meshviewer_test.go +++ b/output/meshviewer-ffrgb/meshviewer_test.go @@ -165,8 +165,6 @@ func TestTransform(t *testing.T) { assert.Equal("node:b:mac:lan", link.TargetAddress) assert.Equal(float32(0.2), link.SourceTQ) assert.Equal(float32(0), link.TargetTQ) - break - case "node:a:mac:wifi": assert.Equal("wifi", link.Type) assert.Equal("node:b:mac:wifi", link.TargetAddress) @@ -177,7 +175,6 @@ func TestTransform(t *testing.T) { assert.Equal("node:c:mac:lan", link.TargetAddress) assert.Equal(float32(0.8), link.SourceTQ) assert.Equal(float32(0.4), link.TargetTQ) - break default: assert.False(true, "invalid link.SourceAddress found") } diff --git a/output/meshviewer-ffrgb/output.go b/output/meshviewer-ffrgb/output.go index ad6ab43..2a673b9 100644 --- a/output/meshviewer-ffrgb/output.go +++ b/output/meshviewer-ffrgb/output.go @@ -26,8 +26,7 @@ func init() { } func Register(configuration map[string]interface{}) (output.Output, error) { - var config Config - config = configuration + config := Config(configuration) if path := config.Path(); path != "" { return &Output{ diff --git a/output/meshviewer/output.go b/output/meshviewer/output.go index b37e7de..e99d04b 100644 --- a/output/meshviewer/output.go +++ b/output/meshviewer/output.go @@ -45,8 +45,7 @@ func init() { } func Register(configuration map[string]interface{}) (output.Output, error) { - var config Config - config = configuration + config := Config(configuration) builder := nodeFormats[config.Version()] if builder == nil { diff --git a/output/nodelist/output.go b/output/nodelist/output.go index 3ed9eb7..53b9e32 100644 --- a/output/nodelist/output.go +++ b/output/nodelist/output.go @@ -26,8 +26,7 @@ func init() { } func Register(configuration map[string]interface{}) (output.Output, error) { - var config Config - config = configuration + config := Config(configuration) if path := config.Path(); path != "" { return &Output{ diff --git a/output/raw-jsonl/output.go b/output/raw-jsonl/output.go index 9334da1..64ecd84 100644 --- a/output/raw-jsonl/output.go +++ b/output/raw-jsonl/output.go @@ -26,8 +26,7 @@ func init() { } func Register(configuration map[string]interface{}) (output.Output, error) { - var config Config - config = configuration + config := Config(configuration) if path := config.Path(); path != "" { return &Output{ diff --git a/output/raw-jsonl/raw_jsonl_test.go b/output/raw-jsonl/raw_jsonl_test.go index 2ea6aed..190939e 100644 --- a/output/raw-jsonl/raw_jsonl_test.go +++ b/output/raw-jsonl/raw_jsonl_test.go @@ -9,11 +9,6 @@ import ( "github.com/FreifunkBremen/yanic/runtime" ) -const ( - testNodeDescription string = "Online\nClients: 42\nModel: TP-Link 841\n" + - "Site: mysite\nDomain: domain_42\n" -) - func TestTransform(t *testing.T) { testNodes := createTestNodes() result := transform(testNodes) diff --git a/output/raw/output.go b/output/raw/output.go index 9c5fcbc..5103584 100644 --- a/output/raw/output.go +++ b/output/raw/output.go @@ -26,8 +26,7 @@ func init() { } func Register(configuration map[string]interface{}) (output.Output, error) { - var config Config - config = configuration + config := Config(configuration) if path := config.Path(); path != "" { return &Output{ diff --git a/respond/collector.go b/respond/collector.go index 79472b1..8ba6eab 100644 --- a/respond/collector.go +++ b/respond/collector.go @@ -89,7 +89,9 @@ func (coll *Collector) listenUDP(iface InterfaceConfig) { if err != nil { log.Panic(err) } - conn.SetReadBuffer(MaxDataGramSize) + if err := conn.SetReadBuffer(MaxDataGramSize); err != nil { + log.Println("failed to set read buffer:", err) + } raw, err := conn.SyscallConn() if err != nil { diff --git a/rrd/rrd.go b/rrd/rrd.go index 9131193..ff92e5f 100644 --- a/rrd/rrd.go +++ b/rrd/rrd.go @@ -13,7 +13,7 @@ import ( "github.com/bdlm/log" ) -var linePattern = regexp.MustCompile("^ ([^<]+)([^<]+)") +var linePattern = regexp.MustCompile(`^ ([^<]+)([^<]+)`) // Dataset a timestemp with values (node and clients) type Dataset struct { diff --git a/runtime/nodes.go b/runtime/nodes.go index 55d871d..082631e 100644 --- a/runtime/nodes.go +++ b/runtime/nodes.go @@ -56,7 +56,7 @@ func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) *Node { now := jsontime.Now() nodes.Lock() - node, _ := nodes.List[nodeID] + node := nodes.List[nodeID] if node == nil { node = &Node{ @@ -213,7 +213,7 @@ func (nodes *Nodes) readIfaces(nodeinfo *data.Nodeinfo, warning bool) { if addr == "" { continue } - if oldNodeID, _ := nodes.ifaceToNodeID[addr]; oldNodeID != nodeID { + if oldNodeID := nodes.ifaceToNodeID[addr]; oldNodeID != nodeID { if oldNodeID != "" && warning { log.Warnf("override nodeID from %s to %s on MAC address %s", oldNodeID, nodeID, addr) } diff --git a/runtime/nodes_test.go b/runtime/nodes_test.go index a6c6c1d..e572840 100644 --- a/runtime/nodes_test.go +++ b/runtime/nodes_test.go @@ -135,7 +135,7 @@ func TestSelectNodes(t *testing.T) { }) assert.Len(selectedNodes, 1) time := jsontime.Time{} - time.UnmarshalJSON([]byte("2017-03-10T12:12:01")) + assert.NoError(time.UnmarshalJSON([]byte("\"2017-03-10T12:12:01\""))) assert.Equal(time, selectedNodes[0].Firstseen) }