From fa920fdc85f2c2c04b86d4465114b44ab2d41474 Mon Sep 17 00:00:00 2001 From: CEE Date: Wed, 2 Oct 2024 01:50:37 +0900 Subject: [PATCH] feat(ci): lint, ci, test, ghaction --- .github/dependabot.yml | 32 +++++ .github/workflows/commitlint.yml | 15 +++ .github/workflows/go.yml | 25 ---- .github/workflows/lint.yml | 27 ++++ .github/workflows/test.yml | 34 +++++ .gitignore | 15 ++- .gitlab-ci.yml | 42 ++++++ .golang-ci.yml | 54 ++++++++ .goreleaser.yml | 149 ++++++++++++++++++++++ .pre-commit-config.yaml | 14 ++ Dockerfile | 2 +- Makefile | 60 ++++++++- _config.yml | 1 + codecov.yml | 13 ++ commitlint.config.js | 5 + go.mod | 2 +- handlers/proxy/docker_proxy_controller.go | 6 +- handlers/proxy/maven_proxy_controller.go | 1 + install.sh | 22 ++++ 19 files changed, 481 insertions(+), 38 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/commitlint.yml delete mode 100644 .github/workflows/go.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/test.yml create mode 100644 .gitlab-ci.yml create mode 100644 .golang-ci.yml create mode 100644 .goreleaser.yml create mode 100644 .pre-commit-config.yaml create mode 100644 _config.yml create mode 100644 codecov.yml create mode 100644 commitlint.config.js create mode 100644 install.sh diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..022e304 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,32 @@ +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "daily" + time: "08:00" + labels: + - "dependencies" + commit-message: + prefix: "feat" + include: "scope" + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + time: "08:00" + labels: + - "dependencies" + commit-message: + prefix: "chore" + include: "scope" + - package-ecosystem: "docker" + directory: "/" + schedule: + interval: "daily" + time: "08:00" + labels: + - "dependencies" + commit-message: + prefix: "feat" + include: "scope" diff --git a/.github/workflows/commitlint.yml b/.github/workflows/commitlint.yml new file mode 100644 index 0000000..a754aa1 --- /dev/null +++ b/.github/workflows/commitlint.yml @@ -0,0 +1,15 @@ +name: Lint Commit Messages +on: + push: + branches: + - master + pull_request: + +jobs: + commitlint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: wagoid/commitlint-github-action@v5 diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index 567aca7..0000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: Go - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.22 - - - name: Build - run: go build -v ./... - - - name: Test - run: go test -v ./... diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..3ad73b5 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,27 @@ +name: golangci-lint +on: + push: + paths: + - '**.go' + branches: + - master + pull_request: + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version: '1.23' + + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.61 + args: -c .golang-ci.yml -v --timeout=5m + env: + GO111MODULES: off diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..54e7b63 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,34 @@ +name: Test and coverage + +on: [push, pull_request] + +jobs: + test: + strategy: + matrix: + go-version: [1.23.x] + os: [ubuntu-latest, macos-latest, windows-latest] + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 2 + + - uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + + - name: go get + run: go get ./... + + - name: go mod tidy + run: go mod tidy + + - name: Run coverage + run: go test -race -coverprofile="coverage.out" -covermode=atomic ./... + + - name: Upload coverage to Codecov + if: matrix.os == 'ubuntu-latest' + run: bash <(curl -s https://codecov.io/bash) diff --git a/.gitignore b/.gitignore index 8e0c63f..eed3676 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,13 @@ -.idea +# ide +.idea/ + +# dev .env -# go -vendor/ +# build +build/ /main +/proxynd # test tmp/ @@ -13,3 +17,8 @@ tmpconf/ *-test.yaml *-test.yml *-test.json + +# dependencies +node_modules/ +vendor/ +dist/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..c0f888f --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,42 @@ +--- +image: tetafro/golang-gcc:1.19-alpine + +stages: + - lint + - build + - test + - release + +lint: + stage: lint + before_script: + - wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.41.1 + script: + - ./bin/golangci-lint run -c .golang-ci.yml + allow_failure: true + +build: + stage: build + script: + - go build + +test: + stage: test + script: + - go test -v -race "$(go list ./... | grep -v /vendor/)" -v -coverprofile=coverage.out + - go tool cover -func=coverage.out + +release: + stage: release + image: + name: goreleaser/goreleaser:v0.164.0 + entrypoint: ["/bin/bash", "-c"] + only: + refs: + - tags + variables: + GITLAB_TOKEN: $GITLAB_TOKEN + + script: + - cd "$CI_PROJECT_DIR" + - goreleaser release --rm-dist diff --git a/.golang-ci.yml b/.golang-ci.yml new file mode 100644 index 0000000..f508d40 --- /dev/null +++ b/.golang-ci.yml @@ -0,0 +1,54 @@ +linters-settings: + lll: + line-length: 180 +linters: + # disable-all: true + # Uncomment to enable all linters + # enable-all: true + # disabling linters that are abandoned or replaced with better versions + disable: +# - golint +# - deadcode +# - exhaustivestruct +# - ifshort +# - interfacer +# - maligned +# - nosnakecase +# - scopelint +# - structcheck +# - varcheck + - testpackage + - forbidigo + - paralleltest + - varnamelen + - rowserrcheck + - sqlclosecheck + - wastedassign + - exhaustruct + - nolintlint + - wrapcheck +# manual + - unused + - revive + # additional non-default linters that we want to use + enable: +# - gochecknoglobals +# - err113 +# - revive +# - taglint +# - unused +# - gosec +# - gocritic +# - funlen + - goconst + - godox + - gofmt + - misspell + - ireturn + - lll + - nonamedreturns + - staticcheck + - ineffassign + - govet + - gosimple + - errcheck diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..8dd0232 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,149 @@ +env: + - GO111MODULE=on + - CGO_ENABLED=0 + +before: + hooks: + - ./scripts/manpages.sh + - ./scripts/completions.sh + +builds: + - + binary: gzh-manager + ldflags: -s -w -X main.version={{ .Version }} + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + +archives: + - + builds: + - gzh-manager + format_overrides: + - goos: windows + format: zip + replacements: + darwin: Darwin + linux: Linux + amd64: x86_64 + +nfpms: + - builds: + - gzh-manager + vendor: Gizzahub + homepage: https://github.com/Gizzahub/gzh-manager-go + maintainer: "Tom Morelly " + description: "A general purpose golang CLI template for Github and Gitlab" + license: GPL-3.0 + formats: + - apk + - deb + - rpm + bindir: /usr/bin + contents: + - src: ./manpages/gzh-manager.1.gz + dst: /usr/share/man/man1/gzh-manager.1.gz + file_info: + mode: 0644 + - src: ./completions/gzh-manager.bash + dst: /usr/share/bash-completion/completions/gzh-manager + file_info: + mode: 0644 + - src: ./completions/gzh-manager.fish + dst: /usr/share/fish/vendor_completions.d/gzh-manager.fish + file_info: + mode: 0644 + - src: ./completions/gzh-manager.zsh + dst: /usr/share/zsh/vendor-completions/_gzh-manager + file_info: + mode: 0644 + +dockers: +- image_templates: + - 'gizzahub/{{.ProjectName}}:{{ .Tag }}-amd64' + - 'ghcr.io/gizzahub/{{.ProjectName}}:{{ .Tag }}-amd64' + dockerfile: Dockerfile + use: buildx + build_flag_templates: + - "--pull" + - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/Gizzahub/gzh-manager/master/README.md" + - "--label=io.artifacthub.package.maintainers=[{\"name\":\"Tom Morelly\",\"email\":\"tommorelly@gmail.com\"}]" + - "--label=io.artifacthub.package.license=MIT" + - "--label=org.opencontainers.image.description=A general purpose golang CLI template for Github and Gitlab" + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.name={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=org.opencontainers.image.source={{.GitURL}}" + - "--platform=linux/amd64" +- image_templates: + - 'gizzahub/{{.ProjectName}}:{{ .Tag }}-arm64' + - 'ghcr.io/gizzahub/{{.ProjectName}}:{{ .Tag }}-arm64' + dockerfile: Dockerfile + use: buildx + build_flag_templates: + - "--pull" + - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/gizzahub/gzh-manager/master/README.md" + - "--label=io.artifacthub.package.logo-url=https://raw.githubusercontent.com/gizzahub/gzh-manager/master/www/static/images/logo.png" + - "--label=io.artifacthub.package.maintainers=[{\"name\":\"Tom Morelly\",\"email\":\"tommorelly@gmail.com\"}]" + - "--label=io.artifacthub.package.license=MIT" + - "--label=org.opencontainers.image.description=A general purpose golang CLI template for Github and Gitlab" + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.name={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=org.opencontainers.image.source={{.GitURL}}" + - "--platform=linux/arm64" + goarch: arm64 + +docker_manifests: +- name_template: 'gizzahub/{{.ProjectName}}:{{ .Tag }}' + image_templates: + - 'gizzahub/{{.ProjectName}}:{{ .Tag }}-amd64' + - 'gizzahub/{{.ProjectName}}:{{ .Tag }}-arm64' +- name_template: 'ghcr.io/gizzahub/{{.ProjectName}}:{{ .Tag }}' + image_templates: + - 'ghcr.io/gizzahub/{{.ProjectName}}:{{ .Tag }}-amd64' + - 'ghcr.io/gizzahub/{{.ProjectName}}:{{ .Tag }}-arm64' +- name_template: 'gizzahub/{{.ProjectName}}:latest' + image_templates: + - 'gizzahub/{{.ProjectName}}:{{ .Tag }}-amd64' + - 'gizzahub/{{.ProjectName}}:{{ .Tag }}-arm64' +- name_template: 'ghcr.io/gizzahub/{{.ProjectName}}:latest' + image_templates: + - 'ghcr.io/gizzahub/{{.ProjectName}}:{{ .Tag }}-amd64' + - 'ghcr.io/gizzahub/{{.ProjectName}}:{{ .Tag }}-arm64' + +checksum: + name_template: "checksums.txt" + +changelog: + sort: asc + use: github + filters: + exclude: + - '^test:' + - '^chore' + - 'merge conflict' + - Merge pull request + - Merge remote-tracking branch + - Merge branch + - go mod tidy + groups: + - title: Dependency updates + regexp: '^.*?(feat|fix)\(deps\)!?:.+$' + order: 300 + - title: 'New Features' + regexp: '^.*?feat(\([[:word:]]+\))??!?:.+$' + order: 100 + - title: 'Bug fixes' + regexp: '^.*?fix(\([[:word:]]+\))??!?:.+$' + order: 200 + - title: 'Documentation updates' + regexp: ^.*?doc(\([[:word:]]+\))??!?:.+$ + order: 400 + - title: Other work + order: 9999 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..b33187b --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,14 @@ +repos: + - repo: https://github.com/tekwizely/pre-commit-golang + rev: v1.0.0-rc.1 + hooks: + - id: go-build-mod + - id: go-test-mod + - id: go-vet-mod + - id: go-staticcheck-mod + - id: go-fmt + - id: go-fumpt + - id: go-imports + - id: go-lint + - id: golangci-lint-mod + args: [-c.golang-ci.yml] diff --git a/Dockerfile b/Dockerfile index 11d4d2a..75dd5f6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -ARG GO_VERSION=1.22 +ARG GO_VERSION=1.23.1 FROM golang:${GO_VERSION} AS builder diff --git a/Makefile b/Makefile index 047c7e8..a590291 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,15 @@ +projectname?=gzh-manager +executablename?=gzh-manager ENV=develop #DOCKER_REGISTRY=scripton-io DOCKER_REGISTRY=archmagece +default: help + +.PHONY: help +help: ## list makefile targets + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + setup: mkdir -p ~/tmp/config mkdir -p ~/tmp/storage @@ -24,11 +32,53 @@ docker-enter: @echo "Entering..." docker exec -it proxynd bash -go-update: - go get -u ./... -go-setup: - go mod tidy - go mod vendor .PHONY: build +build: ## build golang binary + @go build -ldflags "-X main.version=$(shell git describe --abbrev=0 --tags)" -o $(executablename) + +.PHONY: install +install: ## install golang binary + @go install -ldflags "-X main.version=$(shell git describe --abbrev=0 --tags)" + +.PHONY: run +run: ## run the app + @go run -ldflags "-X main.version=$(shell git describe --abbrev=0 --tags)" main.go + +.PHONY: bootstrap +bootstrap: ## install build deps + go generate -tags tools tools/tools.go + +PHONY: test +test: clean ## display test coverage + go test --cover -parallel=1 -v -coverprofile=coverage.out ./... + go tool cover -func=coverage.out | sort -rnk3 + +PHONY: clean +clean: ## clean up environment + @rm -rf coverage.out dist/ $(executablename) + +PHONY: cover +cover: ## display test coverage + go test -v -race $(shell go list ./... | grep -v /vendor/) -v -coverprofile=coverage.out + go tool cover -func=coverage.out + +PHONY: fmt +fmt: ## format go files + gofumpt -w . + gci write . + +PHONY: lint +lint: ## lint go files + golangci-lint run -c .golang-ci.yml + +.PHONY: pre-commit +pre-commit: ## run pre-commit hooks + pre-commit run --all-files + +.PHONY: deploy +deploy: + # TODO ... + # $build and deploy + cp * .$(executablename) diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..fff4ab9 --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-minimal diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..a9a3921 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,13 @@ +coverage: + status: + project: + default: + target: auto + threshold: 5% + patch: + default: + target: 50% + threshold: 5% + +ignore: + - "cmd/man.go" diff --git a/commitlint.config.js b/commitlint.config.js new file mode 100644 index 0000000..ae5250d --- /dev/null +++ b/commitlint.config.js @@ -0,0 +1,5 @@ +// commitlint.config.js +module.exports = { + extends: ['@commitlint/config-conventional'], + ignores: [(message) => /^Bumps \[.+]\(.+\) from .+ to .+\.$/m.test(message)], +} diff --git a/go.mod b/go.mod index 2b54e73..e2b2402 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module proxynd -go 1.22 +go 1.23 require ( github.com/gin-gonic/gin v1.10.0 diff --git a/handlers/proxy/docker_proxy_controller.go b/handlers/proxy/docker_proxy_controller.go index 562da58..9c3b593 100644 --- a/handlers/proxy/docker_proxy_controller.go +++ b/handlers/proxy/docker_proxy_controller.go @@ -41,14 +41,14 @@ func DockerProxy(c *gin.Context) { // Get the data //proxy := config.Proxies["docker"] for s, server := range config.Proxies { - log.Printf("Trying server %d: %s\n", s, server.URL) - resp, err := http.Get(helpers.JoinURL(server.URL, imageName, tag)) + log.Printf("Trying server %d: %s\n", s, server.Url) + resp, err := http.Get(helpers.JoinURL(server.Url, imageName, tag)) if err != nil { log.Fatal(err) return } if resp.StatusCode != http.StatusOK { - log.Printf("Server %s returned status %d\n", server.URL, resp.StatusCode) + log.Printf("Server %s returned status %d\n", server.Url, resp.StatusCode) continue } // Write the body to file diff --git a/handlers/proxy/maven_proxy_controller.go b/handlers/proxy/maven_proxy_controller.go index 192d233..3338eaa 100644 --- a/handlers/proxy/maven_proxy_controller.go +++ b/handlers/proxy/maven_proxy_controller.go @@ -85,6 +85,7 @@ func MavenProxy(c *gin.Context) { } resp.Body.Close() responseContent = bytes + // FIXME ???? ai코드?? break } } else { diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..346be5b --- /dev/null +++ b/install.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -euo pipefail + +read -rp "GitHub Username: " user +read -rp "Projectname: " projectname + +git clone git@github.com:Gizzahub/gzh-manager-go.git "$projectname" +cd "$projectname" +rm -rf .git +find . -type f -exec sed -i "s/gzh-manager-go/$projectname/g" {} + +find . -type f -exec sed -i "s/[Gg]izzahub/$user/g" {} + +git init +git add . +git commit -m "initial commit" +git remote add origin "git@github.com:$user/$projectname.git" + +echo "template successfully installed." + +go run main.go + +exit 0