diff --git a/.dockerignore b/.dockerignore index 3f56cbb..30805e1 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ /test /vendor /azure-scheduledevents-exporter +/release-assets diff --git a/.github/workflows/ci-docker.yml b/.github/workflows/ci-docker.yaml similarity index 83% rename from .github/workflows/ci-docker.yml rename to .github/workflows/ci-docker.yaml index ca9d711..da02fff 100644 --- a/.github/workflows/ci-docker.yml +++ b/.github/workflows/ci-docker.yaml @@ -8,16 +8,16 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Run Gosec Security Scanner - uses: securego/gosec@master + - name: Set Swap Space + uses: pierotofy/set-swap-space@master with: - args: ./... + swap-size-gb: 12 - name: Run Golangci lint uses: golangci/golangci-lint-action@v2 with: version: latest - args: -E exportloopref,gofmt --timeout=30m + args: --print-resources-usage - name: Docker meta id: docker_meta @@ -39,6 +39,6 @@ jobs: context: . file: ./Dockerfile push: false - platforms: linux/amd64,linux/arm,linux/arm64 + platforms: linux/amd64,linux/arm64 tags: ${{ steps.docker_meta.outputs.tags }} labels: ${{ steps.docker_meta.outputs.labels }} diff --git a/.github/workflows/release-assets.yaml b/.github/workflows/release-assets.yaml new file mode 100644 index 0000000..9d38757 --- /dev/null +++ b/.github/workflows/release-assets.yaml @@ -0,0 +1,34 @@ +name: "Release: assets" + +on: + release: + types: [created] + +jobs: + release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set Swap Space + uses: pierotofy/set-swap-space@master + with: + swap-size-gb: 12 + + - uses: actions/setup-go@v2 + with: + go-version: '1.19' + check-latest: true + + - name: Build + run: | + make release-assets + + - name: Upload assets to release + uses: svenstaro/upload-release-action@v2 + with: + repo_token: ${{ secrets.GITHUB_TOKEN }} + file: ./release-assets/* + tag: ${{ github.ref }} + overwrite: true + file_glob: true diff --git a/.github/workflows/release-docker.yml b/.github/workflows/release-docker.yaml similarity index 87% rename from .github/workflows/release-docker.yml rename to .github/workflows/release-docker.yaml index f4dd07d..421e4a3 100644 --- a/.github/workflows/release-docker.yml +++ b/.github/workflows/release-docker.yaml @@ -7,23 +7,22 @@ on: tags: - '*.*.*' - jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Run Gosec Security Scanner - uses: securego/gosec@master + - name: Set Swap Space + uses: pierotofy/set-swap-space@master with: - args: ./... + swap-size-gb: 12 - name: Run Golangci lint uses: golangci/golangci-lint-action@v2 with: version: latest - args: -E exportloopref,gofmt --timeout=30m + args: --print-resources-usage build: needs: lint @@ -47,6 +46,11 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Set Swap Space + uses: pierotofy/set-swap-space@master + with: + swap-size-gb: 12 + - name: Docker meta id: docker_meta uses: docker/metadata-action@v4 @@ -82,7 +86,7 @@ jobs: with: context: . file: ./${{ matrix.Dockerfile }} - platforms: linux/amd64,linux/arm,linux/arm64,linux/ppc64le + platforms: linux/amd64,linux/arm64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.docker_meta.outputs.tags }} labels: ${{ steps.docker_meta.outputs.labels }} diff --git a/.github/workflows/scheduled-docker.yml b/.github/workflows/scheduled-docker.yaml similarity index 87% rename from .github/workflows/scheduled-docker.yml rename to .github/workflows/scheduled-docker.yaml index 22da68e..37725d2 100644 --- a/.github/workflows/scheduled-docker.yml +++ b/.github/workflows/scheduled-docker.yaml @@ -10,16 +10,16 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Run Gosec Security Scanner - uses: securego/gosec@master + - name: Set Swap Space + uses: pierotofy/set-swap-space@master with: - args: ./... + swap-size-gb: 12 - name: Run Golangci lint uses: golangci/golangci-lint-action@v2 with: version: latest - args: -E exportloopref,gofmt --timeout=30m + args: --print-resources-usage build: needs: lint @@ -43,6 +43,11 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Set Swap Space + uses: pierotofy/set-swap-space@master + with: + swap-size-gb: 12 + - name: Docker meta id: docker_meta uses: docker/metadata-action@v4 @@ -78,7 +83,7 @@ jobs: with: context: . file: ./${{ matrix.Dockerfile }} - platforms: linux/amd64,linux/arm,linux/arm64,linux/ppc64le + platforms: linux/amd64,linux/arm64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.docker_meta.outputs.tags }} labels: ${{ steps.docker_meta.outputs.labels }} diff --git a/.gitignore b/.gitignore index 49f352d..2aa05c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor /azure-scheduledevents-manager +/release-assets diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..4496150 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,26 @@ +run: + timeout: 120m + +linters: + enable: + - asciicheck + - bidichk + - bodyclose + - errorlint + - exportloopref + - gofmt + - goimports + - gosec + +linters-settings: + gosec: + excludes: [] + confidence: low + config: + global: + audit: true + +issues: {} + +output: + sort-results: true diff --git a/Makefile b/Makefile index 12a8761..ff909ca 100644 --- a/Makefile +++ b/Makefile @@ -5,18 +5,17 @@ LDFLAGS := -X "main.gitTag=$(GIT_TAG)" -X "main.gitCommit=$(GIT_COMMIT)" -ext FIRST_GOPATH := $(firstword $(subst :, ,$(shell go env GOPATH))) GOLANGCI_LINT_BIN := $(FIRST_GOPATH)/bin/golangci-lint -GOSEC_BIN := $(FIRST_GOPATH)/bin/gosec .PHONY: all -all: build +all: vendor build .PHONY: clean clean: git clean -Xfd . -.PHONY: build -build: - GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -a -ldflags '$(LDFLAGS)' -o $(PROJECT_NAME) . +####################################### +# builds +####################################### .PHONY: vendor vendor: @@ -24,35 +23,72 @@ vendor: go mod vendor go mod verify +.PHONY: build-all +build-all: + GOOS=linux GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(PROJECT_NAME)' . + GOOS=darwin GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(PROJECT_NAME).darwin' . + GOOS=windows GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(PROJECT_NAME).exe' . + +.PHONY: build +build: + GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o $(PROJECT_NAME) . + .PHONY: image -image: build +image: image docker build -t $(PROJECT_NAME):$(GIT_TAG) . +.PHONY: build-push-development build-push-development: docker buildx create --use docker buildx build -t webdevops/$(PROJECT_NAME):development --platform linux/amd64,linux/arm,linux/arm64 --push . -.PHONY: test -test: - go test ./... +####################################### +# quality checks +####################################### -.PHONY: dependencies -dependencies: - go mod vendor +.PHONY: check +check: vendor lint test -.PHONY: check-release -check-release: vendor lint gosec test +.PHONY: test +test: + time go test ./... .PHONY: lint lint: $(GOLANGCI_LINT_BIN) - $(GOLANGCI_LINT_BIN) run -E exportloopref,gofmt --timeout=30m - -.PHONY: gosec -gosec: $(GOSEC_BIN) - $(GOSEC_BIN) ./... + time $(GOLANGCI_LINT_BIN) run --verbose --print-resources-usage $(GOLANGCI_LINT_BIN): curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(FIRST_GOPATH)/bin -$(GOSEC_BIN): - curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(FIRST_GOPATH)/bin +####################################### +# release assets +####################################### + +RELEASE_ASSETS = \ + $(foreach GOARCH,amd64 arm64,\ + $(foreach GOOS,linux darwin windows,\ + release-assets/$(GOOS).$(GOARCH))) \ + +word-dot = $(word $2,$(subst ., ,$1)) + +.PHONY: release-assets +release-assets: clean-release-assets vendor $(RELEASE_ASSETS) + +.PHONY: clean-release-assets +clean-release-assets: + rm -rf ./release-assets + mkdir -p ./release-assets + +release-assets/windows.%: $(SOURCE) + echo 'build release-assets for windows/$(call word-dot,$*,2)' + GOOS=windows \ + GOARCH=$(call word-dot,$*,1) \ + CGO_ENABLED=0 \ + time go build -ldflags '$(LDFLAGS)' -o './release-assets/$(PROJECT_NAME).windows.$(call word-dot,$*,1).exe' . + +release-assets/%: $(SOURCE) + echo 'build release-assets for $(call word-dot,$*,1)/$(call word-dot,$*,2)' + GOOS=$(call word-dot,$*,1) \ + GOARCH=$(call word-dot,$*,2) \ + CGO_ENABLED=0 \ + time go build -ldflags '$(LDFLAGS)' -o './release-assets/$(PROJECT_NAME).$(call word-dot,$*,1).$(call word-dot,$*,2)' . diff --git a/go.mod b/go.mod index affb00a..f8ffe3e 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/containrrr/shoutrrr v0.6.1 github.com/fatih/color v1.13.0 // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect - github.com/jessevdk/go-flags v1.5.0 github.com/mattn/go-colorable v0.1.13 // indirect github.com/onsi/ginkgo v1.16.5 // indirect github.com/onsi/gomega v1.19.0 // indirect @@ -20,7 +19,10 @@ require ( google.golang.org/protobuf v1.28.1 // indirect ) -require github.com/go-resty/resty/v2 v2.7.0 +require ( + github.com/go-resty/resty/v2 v2.7.0 + github.com/jessevdk/go-flags v1.5.0 +) require ( github.com/beorn7/perks v1.0.1 // indirect diff --git a/main.go b/main.go index f907ba3..663d4f8 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "net/http" "net/url" @@ -10,7 +11,7 @@ import ( "strings" "sync/atomic" - "github.com/jessevdk/go-flags" + flags "github.com/jessevdk/go-flags" "github.com/prometheus/client_golang/prometheus/promhttp" log "github.com/sirupsen/logrus" @@ -116,7 +117,8 @@ func initArgparser() { // check if there is an parse error if err != nil { - if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp { + var flagsErr *flags.Error + if ok := errors.As(err, &flagsErr); ok && flagsErr.Type == flags.ErrHelp { os.Exit(0) } else { fmt.Println()