*: update Makefile.common with new staticcheck (#1692)

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2019-01-04 15:37:33 +01:00 committed by stuart nelson
parent cfc0d9c558
commit b676fa79c0
15 changed files with 54 additions and 56 deletions

View File

@ -17,9 +17,7 @@ FRONTEND_DIR = $(BIN_DIR)/ui/app
DOCKER_IMAGE_NAME ?= alertmanager DOCKER_IMAGE_NAME ?= alertmanager
ERRCHECK_BINARY := $(FIRST_GOPATH)/bin/errcheck ERRCHECK_BINARY := $(FIRST_GOPATH)/bin/errcheck
STATICCHECK_IGNORE = \ STATICCHECK_IGNORE =
github.com/prometheus/alertmanager/notify/notify.go:SA6002
# Go modules needs the bzr binary because of the dependency on launchpad.net/gocheck. # Go modules needs the bzr binary because of the dependency on launchpad.net/gocheck.
$(eval $(call PRECHECK_COMMAND_template,bzr)) $(eval $(call PRECHECK_COMMAND_template,bzr))

View File

@ -29,6 +29,8 @@ GO ?= go
GOFMT ?= $(GO)fmt GOFMT ?= $(GO)fmt
FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH))) FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
GOOPTS ?= GOOPTS ?=
GOHOSTOS ?= $(shell $(GO) env GOHOSTOS)
GOHOSTARCH ?= $(shell $(GO) env GOHOSTARCH)
GO_VERSION ?= $(shell $(GO) version) GO_VERSION ?= $(shell $(GO) version)
GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION)) GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
@ -62,17 +64,30 @@ PROMU := $(FIRST_GOPATH)/bin/promu
STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck
pkgs = ./... pkgs = ./...
GO_VERSION ?= $(shell $(GO) version) ifeq (arm, $(GOHOSTARCH))
GO_BUILD_PLATFORM ?= $(subst /,-,$(lastword $(GO_VERSION))) GOHOSTARM ?= $(shell GOARM= $(GO) env GOARM)
GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)v$(GOHOSTARM)
else
GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)
endif
PROMU_VERSION ?= 0.2.0 PROMU_VERSION ?= 0.2.0
PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz
STATICCHECK_VERSION ?= 2019.1
STATICCHECK_URL := https://github.com/dominikh/go-tools/releases/download/$(STATICCHECK_VERSION)/staticcheck_$(GOHOSTOS)_$(GOHOSTARCH)
PREFIX ?= $(shell pwd) PREFIX ?= $(shell pwd)
BIN_DIR ?= $(shell pwd) BIN_DIR ?= $(shell pwd)
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
DOCKER_REPO ?= prom DOCKER_REPO ?= prom
ifeq ($(GOHOSTARCH),amd64)
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux freebsd darwin windows))
# Only supported on amd64
test-flags := -race
endif
endif
.PHONY: all .PHONY: all
all: precheck style staticcheck unused build test all: precheck style staticcheck unused build test
@ -110,12 +125,12 @@ common-test-short:
.PHONY: common-test .PHONY: common-test
common-test: common-test:
@echo ">> running all tests" @echo ">> running all tests"
GO111MODULE=$(GO111MODULE) $(GO) test -race $(GOOPTS) $(pkgs) GO111MODULE=$(GO111MODULE) $(GO) test $(test-flags) $(GOOPTS) $(pkgs)
.PHONY: common-format .PHONY: common-format
common-format: common-format:
@echo ">> formatting code" @echo ">> formatting code"
GO111MODULE=$(GO111MODULE) $(GO) fmt $(GOOPTS) $(pkgs) GO111MODULE=$(GO111MODULE) $(GO) fmt $(pkgs)
.PHONY: common-vet .PHONY: common-vet
common-vet: common-vet:
@ -125,8 +140,12 @@ common-vet:
.PHONY: common-staticcheck .PHONY: common-staticcheck
common-staticcheck: $(STATICCHECK) common-staticcheck: $(STATICCHECK)
@echo ">> running staticcheck" @echo ">> running staticcheck"
chmod +x $(STATICCHECK)
ifdef GO111MODULE ifdef GO111MODULE
GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" -checks "SA*" $(pkgs) # 'go list' needs to be executed before staticcheck to prepopulate the modules cache.
# Otherwise staticcheck might fail randomly for some reason not yet explained.
GO111MODULE=$(GO111MODULE) $(GO) list -e -compiled -test=true -export=false -deps=true -find=false -tags= -- ./... > /dev/null
GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
else else
$(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
endif endif
@ -140,8 +159,9 @@ else
ifdef GO111MODULE ifdef GO111MODULE
@echo ">> running check for unused/missing packages in go.mod" @echo ">> running check for unused/missing packages in go.mod"
GO111MODULE=$(GO111MODULE) $(GO) mod tidy GO111MODULE=$(GO111MODULE) $(GO) mod tidy
ifeq (,$(wildcard vendor))
@git diff --exit-code -- go.sum go.mod @git diff --exit-code -- go.sum go.mod
ifneq (,$(wildcard vendor)) else
@echo ">> running check for unused packages in vendor/" @echo ">> running check for unused packages in vendor/"
GO111MODULE=$(GO111MODULE) $(GO) mod vendor GO111MODULE=$(GO111MODULE) $(GO) mod vendor
@git diff --exit-code -- go.sum go.mod vendor/ @git diff --exit-code -- go.sum go.mod vendor/
@ -175,30 +195,20 @@ common-docker-tag-latest:
promu: $(PROMU) promu: $(PROMU)
$(PROMU): $(PROMU):
curl -s -L $(PROMU_URL) | tar -xvz -C /tmp $(eval PROMU_TMP := $(shell mktemp -d))
mkdir -v -p $(FIRST_GOPATH)/bin curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP)
cp -v /tmp/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(PROMU) mkdir -p $(FIRST_GOPATH)/bin
cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu
rm -r $(PROMU_TMP)
.PHONY: proto .PHONY: proto
proto: proto:
@echo ">> generating code from proto files" @echo ">> generating code from proto files"
@./scripts/genproto.sh @./scripts/genproto.sh
.PHONY: $(STATICCHECK)
$(STATICCHECK): $(STATICCHECK):
ifdef GO111MODULE mkdir -p $(FIRST_GOPATH)/bin
# Get staticcheck from a temporary directory to avoid modifying the local go.{mod,sum}. curl -s -L $(STATICCHECK_URL) > $(STATICCHECK)
# See https://github.com/golang/go/issues/27643.
# For now, we are using the next branch of staticcheck because master isn't compatible yet with Go modules.
tmpModule=$$(mktemp -d 2>&1) && \
mkdir -p $${tmpModule}/staticcheck && \
cd "$${tmpModule}"/staticcheck && \
GO111MODULE=on $(GO) mod init example.com/staticcheck && \
GO111MODULE=on GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck@next && \
rm -rf $${tmpModule};
else
GOOS= GOARCH= GO111MODULE=off $(GO) get -u honnef.co/go/tools/cmd/staticcheck
endif
ifdef GOVENDOR ifdef GOVENDOR
.PHONY: $(GOVENDOR) .PHONY: $(GOVENDOR)

