Skip to content

Commit eb03f11

Browse files
feat: add version command (#18)
Signed-off-by: Jintao Zhang <[email protected]>
1 parent 5462d6e commit eb03f11

File tree

6 files changed

+104
-6
lines changed

6 files changed

+104
-6
lines changed

.github/workflows/release.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*'
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
goreleaser:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v3
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Set up Go
21+
uses: actions/setup-go@v4
22+
with:
23+
go-version: '1.20'
24+
25+
- name: Run GoReleaser
26+
uses: goreleaser/goreleaser-action@v4
27+
with:
28+
distribution: goreleaser
29+
version: v1.20.0
30+
args: release
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: Upload assets
35+
uses: actions/upload-artifact@v3
36+
with:
37+
name: dist
38+
path: dist/*

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
go.work
2222
.idea
2323
bin
24+
25+
dist/

.goreleaser.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
builds:
2+
- env:
3+
- CGO_ENABLED=0
4+
goos:
5+
- linux
6+
- windows
7+
- darwin
8+
goarch:
9+
- amd64
10+
- arm64
11+
ignore:
12+
- goos: windows
13+
goarch: arm64
14+
ldflags:
15+
- -X github.com/api7/adc/cmd.VERSION={{ .Tag }} -X github.com/api7/adc/cmd.GitRevision={{ .Commit }}
16+
archives:
17+
- format: tar.gz
18+
checksum:
19+
name_template: 'checksums.txt'
20+
snapshot:
21+
name_template: "{{ .Tag }}"
22+
changelog:
23+
sort: asc
24+
filters:
25+
exclude:
26+
- '^test:'

Makefile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22
PWD ?= $(shell pwd)
33
export PATH := $(PWD)/bin:$(PATH)
44

5-
default: help
5+
VERSION ?= dev
6+
GITSHA = $(shell git rev-parse --short=7 HEAD)
7+
LDFLAGS = "-X github.com/api7/adc/cmd.VERSION=$(VERSION) -X github.com/api7/adc/cmd.GitRevision=$(GITSHA)"
8+
9+
.DEFAULT_GOAL := help
10+
11+
.PHONY: help
612
help: ## Display this help
713
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\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)
8-
.PHONY: help
914

10-
build: ## Build adc
11-
@go build -o bin/adc main.go
1215
.PHONY: build
16+
build: ## Build adc
17+
@echo "Building adc..."
18+
@CGO_ENABLED=0 go build -o bin/adc -ldflags $(LDFLAGS) main.go
1319

20+
.PHONY: test
1421
test: ## Run cli test
1522
@cd test/cli && ginkgo -r
16-
.PHONY: test
1723

24+
.PHONY: unit-test
1825
unit-test: ## Run unit test
1926
@go test -v $$(go list ./... | grep -v /test/)
20-
.PHONY: unit-test
2127

2228
.PHONY: fmt
2329
fmt: ## Format all go codes

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ It can be used to dump, diff, sync configurations to API7 server.
3939
rootCmd.AddCommand(newDiffCmd())
4040
rootCmd.AddCommand(newSyncCmd())
4141
rootCmd.AddCommand(newValidateCmd())
42+
rootCmd.AddCommand(newVersionCmd())
4243
return rootCmd
4344
}
4445

cmd/version.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import (
4+
"github.com/fatih/color"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var (
9+
VERSION = "dev"
10+
GitRevision = "unknown"
11+
)
12+
13+
// newVersionCmd represents the version command
14+
func newVersionCmd() *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "version",
17+
Short: "Print the version of adc",
18+
Long: `The version command can be used to print the version of adc.`,
19+
Run: func(cmd *cobra.Command, args []string) {
20+
color.Green("adc version: %s - %s\n", VERSION, GitRevision)
21+
},
22+
}
23+
24+
return cmd
25+
}

0 commit comments

Comments
 (0)