From 88849c752164226acd0a89cb5aaf7e1d223c5e68 Mon Sep 17 00:00:00 2001 From: Howard Wu Date: Tue, 2 Apr 2019 11:44:49 +1300 Subject: [PATCH] Applying lint. --- .travis.yml | 4 ++++ map180/map.go | 6 +++--- map180/nzmap/nzmap.go | 3 --- map180/nzmap/nzmap_test.go | 4 +++- metrics/ddog_http.go | 29 +++++++++++++---------------- metrics/ddog_msg.go | 29 +++++++++++++---------------- metrics/timers.go | 15 ++++++--------- metrics/types.go | 4 +++- slink/msrecord.go | 1 + slink/slconn_test.go | 11 ++++++++--- weft/handlers.go | 6 +++--- wgs84/wgs84.go | 3 +-- 12 files changed, 58 insertions(+), 57 deletions(-) diff --git a/.travis.yml b/.travis.yml index f061283..19e6b28 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,9 +7,13 @@ install: - go get github.com/golang/protobuf/proto - go get github.com/golang/groupcache +before_script: + - curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin v1.15.0 + script: - make -C cvendor/libmseed - make -C cvendor/libslink - test -z "$(gofmt -s -l `find . -name "*.go" | egrep -v vendor` | tee /dev/stderr)" - go vet -cgocall=false ./... - go test -v ./... + - golangci-lint run -E gosec # default linters + gosec diff --git a/map180/map.go b/map180/map.go index 321e502..7e828c9 100644 --- a/map180/map.go +++ b/map180/map.go @@ -408,13 +408,13 @@ func (m *map3857) nePolySVG(zoom int, layer int) (string, error) { switch m.crossesCentral { case true: // things to the left of 180. - db.QueryRow(svgPolyQuery, + _ = db.QueryRow(svgPolyQuery, m.llx, m.lly, left3857, m.ury, m.xshift, m.yshift, m.dx, m.dx, zoom, layer, m.region).Scan(&l) // things to the right of 180 and shift them over. - db.QueryRow(svgPolyQuery, + _ = db.QueryRow(svgPolyQuery, right3857, m.lly, m.urx, m.ury, width3857-m.llx, m.yshift, m.dx, m.dx, zoom, layer, m.region).Scan(&r) case false: - db.QueryRow(svgPolyQuery, + _ = db.QueryRow(svgPolyQuery, m.llx, m.lly, m.urx, m.ury, m.xshift, m.yshift, m.dx, m.dx, zoom, layer, m.region).Scan(&l) } diff --git a/map180/nzmap/nzmap.go b/map180/nzmap/nzmap.go index 7bd9c68..68d32e0 100644 --- a/map180/nzmap/nzmap.go +++ b/map180/nzmap/nzmap.go @@ -128,8 +128,6 @@ func (p *Point) Icon(b *bytes.Buffer) { p.x = -1000 p.y = -1000 } - - return } /* @@ -277,5 +275,4 @@ func (pts Points) Medium(b *bytes.Buffer) { } b.WriteString(nzMedium) } - return } diff --git a/map180/nzmap/nzmap_test.go b/map180/nzmap/nzmap_test.go index b5edc7a..2d248f1 100644 --- a/map180/nzmap/nzmap_test.go +++ b/map180/nzmap/nzmap_test.go @@ -10,7 +10,9 @@ import ( // these tests also output SVG to svg_test/ for visual inspection. func init() { - os.Mkdir("svg_test", 0755) + if err := os.Mkdir("svg_test", 0755); err != nil { + fmt.Println("Error creating svg_test directory", err) + } } func TestIconWellington(t *testing.T) { diff --git a/metrics/ddog_http.go b/metrics/ddog_http.go index 64510f1..01277ab 100644 --- a/metrics/ddog_http.go +++ b/metrics/ddog_http.go @@ -29,23 +29,20 @@ func DataDogHttp(apiKey, hostName, appName string, logger Logger) { ticker := time.NewTicker(time.Second * 60).C var err error - for { - select { - case <-ticker: - ReadHttpCounters(&c) - runtime.ReadMemStats(&m) - - if apiKey != "" { - err = dogHttp(apiKey, hostName, appName, m, ReadTimers(), c) - if err != nil { - logger.Printf("error sending metrics to datadog for %s %s %s", hostName, appName, err.Error()) - } - } else { - logger.Printf("%s %s", hostName, appName) - logger.Printf("%+v", m) - logger.Printf("%+v", ReadTimers()) - logger.Printf("%+v", c) + for range ticker { + ReadHttpCounters(&c) + runtime.ReadMemStats(&m) + + if apiKey != "" { + err = dogHttp(apiKey, hostName, appName, m, ReadTimers(), c) + if err != nil { + logger.Printf("error sending metrics to datadog for %s %s %s", hostName, appName, err.Error()) } + } else { + logger.Printf("%s %s", hostName, appName) + logger.Printf("%+v", m) + logger.Printf("%+v", ReadTimers()) + logger.Printf("%+v", c) } } }() diff --git a/metrics/ddog_msg.go b/metrics/ddog_msg.go index 797e384..e9f604c 100644 --- a/metrics/ddog_msg.go +++ b/metrics/ddog_msg.go @@ -61,23 +61,20 @@ func DataDogMsg(apiKey, hostName, appName string, logger Logger) { ticker := time.NewTicker(time.Second * 60).C var err error - for { - select { - case <-ticker: - ReadMsgCounters(&c) - runtime.ReadMemStats(&m) - - if apiKey != "" { - err = dogMsg(apiKey, hostName, appName, m, ReadTimers(), c) - if err != nil { - logger.Printf("error sending metrics to datadog for %s %s %s", hostName, appName, err.Error()) - } - } else { - logger.Printf("%s %s", hostName, appName) - logger.Printf("%+v", m) - logger.Printf("%+v", ReadTimers()) - logger.Printf("%+v", c) + for range ticker { + ReadMsgCounters(&c) + runtime.ReadMemStats(&m) + + if apiKey != "" { + err = dogMsg(apiKey, hostName, appName, m, ReadTimers(), c) + if err != nil { + logger.Printf("error sending metrics to datadog for %s %s %s", hostName, appName, err.Error()) } + } else { + logger.Printf("%s %s", hostName, appName) + logger.Printf("%+v", m) + logger.Printf("%+v", ReadTimers()) + logger.Printf("%+v", c) } } }() diff --git a/metrics/timers.go b/metrics/timers.go index cef7196..f6415ed 100644 --- a/metrics/timers.go +++ b/metrics/timers.go @@ -41,15 +41,12 @@ func init() { timers = make(chan Timer, 300) go func() { - for { - select { - case m := <-timers: - agg.m.Lock() - agg.count[m.id]++ - agg.sum[m.id] += m.taken - agg.taken[m.id] = append(agg.taken[m.id], m.taken) - agg.m.Unlock() - } + for m := range timers { + agg.m.Lock() + agg.count[m.id]++ + agg.sum[m.id] += m.taken + agg.taken[m.id] = append(agg.taken[m.id], m.taken) + agg.m.Unlock() } }() } diff --git a/metrics/types.go b/metrics/types.go index d5a4ae4..278a735 100644 --- a/metrics/types.go +++ b/metrics/types.go @@ -9,7 +9,9 @@ type Processor interface { func DoProcess(m Processor, b []byte) error { MsgRx() t := Start() - defer t.Track("process") + defer func() { + _ = t.Track("process") // we keep this package simple thus not reporting this error + }() err := m.Process(b) diff --git a/slink/msrecord.go b/slink/msrecord.go index 590f3bb..691fd2e 100644 --- a/slink/msrecord.go +++ b/slink/msrecord.go @@ -33,6 +33,7 @@ func (r *SLMSRecord) DNomSampRate() float64 { return float64(C.sl_msr_dnomsamprate((*_Ctype_struct_SLMSrecord_s)(r))) } +//nolint:unused // reserved for future use func (r *SLMSRecord) sl_DEpochSTime() float64 { return float64(C.sl_msr_depochstime((*_Ctype_struct_SLMSrecord_s)(r))) } diff --git a/slink/slconn_test.go b/slink/slconn_test.go index 4ba9184..5390b24 100644 --- a/slink/slconn_test.go +++ b/slink/slconn_test.go @@ -71,7 +71,9 @@ func TestReadStreamList(t *testing.T) { t.Fatal("unable to open temporary file") } defer os.Remove(tf.Name()) - ioutil.WriteFile(tf.Name(), ([]byte)("# A comment\nGE ISP BH?.D\nNL HGN\nMN AGU BH? HH?\n"), 0644) + if err = ioutil.WriteFile(tf.Name(), ([]byte)("# A comment\nGE ISP BH?.D\nNL HGN\nMN AGU BH? HH?\n"), 0644); err != nil { + t.Errorf("unable to write file: %v", err) + } count, err := slconn.ReadStreamList(tf.Name(), "") if err != nil { t.Error("unable to read stream list, invalid format") @@ -153,7 +155,7 @@ func TestParseStreamList(t *testing.T) { if count != 3 { t.Error("unable to parse stream list, wrong count") } - count, err = slconn.ParseStreamList("IU__KONO:BHE BHN,GE_WLF,MN_AQU:HH?.D", "") + _, err = slconn.ParseStreamList("IU__KONO:BHE BHN,GE_WLF,MN_AQU:HH?.D", "") if err == nil { t.Error("shouldn't be able to parse stream list, invalid string") } @@ -174,7 +176,10 @@ func TestLogInitErrorMessage(t *testing.T) { t.Fatal("unable to create new SLCD") } - slconn.ParseStreamList("IU__KONO:BHE BHN,GE_WLF,MN_AQU:HH?.D", "") + if _, err := slconn.ParseStreamList("IU__KONO:BHE BHN,GE_WLF,MN_AQU:HH?.D", ""); err == nil { + t.Error("shouldn't be able to parse stream list, invalid string") + } + if ans := "not in NET_STA format: IU__KONO"; msg != ans { t.Errorf("error message: expected \"%s\", received \"%s\"", ans, msg) } diff --git a/weft/handlers.go b/weft/handlers.go index 8f104d2..f60d110 100644 --- a/weft/handlers.go +++ b/weft/handlers.go @@ -232,7 +232,9 @@ func MakeHandler(rh RequestHandler, eh ErrorHandler) http.HandlerFunc { metrics.Written(n) name := name(rh) - t.Track(name + "." + r.Method) + if e := t.Track(name + "." + r.Method); e != nil { + logger.Printf("Track error: %s", e.Error()) + } metrics.Request() @@ -284,7 +286,6 @@ func TextError(e error, h http.Header, b *bytes.Buffer) error { h.Set("Surrogate-Control", "max-age=86400") _, err = b.WriteString("this resource no longer exists") h.Set("Content-Type", "text/plain; charset=utf-8") - return nil case http.StatusNotFound: b.Reset() h.Set("Surrogate-Control", "max-age=10") @@ -367,7 +368,6 @@ func HTMLError(e error, h http.Header, b *bytes.Buffer) error { h.Set("Surrogate-Control", "max-age=86400") _, err = b.Write([]byte(ErrGone)) h.Set("Content-Type", "text/html; charset=utf-8") - return nil case http.StatusNotFound: b.Reset() h.Set("Surrogate-Control", "max-age=10") diff --git a/wgs84/wgs84.go b/wgs84/wgs84.go index 63e61a7..f612144 100644 --- a/wgs84/wgs84.go +++ b/wgs84/wgs84.go @@ -61,7 +61,7 @@ func DistanceBearing(lat1, lon1, lat2, lon2 float64) (distance, bearing float64, var c2a, c, cx, cy, cz, d, del, e, sx, sy, y float64 - for true { + for { sx = math.Sin(x) cx = math.Cos(x) tu1 = cu2 * sx @@ -100,7 +100,6 @@ func DistanceBearing(lat1, lon1, lat2, lon2 float64) (distance, bearing float64, } faz = math.Atan2(tu1, tu2) - baz = math.Atan2(cu1*sx, (baz*cx-su1*cu2)) + pi x = math.Sqrt(((1.0/(r*r))-1.0)*c2a+1.0) + 1.0 x = (x - 2.0) / x c = 1.0 - x