View File

@ -165,7 +165,6 @@ func (api *API) Update(cfg *config.Config, resolveTimeout time.Duration) error {
type errorType string type errorType string
const ( const (
errorNone errorType = ""
errorInternal errorType = "server_error" errorInternal errorType = "server_error"
errorBadData errorType = "bad_data" errorBadData errorType = "bad_data"
) )

View File

@ -29,7 +29,6 @@ type routingShow struct {
configFile string configFile string
labels []string labels []string
expectedReceivers string expectedReceivers string
tree treeprint.Tree
debugTree bool debugTree bool
} }
@ -106,15 +105,15 @@ func getMatchingTree(route *dispatch.Route, tree treeprint.Tree, lset client.Lab
final := true final := true
branch := tree.AddBranch(getRouteTreeSlug(route, false, false)) branch := tree.AddBranch(getRouteTreeSlug(route, false, false))
for _, r := range route.Routes { for _, r := range route.Routes {
if r.Matchers.Match(convertClientToCommonLabelSet(lset)) == true { if r.Matchers.Match(convertClientToCommonLabelSet(lset)) {
getMatchingTree(r, branch, lset) getMatchingTree(r, branch, lset)
final = false final = false
if r.Continue != true { if !r.Continue {
break break
} }
} }
} }
if final == true { if final {
branch.SetValue(getRouteTreeSlug(route, false, true)) branch.SetValue(getRouteTreeSlug(route, false, true))
} }
} }

View File

@ -84,7 +84,7 @@ func (c *routingShow) routingTestAction(ctx context.Context, _ *kingpin.ParseCon
kingpin.Fatalf("Failed to parse labels: %v\n", err) kingpin.Fatalf("Failed to parse labels: %v\n", err)
} }
if c.debugTree == true { if c.debugTree {
printMatchingTree(mainRoute, ls) printMatchingTree(mainRoute, ls)
} }

View File

@ -112,7 +112,7 @@ func loadAlertmanagerConfig(ctx context.Context, alertmanagerURL *url.URL, confi
} }
return status.ConfigJSON, nil return status.ConfigJSON, nil
} }
return nil, errors.New("Failed to get Alertmanager configuration.") return nil, errors.New("failed to get Alertmanager configuration")
} }
// convertClientToCommonLabelSet converts client.LabelSet to model.Labelset // convertClientToCommonLabelSet converts client.LabelSet to model.Labelset

View File

