Skip to content

Commit

Permalink
feature version flag
Browse files Browse the repository at this point in the history
Signed-off-by: xiexianbin <[email protected]>
  • Loading branch information
xiexianbin committed May 12, 2024
1 parent b93567d commit c3b43bc
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 41 deletions.
1 change: 1 addition & 0 deletions .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/first-interaction@v1
continue-on-error: true
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
issue-message: "# Welcome.\nThanks for your issue."
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/labeler@v2
- uses: actions/labeler@v4
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
7 changes: 7 additions & 0 deletions .github/workflows/licensed.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: Licensed

on:
workflow_dispatch:
inputs:
reason:
description: 'run license action reason'
required: false
type: string
default: 'manually test'
push:
branches:
- main
Expand Down
24 changes: 15 additions & 9 deletions .github/workflows/release-new-action-version.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
name: Release new action version
on:
workflow_dispatch:
inputs:
reason:
description: 'run release action reason'
required: false
type: string
default: 'manually test'
push:
tags:
- "v*.*.*"
Expand All @@ -8,14 +15,9 @@ permissions:
contents: write

jobs:
build:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Release
uses: softprops/action-gh-release@v1

- name: Checkout
uses: actions/checkout@v3

Expand All @@ -40,8 +42,12 @@ jobs:
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
bin/xca-linux-amd64
bin/xca-linux-arm64
bin/xca-linux-ppc64le
bin/xca-linux-s390x
bin/xca-darwin-amd64
bin/xca-darwin-arm64
bin/xca-windows-amd64.exe
Release.txt
LICENSE
bin/xca-linux
bin/xca-darwin
bin/xca-windows
19 changes: 16 additions & 3 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
name: build-test
name: workflow
on:
workflow_dispatch:
inputs:
reason:
description: 'run action reason'
required: false
type: string
default: 'manually test'
push:
branches:
- main
- dev
- bug/**
- fix/**
- bugfix/**
- feature/**
- release/**
paths-ignore:
- '**.md'
pull_request:
Expand All @@ -14,14 +27,14 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.17.0-rc.2', '1.16.1' ]
go: [ '1.19.11', '1.20.6', '1.21.10' ]
name: Go ${{ matrix.go }} test
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}

Expand Down
132 changes: 105 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,108 @@
# https://www.xiexianbin.cn/program/tools/2016-01-09-makefile/index.html
.PHONY: all test clean build build-linux build-mac build-windows

GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
BINARY_NAME=xca
BINARY_LINUX=$(BINARY_NAME)-linux
BINARY_MAC=$(BINARY_NAME)-darwin
BINARY_WIN=$(BINARY_NAME)-windows

help: ## Show this help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

all: clean test build build-linux build-mac build-windows ## Build all
test: ## run test
export SHELL:=bash
export SHELLOPTS:=$(if $(SHELLOPTS),$(SHELLOPTS):)pipefail:errexit

# https://stackoverflow.com/questions/4122831/disable-make-builtin-rules-and-variables-from-inside-the-make-file
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:

VERSION := latest
BUILD_DATE := $(shell TZ=UTC-8 date +'%Y-%m-%dT%H:%M:%SZ+08:00')
GIT_COMMIT := $(shell git rev-parse HEAD || echo unknown)
GIT_BRANCH := $(shell git rev-parse --symbolic-full-name --verify --quiet --abbrev-ref HEAD)
GIT_TAG := $(shell git describe --exact-match --tags --abbrev=0 2> /dev/null || echo untagged)
GIT_TREE_STATE := $(shell if [ -z "`git status --porcelain`" ]; then echo "clean" ; else echo "dirty"; fi)
RELEASE_TAG := $(shell if [[ "$(GIT_TAG)" =~ ^v[0-9]+\.[0-9]+\.[0-9]+.*$$ ]]; then echo "true"; else echo "false"; fi)
DEV_BRANCH := $(shell [ "$(GIT_BRANCH)" = master ] || [ `echo $(GIT_BRANCH) | cut -c -8` = release- ] || [ `echo $(GIT_BRANCH) | cut -c -4` = dev- ] || [ $(RELEASE_TAG) = true ] && echo false || echo true)

GOCMD ?= go
GOBUILD ?= $(GOCMD) build -v
GOCLEAN ?= $(GOCMD) clean
GOTEST ?= $(GOCMD) test -v -p 20

linux-amd64: GOARGS = GOOS=linux GOARCH=amd64
linux-arm64: GOARGS = GOOS=linux GOARCH=arm64
linux-ppc64le: GOARGS = GOOS=linux GOARCH=ppc64le
linux-s390x: GOARGS = GOOS=linux GOARCH=s390x
darwin-amd64: GOARGS = GOOS=darwin GOARCH=amd64
darwin-arm64: GOARGS = GOOS=darwin GOARCH=arm64
windows-amd64: GOARGS = GOOS=windows GOARCH=amd64

BINARY_NAME ?= main
IMG ?= xiexianbin/go-actions-demo:latest

ifeq ($(RELEASE_TAG),true)
VERSION := $(GIT_TAG)
endif

# $(info GIT_COMMIT=$(GIT_COMMIT) GIT_BRANCH=$(GIT_BRANCH) GIT_TAG=$(GIT_TAG) GIT_TREE_STATE=$(GIT_TREE_STATE) RELEASE_TAG=$(RELEASE_TAG) DEV_BRANCH=$(DEV_BRANCH) VERSION=$(VERSION))
# $(info MAKEFILE_LIST=${MAKEFILE_LIST})

# -X github.com/xiexianbin/go-actions-demo.version=$(VERSION)
override LDFLAGS += \
-X main.version=$(VERSION) \
-X main.buildDate=$(BUILD_DATE) \
-X main.gitCommit=$(GIT_COMMIT) \
-X main.gitTreeState=$(GIT_TREE_STATE)

ifneq ($(GIT_TAG),)
override LDFLAGS += -X main.gitTag=${GIT_TAG}
endif

SUB_BUILD_CMD ?= $(GOBUILD) -gcflags '${GCFLAGS}' -ldflags '${LDFLAGS} -extldflags -static'

.PHONY: help
help: ## Show this help
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

.PHONY: all
all: clean test build linux-amd64 linux-arm64 linux-ppc64le linux-s390x darwin-amd64 darwin-arm64 windows-amd64 ## Build all

.PHONY: test
test: ## Run test
$(GOTEST) -v ./...
clean: ## run clean bin files

.PHONY: clean
clean: ## Run clean bin files
$(GOCLEAN)
rm -f bin/$(BINARY_NAME)
build: ## build for current os
$(GOBUILD) -o bin/$(BINARY_NAME) -v

build-linux: ## build linux amd64
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o bin/$(BINARY_LINUX) -v
build-mac: ## build mac amd64
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o bin/$(BINARY_MAC) -v
build-windows: ## build windows amd64
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o bin/$(BINARY_WIN) -v
rm -f bin/*

.PHONY: build
build: ## Build for current os
${SUB_BUILD_CMD} -o bin/$(BINARY_NAME)

.PHONY: linux-amd64
linux-amd64: ## Build linux amd64
CGO_ENABLED=0 ${GOARGS} ${SUB_BUILD_CMD} -o bin/${BINARY_NAME}-$@

.PHONY: linux-arm64
linux-arm64: ## Build linux arm64
CGO_ENABLED=0 ${GOARGS} ${SUB_BUILD_CMD} -o bin/${BINARY_NAME}-$@

.PHONY: linux-ppc64le
linux-ppc64le: ## Build linux ppc64le
CGO_ENABLED=0 ${GOARGS} ${SUB_BUILD_CMD} -o bin/${BINARY_NAME}-$@

.PHONY: linux-s390x
linux-s390x: ## Build linux s390x
CGO_ENABLED=0 ${GOARGS} ${SUB_BUILD_CMD} -o bin/${BINARY_NAME}-$@

.PHONY: darwin-amd64
darwin-amd64: ## Build darwin amd64
CGO_ENABLED=0 ${GOARGS} ${SUB_BUILD_CMD} -o bin/${BINARY_NAME}-$@

.PHONY: darwin-arm64
darwin-arm64: ## Build darwin arm64
CGO_ENABLED=0 ${GOARGS} ${SUB_BUILD_CMD} -o bin/${BINARY_NAME}-$@

.PHONY: windows-amd64
windows-amd64: ## Build windows amd64
CGO_ENABLED=0 ${GOARGS} ${SUB_BUILD_CMD} -o bin/${BINARY_NAME}-$@.exe

.PHONY: docker-build
docker-build: test ## Build docker image
docker build -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image
docker push ${IMG}
18 changes: 18 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Security Policy

If you have discovered a security vulnerability in this project, please report it
privately. **Do not disclose it as a public issue.** This gives us time to work with you
to fix the issue before public exposure, reducing the chance that the exploit will be
used before a patch is released.

You may submit the report in the following ways:

- send an email to [email protected]
- send us a [private vulnerability report](https://github.com/x-ca/go-ca/security/advisories/new)

Please provide the following information in your report:

- A description of the vulnerability and its impact
- How to reproduce the issue

We ask that you give us 90 days to work on a fix before public exposure.
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
ipStr string
ips []net.IP
help bool
showVersion bool
)

func init() {
Expand All @@ -50,7 +51,8 @@ func init() {
flag.StringVar(&domainStr, "domains", "", "Comma-Separated domain names.")
flag.StringVar(&commonName, "cn", "", "sign cert common name.")
flag.StringVar(&ipStr, "ips", "", "Comma-Separated IP addresses.")
flag.BoolVar(&help, "help", false, "show help message")
flag.BoolVar(&help, "help", false, "show help message.")
flag.BoolVar(&showVersion, "version", false, "show version info.")

flag.Parse()

Expand Down Expand Up @@ -170,6 +172,12 @@ func main() {
return
}

if showVersion {
v := GetVersion()
PrintVersion("go-ca", v, false)
return
}

// create CA
if createCa {
if err := doCreateCa(); err != nil {
Expand Down
73 changes: 73 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
"runtime"
)

var (
version = "v0.0.0" // value from VERSION file
buildDate = "1970-01-01T00:00:00Z" // output from `TZ=UTC-8 date +'%Y-%m-%dT%H:%M:%SZ+08:00'`
gitCommit = "" // output from `git rev-parse HEAD`
gitTag = "" // output from `git describe --exact-match --tags HEAD` (if clean tree state)
gitTreeState = "" // determined from `git status --porcelain`. either 'clean' or 'dirty'
)

type Version struct {
Version string `json:"version"`
BuildDate string `json:"buildDate"`
GitCommit string `json:"gitCommit"`
GitTag string `json:"gitTag"`
GitTreeState string `json:"gitTreeState"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
}

// GetVersion returns the version information
func GetVersion() Version {
var versionStr string
if gitCommit != "" && gitTag != "" && gitTreeState == "clean" {
// if we have a clean tree state and the current commit is tagged,
// this is an official release.
versionStr = gitTag
} else {
// otherwise formulate a version string based on as much metadata
// information we have available.
versionStr = version
if len(gitCommit) >= 7 {
versionStr += "+" + gitCommit[0:7]
if gitTreeState != "clean" {
versionStr += ".dirty"
}
} else {
versionStr += "+unknown"
}
}
return Version{
Version: versionStr,
BuildDate: buildDate,
GitCommit: gitCommit,
GitTag: gitTag,
GitTreeState: gitTreeState,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

func PrintVersion(cliName string, version Version, short bool) {
fmt.Printf("%s: %s\n", cliName, version.Version)
if short {
return
}
fmt.Printf(" BuildDate: %s\n", version.BuildDate)
fmt.Printf(" GitCommit: %s\n", version.GitCommit)
fmt.Printf(" GitTreeState: %s\n", version.GitTreeState)
if version.GitTag != "" {
fmt.Printf(" GitTag: %s\n", version.GitTag)
}
fmt.Printf(" GoVersion: %s\n", version.GoVersion)
fmt.Printf(" Compiler: %s\n", version.Compiler)
fmt.Printf(" Platform: %s\n", version.Platform)
}

0 comments on commit c3b43bc

Please sign in to comment.