diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..9b951e2 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,38 @@ +name: release + +on: + push: + tags: + - 'v*.*' + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: v1.20.0 + args: release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Upload assets + uses: actions/upload-artifact@v3 + with: + name: dist + path: dist/* diff --git a/.gitignore b/.gitignore index e4b24e0..2300aa9 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ go.work .idea bin + +dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..125126c --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,26 @@ +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm64 + ignore: + - goos: windows + goarch: arm64 + ldflags: + - -X github.com/api7/adc/cmd.VERSION={{ .Tag }} -X github.com/api7/adc/cmd.GitRevision={{ .Commit }} +archives: + - format: tar.gz +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}" +changelog: + sort: asc + filters: + exclude: + - '^test:' diff --git a/Makefile b/Makefile index 9256927..d106e3f 100644 --- a/Makefile +++ b/Makefile @@ -2,22 +2,28 @@ PWD ?= $(shell pwd) export PATH := $(PWD)/bin:$(PATH) -default: help +VERSION ?= dev +GITSHA = $(shell git rev-parse --short=7 HEAD) +LDFLAGS = "-X github.com/api7/adc/cmd.VERSION=$(VERSION) -X github.com/api7/adc/cmd.GitRevision=$(GITSHA)" + +.DEFAULT_GOAL := help + +.PHONY: help help: ## Display this help @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) -.PHONY: help -build: ## Build adc - @go build -o bin/adc main.go .PHONY: build +build: ## Build adc + @echo "Building adc..." + @CGO_ENABLED=0 go build -o bin/adc -ldflags $(LDFLAGS) main.go +.PHONY: test test: ## Run cli test @cd test/cli && ginkgo -r -.PHONY: test +.PHONY: unit-test unit-test: ## Run unit test @go test -v $$(go list ./... | grep -v /test/) -.PHONY: unit-test .PHONY: fmt fmt: ## Format all go codes diff --git a/cmd/root.go b/cmd/root.go index 37302f8..f2db6d4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -39,6 +39,7 @@ It can be used to dump, diff, sync configurations to API7 server. rootCmd.AddCommand(newDiffCmd()) rootCmd.AddCommand(newSyncCmd()) rootCmd.AddCommand(newValidateCmd()) + rootCmd.AddCommand(newVersionCmd()) return rootCmd } diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..06eb608 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "github.com/fatih/color" + "github.com/spf13/cobra" +) + +var ( + VERSION = "dev" + GitRevision = "unknown" +) + +// newVersionCmd represents the version command +func newVersionCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Short: "Print the version of adc", + Long: `The version command can be used to print the version of adc.`, + Run: func(cmd *cobra.Command, args []string) { + color.Green("adc version: %s - %s\n", VERSION, GitRevision) + }, + } + + return cmd +}