Skip to content

Commit

Permalink
Update build
Browse files Browse the repository at this point in the history
* Update workflows.
* Update golangci-lint.
* Inline errcheck.
* Enable more linters.
* Update Prometheus common files.

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Jul 15, 2023
1 parent 3b48450 commit dad4040
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 43 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
image: quay.io/prometheus/golang-builder:1.20-base
steps:
- uses: actions/checkout@v3
- uses: prometheus/promci@v0.0.2
- uses: prometheus/promci@v0.1.0
- uses: ./.github/promci/actions/setup_environment
- run: make

Expand All @@ -40,13 +40,13 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.20.x
- name: Lint
uses: golangci/golangci-lint-action@v3.4.0
uses: golangci/golangci-lint-action@v3.6.0
with:
version: v1.51.2
version: v1.53.3

publish_main:
name: Publish main branch artifacts
Expand All @@ -55,7 +55,7 @@ jobs:
if: github.event_name == 'push' && github.event.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- uses: prometheus/promci@v0.0.2
- uses: prometheus/promci@v0.1.0
- uses: ./.github/promci/actions/publish_main
with:
docker_hub_organization: superque
Expand All @@ -74,7 +74,7 @@ jobs:
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v3
- uses: prometheus/promci@v0.0.2
- uses: prometheus/promci@v0.1.0
- uses: ./.github/promci/actions/publish_release
with:
docker_hub_organization: superque
Expand Down
20 changes: 19 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
---
linters:
enable:
- misspell
- revive
disable:
# Disable soon to deprecated[1] linters that lead to false
# positives when build tags disable certain files[2]
# 1: https://github.com/golangci/golangci-lint/issues/1841
# 2: https://github.com/prometheus/node_exporter/issues/1545
- deadcode
- unused
- structcheck
- varcheck