@ -35,7 +35,6 @@ const (
epSilence = apiPrefix + "/silence/:id" epSilence = apiPrefix + "/silence/:id"
epSilences = apiPrefix + "/silences" epSilences = apiPrefix + "/silences"
epAlerts = apiPrefix + "/alerts" epAlerts = apiPrefix + "/alerts"
epAlertGroups = apiPrefix + "/alerts/groups"
statusSuccess = "success" statusSuccess = "success"
statusError = "error" statusError = "error"

View File

@ -319,7 +319,7 @@ func TestAPI(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if bytes.Compare(want, got) != 0 { if !bytes.Equal(want, got) {
t.Errorf("unexpected result: want: %s, got: %s", string(want), string(got)) t.Errorf("unexpected result: want: %s, got: %s", string(want), string(got))
} }
}) })

View File

@ -84,7 +84,7 @@ func newSubset(elements ...uint64) map[uint64]struct{} {
func elements(m map[uint64]struct{}) []uint64 { func elements(m map[uint64]struct{}) []uint64 {
els := make([]uint64, 0, len(m)) els := make([]uint64, 0, len(m))
for k, _ := range m { for k := range m {
els = append(els, k) els = append(els, k)
} }

View File

@ -895,7 +895,7 @@ func (n *Hipchat) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
) )
apiURL.Path += fmt.Sprintf("v2/room/%s/notification", roomid) apiURL.Path += fmt.Sprintf("v2/room/%s/notification", roomid)
q := apiURL.Query() q := apiURL.Query()
q.Set("auth_token", fmt.Sprintf("%s", n.conf.AuthToken)) q.Set("auth_token", string(n.conf.AuthToken))
apiURL.RawQuery = q.Encode() apiURL.RawQuery = q.Encode()
if n.conf.MessageFormat == "html" { if n.conf.MessageFormat == "html" {
@ -1007,7 +1007,7 @@ func (n *Wechat) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
} }
// Refresh AccessToken over 2 hours // Refresh AccessToken over 2 hours
if n.accessToken == "" || time.Now().Sub(n.accessTokenAt) > 2*time.Hour { if n.accessToken == "" || time.Since(n.accessTokenAt) > 2*time.Hour {
parameters := url.Values{} parameters := url.Values{}
parameters.Add("corpsecret", tmpl(string(n.conf.APISecret))) parameters.Add("corpsecret", tmpl(string(n.conf.APISecret)))
parameters.Add("corpid", tmpl(string(n.conf.CorpID))) parameters.Add("corpid", tmpl(string(n.conf.CorpID)))

View File

@ -489,6 +489,7 @@ func getHashBuffer() []byte {
func putHashBuffer(b []byte) { func putHashBuffer(b []byte) {
b = b[:0] b = b[:0]
//lint:ignore SA6002 relax staticcheck verification.
hashBuffers.Put(b) hashBuffers.Put(b)
} }

View File

@ -33,12 +33,8 @@ var (
func Matchers(s string) ([]*labels.Matcher, error) { func Matchers(s string) ([]*labels.Matcher, error) {
matchers := []*labels.Matcher{} matchers := []*labels.Matcher{}
if strings.HasPrefix(s, "{") { s = strings.TrimPrefix(s, "{")
s = s[1:] s = strings.TrimSuffix(s, "}")
}
if strings.HasSuffix(s, "}") {
s = s[:len(s)-1]
}
var insideQuotes bool var insideQuotes bool
var token string var token string

View File

@ -191,7 +191,7 @@ func (t *AcceptanceTest) Run() {
deadline := t.opts.expandTime(latest) deadline := t.opts.expandTime(latest)
select { select {
case <-time.After(deadline.Sub(time.Now())): case <-time.After(time.Until(deadline)):
// continue // continue
case err := <-errc: case err := <-errc:
t.Error(err) t.Error(err)
@ -213,7 +213,7 @@ func (t *AcceptanceTest) runActions() {
for _, f := range fs { for _, f := range fs {
go func(f func()) { go func(f func()) {
time.Sleep(ts.Sub(time.Now())) time.Sleep(time.Until(ts))
f() f()
wg.Done() wg.Done()
}(f) }(f)

View File

@ -199,7 +199,7 @@ func (t *AcceptanceTest) Run() {
deadline := t.opts.expandTime(latest) deadline := t.opts.expandTime(latest)
select { select {
case <-time.After(deadline.Sub(time.Now())): case <-time.After(time.Until(deadline)):
// continue // continue
case err := <-errc: case err := <-errc:
t.Error(err) t.Error(err)
@ -216,7 +216,7 @@ func (t *AcceptanceTest) runActions() {
for _, f := range fs { for _, f := range fs {
go func(f func()) { go func(f func()) {
time.Sleep(ts.Sub(time.Now())) time.Sleep(time.Until(ts))
f() f()
wg.Done() wg.Done()
}(f) }(f)

View File

@ -373,10 +373,6 @@ type Silence struct {
CreatedBy string `json:"createdBy"` CreatedBy string `json:"createdBy"`
Comment string `json:"comment,omitempty"` Comment string `json:"comment,omitempty"`
// timeFunc provides the time against which to evaluate
// the silence. Used for test injection.
now func() time.Time
Status SilenceStatus `json:"status"` Status SilenceStatus `json:"status"`
} }