Skip to content

Commit 981e1dc

Browse files
committed
Fixed issues raised by staticcheck
1 parent 4563bfc commit 981e1dc

File tree

5 files changed

+19
-20
lines changed

5 files changed

+19
-20
lines changed

.github/run-tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go get -u honnef.co/go/tools/cmd/staticcheck
77

88
# Run the static-check tool - we ignore documentation-errors.
99
t=$(mktemp)
10-
staticcheck -checks all ./... | grep -v "package comment"> $t
10+
staticcheck -checks all ./... | grep -v "package comment" | grep -v context.TODO > $t
1111
if [ -s $t ]; then
1212
echo "Found errors via 'staticcheck'"
1313
cat $t

alerts/alerts.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func New() (*Alerts, error) {
4747
//
4848
dsn := os.Getenv("PURPLE_DSN")
4949
if dsn == "" {
50-
return m, errors.New("You must specify the environmental variable 'PURPLE_DSN' with your DB details")
50+
return m, errors.New("you must specify the environmental variable 'PURPLE_DSN' with your DB details")
5151
}
5252

5353
//
@@ -147,10 +147,10 @@ func (s *Alerts) AddEvent(data alert.Alert) error {
147147
// Otherwise update the existing event in-place.
148148
//
149149
up, err := s.db.Prepare("UPDATE events SET raise_at=?, subject=?, detail=? WHERE i=?")
150-
defer up.Close()
151150
if err != nil {
152151
return err
153152
}
153+
defer up.Close()
154154

155155
_, err = up.Exec(raise, data.Subject, data.Detail, id)
156156
if err != nil {

cmd_serve.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ func AddContext(next http.Handler) http.Handler {
162162
// in, and supply no context.
163163
//
164164
next.ServeHTTP(w, r)
165-
return
166165
})
167166
}
168167

@@ -352,7 +351,7 @@ func ackEvent(res http.ResponseWriter, req *http.Request) {
352351
//
353352
username := req.Context().Value(keyUser)
354353
if username == nil {
355-
http.Redirect(res, req, "/login", 302)
354+
http.Redirect(res, req, "/login", http.StatusFound)
356355
return
357356
}
358357

@@ -363,7 +362,7 @@ func ackEvent(res http.ResponseWriter, req *http.Request) {
363362
id := vars["id"]
364363

365364
storage.AckEvent(id)
366-
http.Redirect(res, req, "/", 302)
365+
http.Redirect(res, req, "/", http.StatusFound)
367366

368367
}
369368

@@ -377,7 +376,7 @@ func clearEvent(res http.ResponseWriter, req *http.Request) {
377376
//
378377
username := req.Context().Value(keyUser)
379378
if username == nil {
380-
http.Redirect(res, req, "/login", 302)
379+
http.Redirect(res, req, "/login", http.StatusFound)
381380
return
382381
}
383382

@@ -388,7 +387,7 @@ func clearEvent(res http.ResponseWriter, req *http.Request) {
388387
id := vars["id"]
389388

390389
storage.ClearEvent(id)
391-
http.Redirect(res, req, "/", 302)
390+
http.Redirect(res, req, "/", http.StatusFound)
392391

393392
}
394393

@@ -401,7 +400,7 @@ func raiseEvent(res http.ResponseWriter, req *http.Request) {
401400
//
402401
username := req.Context().Value(keyUser)
403402
if username == nil {
404-
http.Redirect(res, req, "/login", 302)
403+
http.Redirect(res, req, "/login", http.StatusFound)
405404
return
406405
}
407406

@@ -412,7 +411,7 @@ func raiseEvent(res http.ResponseWriter, req *http.Request) {
412411
id := vars["id"]
413412

414413
storage.RaiseEvent(id)
415-
http.Redirect(res, req, "/", 302)
414+
http.Redirect(res, req, "/", http.StatusFound)
416415

417416
}
418417

@@ -422,11 +421,11 @@ func raiseEvent(res http.ResponseWriter, req *http.Request) {
422421
func serveResource(response http.ResponseWriter, request *http.Request, resource string, mime string) {
423422
tmpl, err := getResource(resource)
424423
if err != nil {
425-
fmt.Fprintf(response, err.Error())
424+
fmt.Fprint(response, err.Error())
426425
return
427426
}
428427
response.Header().Set("Content-Type", mime)
429-
fmt.Fprintf(response, string(tmpl))
428+
fmt.Fprint(response, string(tmpl))
430429
}
431430

432431
//
@@ -473,14 +472,14 @@ func loginHandler(response http.ResponseWriter, request *http.Request) {
473472
http.SetCookie(response, cookie)
474473
}
475474

476-
http.Redirect(response, request, "/", 302)
475+
http.Redirect(response, request, "/", http.StatusFound)
477476
return
478477
}
479478

480479
//
481480
// Failure to login, redirect to try again.
482481
//
483-
http.Redirect(response, request, "/login#failed", 302)
482+
http.Redirect(response, request, "/login#failed", http.StatusFound)
484483
}
485484

486485
//
@@ -494,7 +493,7 @@ func logoutHandler(response http.ResponseWriter, request *http.Request) {
494493
MaxAge: -1,
495494
}
496495
http.SetCookie(response, cookie)
497-
http.Redirect(response, request, "/", 302)
496+
http.Redirect(response, request, "/", http.StatusFound)
498497
}
499498

500499
//
@@ -509,7 +508,7 @@ func eventsHandler(response http.ResponseWriter, request *http.Request) {
509508
//
510509
username := request.Context().Value(keyUser)
511510
if username == nil {
512-
http.Redirect(response, request, "/login", 302)
511+
http.Redirect(response, request, "/login", http.StatusFound)
513512
return
514513
}
515514

@@ -547,7 +546,7 @@ func indexPageHandler(response http.ResponseWriter, request *http.Request) {
547546
//
548547
username := request.Context().Value(keyUser)
549548
if username == nil {
550-
http.Redirect(response, request, "/login", 302)
549+
http.Redirect(response, request, "/login", http.StatusFound)
551550
return
552551
}
553552

static.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"bytes"
1010
"compress/gzip"
1111
"encoding/hex"
12-
"errors"
12+
"fmt"
1313
"io/ioutil"
1414
)
1515

@@ -96,7 +96,7 @@ func getResource(path string) ([]byte, error) {
9696
return raw.Bytes(), nil
9797
}
9898
}
99-
return nil, errors.New("Failed to find resource")
99+
return nil, fmt.Errorf("failed to find resource '%s'", path)
100100
}
101101

102102
//

util/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Str2Unix(data string) (int64, error) {
2424
return now, nil
2525
}
2626

27-
re := regexp.MustCompile("^\\+?([0-9]+)([hHmMsS])$")
27+
re := regexp.MustCompile(`^\\+?([0-9]+)([hHmMsS])$`)
2828
out := re.FindStringSubmatch(data)
2929

3030
res := now

0 commit comments

Comments
 (0)