linters-settings:
errcheck:
exclude: scripts/errcheck_excludes.txt
exclude-functions:
# Used in HTTP handlers, any error is handled by the server itself.
- (net/http.ResponseWriter).Write
# Never check for logger errors.
- (github.com/go-kit/log.Logger).Log
3 changes: 1 addition & 2 deletions .yamllint
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ rules:
config/testdata/section_key_dup.bad.yml
line-length: disable
truthy:
ignore: |
.github/workflows/*.yml
check-keys: false
20 changes: 11 additions & 9 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ endif
GOTEST := $(GO) test
GOTEST_DIR :=
ifneq ($(CIRCLE_JOB),)
ifneq ($(shell which gotestsum),)
ifneq ($(shell command -v gotestsum > /dev/null),)
GOTEST_DIR := test-results
GOTEST := gotestsum --junitfile $(GOTEST_DIR)/unit-tests.xml --
endif
Expand All @@ -61,7 +61,7 @@ PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_
SKIP_GOLANGCI_LINT :=
GOLANGCI_LINT :=
GOLANGCI_LINT_OPTS ?=
GOLANGCI_LINT_VERSION ?= v1.51.2
GOLANGCI_LINT_VERSION ?= v1.53.3
# golangci-lint only supports linux, darwin and windows platforms on i386/amd64.
# windows isn't included here because of the path separator being different.
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))
Expand Down Expand Up @@ -91,6 +91,8 @@ BUILD_DOCKER_ARCHS = $(addprefix common-docker-,$(DOCKER_ARCHS))
PUBLISH_DOCKER_ARCHS = $(addprefix common-docker-publish-,$(DOCKER_ARCHS))
TAG_DOCKER_ARCHS = $(addprefix common-docker-tag-latest-,$(DOCKER_ARCHS))

SANITIZED_DOCKER_IMAGE_TAG := $(subst +,-,$(DOCKER_IMAGE_TAG))

ifeq ($(GOHOSTARCH),amd64)
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux freebsd darwin windows))
# Only supported on amd64
Expand Down Expand Up @@ -176,7 +178,7 @@ endif
.PHONY: common-yamllint
common-yamllint:
@echo ">> running yamllint on all YAML files in the repository"
ifeq (, $(shell which yamllint))
ifeq (, $(shell command -v yamllint > /dev/null))
@echo "yamllint not installed so skipping"
else
yamllint .
Expand Down Expand Up @@ -205,7 +207,7 @@ common-tarball: promu
.PHONY: common-docker $(BUILD_DOCKER_ARCHS)
common-docker: $(BUILD_DOCKER_ARCHS)
$(BUILD_DOCKER_ARCHS): common-docker-%:
docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" \
docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(SANITIZED_DOCKER_IMAGE_TAG)" \
-f $(DOCKERFILE_PATH) \
--build-arg ARCH="$*" \
--build-arg OS="linux" \
Expand All @@ -214,19 +216,19 @@ $(BUILD_DOCKER_ARCHS): common-docker-%:
.PHONY: common-docker-publish $(PUBLISH_DOCKER_ARCHS)
common-docker-publish: $(PUBLISH_DOCKER_ARCHS)
$(PUBLISH_DOCKER_ARCHS): common-docker-publish-%:
docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)"
docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(SANITIZED_DOCKER_IMAGE_TAG)"

DOCKER_MAJOR_VERSION_TAG = $(firstword $(subst ., ,$(shell cat VERSION)))
.PHONY: common-docker-tag-latest $(TAG_DOCKER_ARCHS)
common-docker-tag-latest: $(TAG_DOCKER_ARCHS)
$(TAG_DOCKER_ARCHS): common-docker-tag-latest-%:
docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:latest"
docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:v$(DOCKER_MAJOR_VERSION_TAG)"
docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(SANITIZED_DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:latest"
docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(SANITIZED_DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:v$(DOCKER_MAJOR_VERSION_TAG)"

.PHONY: common-docker-manifest
common-docker-manifest:
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest create -a "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" $(foreach ARCH,$(DOCKER_ARCHS),$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$(ARCH):$(DOCKER_IMAGE_TAG))
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)"
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest create -a "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(SANITIZED_DOCKER_IMAGE_TAG)" $(foreach ARCH,$(DOCKER_ARCHS),$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$(ARCH):$(SANITIZED_DOCKER_IMAGE_TAG))
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(SANITIZED_DOCKER_IMAGE_TAG)"

.PHONY: promu
promu: $(PROMU)
Expand Down
5 changes: 0 additions & 5 deletions scripts/errcheck_excludes.txt

This file was deleted.

2 changes: 1 addition & 1 deletion vigor_v5/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//
// ## Prefix padding base64 encoding.
//
// The base64 encoding sent and recived from the firmware does not use the
// The base64 encoding sent and received from the firmware does not use the
// standard trailing `=` padding. Instead, it places a number at the beginning
// to denote how much encoding padding is needed.
//
Expand Down
8 changes: 4 additions & 4 deletions vigor_v5/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
var ErrLoginFailed = errors.New("login failed")

const (
loginJsonTemplate = `{"param":[],"ct":[{"Name":"%s","Password":"%s","locales":"en"}]}`
loginJSONTemplate = `{"param":[],"ct":[{"Name":"%s","Password":"%s","locales":"en"}]}`
)

func (v *Vigor) Login() error {
Expand Down Expand Up @@ -58,13 +58,13 @@ func (v *Vigor) Login() error {
return ErrLoginFailed
}

respJson, err := decodeVigorJson(resp)
respJSON, err := decodeVigorJSON(resp)
if err != nil {
level.Debug(v.logger).Log("msg", "Decoding response failed", "err", err)
return ErrLoginFailed
}

rid := gjson.Get(respJson, "rid").String()
rid := gjson.Get(respJSON, "rid").String()
if rid != "0000" {
level.Debug(v.logger).Log("msg", "Got invalid response ID", "rid", rid)
return ErrLoginFailed
Expand All @@ -89,5 +89,5 @@ func encodeLogin(username string, password string) string {
h.Write([]byte(password))
encodedPassword := hex.EncodeToString(h.Sum(nil))

return fmt.Sprintf(loginJsonTemplate, username, encodedPassword)
return fmt.Sprintf(loginJSONTemplate, username, encodedPassword)
}
8 changes: 4 additions & 4 deletions vigor_v5/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func (v *Vigor) FetchStatus() (Status, error) {
return Status{}, err
}

return v.parseDSLStatusGeneralJson(resp)
return v.parseDSLStatusGeneralJSON(resp)
}

func (v *Vigor) parseDSLStatusGeneralJson(respJson string) (Status, error) {
value := gjson.Get(respJson, "ct.0.0MONITORING_DSL_GENERAL.#(Name==\"Setting\")")
func (v *Vigor) parseDSLStatusGeneralJSON(respJSON string) (Status, error) {
value := gjson.Get(respJSON, "ct.0.0MONITORING_DSL_GENERAL.#(Name==\"Setting\")")
if !value.Exists() {
level.Debug(v.logger).Log("msg", "Unable to get settings", "response_json", respJson)
level.Debug(v.logger).Log("msg", "Unable to get settings", "response_json", respJSON)
return Status{}, ErrParseFailed
}

Expand Down
22 changes: 11 additions & 11 deletions vigor_v5/vigor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/tidwall/gjson"
)

var ErrJsonDecodeFailed = errors.New("json decode failed")
var ErrJSONDecodeFailed = errors.New("json decode failed")
var ErrRequestFailed = errors.New("failed to request with login")

type Vigor struct {
Expand Down Expand Up @@ -85,7 +85,7 @@ func (v *Vigor) postForm(p vigorForm) (*http.Response, error) {
urlValues := url.Values{
"pid": {p.pid},
"op": {p.op},
"ct": {encodeVigorJson(p.ct)},
"ct": {encodeVigorJSON(p.ct)},
"_token": {v.csrf},
}

Expand All @@ -103,12 +103,12 @@ func (v *Vigor) postWithLogin(p vigorForm) (string, error) {
for attempts := 0; attempts < 3; attempts++ {
resp, err := v.postForm(p)
if err == nil && resp.StatusCode == http.StatusOK {
respJson, err := decodeVigorJson(resp)
respJSON, err := decodeVigorJSON(resp)
if err == nil {
rid = gjson.Get(respJson, "rid").String()
rid = gjson.Get(respJSON, "rid").String()
if rid == "0000" {
resp.Body.Close()
return respJson, nil
return respJSON, nil
}
}
}
Expand All @@ -123,18 +123,18 @@ func (v *Vigor) postWithLogin(p vigorForm) (string, error) {
return "", ErrRequestFailed
}

func decodeVigorJson(resp *http.Response) (string, error) {
func decodeVigorJSON(resp *http.Response) (string, error) {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", ErrJsonDecodeFailed
return "", ErrJSONDecodeFailed
}
respPadding, err := strconv.Atoi(string(body[0]))
if err != nil {
return "", ErrJsonDecodeFailed
return "", ErrJSONDecodeFailed
}
if respPadding > 2 {
return "", ErrJsonDecodeFailed
return "", ErrJSONDecodeFailed
}
body = body[1:]
for i := 0; i < respPadding; i++ {
Expand All @@ -143,13 +143,13 @@ func decodeVigorJson(resp *http.Response) (string, error) {

decoded, err := base64.StdEncoding.DecodeString(string(body))
if err != nil {
return "", ErrJsonDecodeFailed
return "", ErrJSONDecodeFailed
}

return string(decoded), nil
}

func encodeVigorJson(j string) string {
func encodeVigorJSON(j string) string {
j = base64.StdEncoding.EncodeToString([]byte(j))

paddingLength := len(j)
Expand Down

0 comments on commit dad4040

Please sign in to comment.