Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version command #18

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -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/*
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
go.work
.idea
bin

dist/
26 changes: 26 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -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:'
18 changes: 12 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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<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)
.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
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
25 changes: 25 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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
}