diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index f397afe..56daff1 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -19,6 +19,10 @@ jobs: - name: Test run: go test -v ./... + + - name: Lint source code + run: | + make tools lint - name: Create release tag run: | diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..d51b61b --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,26 @@ +linters: + disable-all: true + enable: + - deadcode + - errcheck + - gofmt + - goimports + - gosimple + - ineffassign + - misspell + - staticcheck + - structcheck + - unconvert + - unused + - varcheck + - govet + +linters-settings: + goimports: + local-prefixes: github.com/bastiandoetsch/mullvad-best-server + +output: + format: tab + +run: + deadline: 10m diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 9aaadd6..1a751d7 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -5,6 +5,8 @@ before: - go mod tidy builds: + - flags: + - -trimpath - env: - CGO_ENABLED=0 goos: @@ -12,8 +14,6 @@ builds: - windows - darwin mod_timestamp: "{{ .CommitTimestamp }}" - ldflags: - - -s -w archives: - replacements: diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1cbe263 --- /dev/null +++ b/Makefile @@ -0,0 +1,60 @@ +# project variables +PROJECT_NAME := mullvad-best-server + +# build variables +.DEFAULT_GOAL = lint +BUILD_DIR := dist +DEV_GOARCH := $(shell go env GOARCH) +DEV_GOOS := $(shell go env GOOS) + + +## tools: Install required tooling. +.PHONY: tools +tools: +ifeq (,$(wildcard ./.bin/golangci-lint*)) + @curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b .bin/ v1.44.2 +else + @echo "==> Required tooling is already installed" +endif + +## clean: Delete the build directory +.PHONY: clean +clean: + @echo "==> Removing '$(BUILD_DIR)' directory..." + @rm -rf $(BUILD_DIR) + +## lint: Lint code with golangci-lint. +.PHONY: lint +lint: tools + @echo "==> Linting code with 'golangci-lint'..." + @.bin/golangci-lint run ./... + +## test: Run all unit tests. +.PHONY: test +test: + @echo "==> Running unit tests..." + @mkdir -p $(BUILD_DIR) + @go test -count=1 -v -cover -coverprofile=$(BUILD_DIR)/coverage.out -parallel=4 ./... + +## build: Build binary for default local system's OS and architecture. +.PHONY: build +build: + @echo "==> Building binary..." + @echo " running go build for GOOS=$(DEV_GOOS) GOARCH=$(DEV_GOARCH)" +# workaround for missing .exe extension on Windows +ifeq ($(OS),Windows_NT) + @go build -o $(BUILD_DIR)/$(PROJECT_NAME).$(DEV_GOOS).$(DEV_GOARCH).exe +else + @go build -o $(BUILD_DIR)/$(PROJECT_NAME).$(DEV_GOOS).$(DEV_GOARCH) +endif + +.PHONY: run +run: + @echo "==> Running $(PROJECT_NAME)" + @go run main.go + +help: Makefile + @echo "Usage: make " + @echo "" + @echo "Commands:" + @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' diff --git a/main.go b/main.go index 4a14590..c5f4760 100644 --- a/main.go +++ b/main.go @@ -4,14 +4,15 @@ import ( "encoding/json" "flag" "fmt" - "github.com/go-ping/ping" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "io" "io/ioutil" "net/http" "strings" "time" + + "github.com/go-ping/ping" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) func main() { @@ -66,6 +67,9 @@ func getServers(serverType string) []server { log.Err(err) } }(resp.Body) + if err != nil { + log.Fatal().Err(err) + } var servers []server err = json.Unmarshal(responseBody, &servers) if err != nil {