From b48fd7633eb4d1bce0325c0558897d2aedccdde0 Mon Sep 17 00:00:00 2001 From: hedy Date: Mon, 19 Jun 2023 12:14:08 +0800 Subject: [PATCH] Add --version and Makefile etc --- .gitignore | 2 ++ LICENSE | 4 ++-- Makefile | 57 +++++++++++++++++++++++++++++++++++++++++++++ _scripts/release.sh | 20 ++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- spsrv.go | 17 +++++++++++++- 7 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100755 _scripts/release.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..377735d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.out +bin diff --git a/LICENSE b/LICENSE index e6cff00..1ca0e3c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 Hedy Li +Copyright (c) 2021-2023 hedy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..31afbc9 --- /dev/null +++ b/Makefile @@ -0,0 +1,57 @@ +.PHONY: help build clean build-all package release +.DEFAULT_GOAL := help + +pkg_root = git.sr.ht/~hedy/spsrv + +### Calculate a few variables for use in building +VERSION = $(shell git describe --tags --abbrev=0 --always) +COMMIT = $(shell git log --pretty='format:%h' -n 1) +BUILDDATE = $(shell date +"%Y-%m-%dT%H:%M:%S") +# ldflags inject new values into variables at compilation time +# this is how we dynamically set the version/etc of the application +ldflags = "-X 'main.appVersion=$(VERSION)' \ + -X 'main.appCommit=$(COMMIT)' \ + -X 'main.buildTime=$(BUILDDATE)' \ + -w -s" + +##@ Help +help: ## Display this help + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) + +##@ Utilities +init: ## Install utils + go mod download + +dep-tidy: ## Remove unused dependencies + go mod tidy + +dep-upgrade: ## Upgrade versions of dependencies + go get -u + +##@ Build +build: clean ## Build spsrv for your native architecture + go build -o ./bin/spsrv -ldflags=$(ldflags) $(pkg_root) + +build-all: clean ## Build spsrv for linux and mac + GOOS=darwin GOARCH=amd64 go build -o ./bin/spsrv-darwin-amd64/spsrv -ldflags=$(ldflags) $(pkg_root) + GOOS=darwin GOARCH=arm64 go build -o ./bin/spsrv-darwin-arm64/spsrv -ldflags=$(ldflags) $(pkg_root) + GOOS=linux GOARCH=amd64 go build -o ./bin/spsrv-linux-amd64/spsrv -ldflags=$(ldflags) $(pkg_root) + GOOS=linux GOARCH=arm64 go build -o ./bin/spsrv-linux-arm64/spsrv -ldflags=$(ldflags) $(pkg_root) + +clean: ## Delete any compiled artifacts + rm -rf ./bin + +##@ Release +package: build-all ## Build everything and package up arch-specific tarballs + tar czvf ./bin/spsrv-darwin-amd64.tar.gz ./bin/spsrv-darwin-amd64 + tar czvf ./bin/spsrv-darwin-arm64.tar.gz ./bin/spsrv-darwin-arm64 + tar czvf ./bin/spsrv-linux-amd64.tar.gz ./bin/spsrv-linux-amd64 + tar czvf ./bin/spsrv-linux-arm64.tar.gz ./bin/spsrv-linux-arm64 + +release: package ## Attach packages to sr.ht ref for current tag + ./_scripts/release.sh + +##@ Test +test: ## Run tests + go test $(shell go list ./...) -coverprofile=coverage.out + # go tool cover -func=coverage.out diff --git a/_scripts/release.sh b/_scripts/release.sh new file mode 100755 index 0000000..4a37da5 --- /dev/null +++ b/_scripts/release.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env sh +set -e + +git_tag=`git describe --exact-match 2> /dev/null || echo ""` +if [ "$git_tag" != "" ]; then + echo "Releasing $git_tag!" + mv ./bin/spsrv-darwin-amd64.tar.gz ./bin/spsrv-darwin-amd64-$git_tag.tar.gz + mv ./bin/spsrv-darwin-arm64.tar.gz ./bin/spsrv-darwin-arm64-$git_tag.tar.gz + mv ./bin/spsrv-linux-amd64.tar.gz ./bin/spsrv-linux-amd64-$git_tag.tar.gz + mv ./bin/spsrv-linux-arm64.tar.gz ./bin/spsrv-linux-arm64-$git_tag.tar.gz + + curl -H"Authorization: token $SRHT_TOKEN" https://git.sr.ht/api/~hedy/repos/spsrv/artifacts/$git_tag -F "file=@./bin/spsrv-darwin-amd64-$git_tag.tar.gz" + curl -H"Authorization: token $SRHT_TOKEN" https://git.sr.ht/api/~hedy/repos/spsrv/artifacts/$git_tag -F "file=@./bin/spsrv-darwin-arm64-$git_tag.tar.gz" + curl -H"Authorization: token $SRHT_TOKEN" https://git.sr.ht/api/~hedy/repos/spsrv/artifacts/$git_tag -F "file=@./bin/spsrv-linux-amd64-$git_tag.tar.gz" + curl -H"Authorization: token $SRHT_TOKEN" https://git.sr.ht/api/~hedy/repos/spsrv/artifacts/$git_tag -F "file=@./bin/spsrv-linux-arm64-$git_tag.tar.gz" + echo "" + echo "DONE!" +else + echo "Non-tagged commit, not releasing!" +fi diff --git a/go.mod b/go.mod index b924d5d..d5a4ef2 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module spsrv go 1.15 require ( - github.com/BurntSushi/toml v0.3.1 + github.com/BurntSushi/toml v1.3.2 github.com/spf13/pflag v1.0.5 ) diff --git a/go.sum b/go.sum index 33499b6..0929dd8 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/spsrv.go b/spsrv.go index 46e253b..a72e458 100644 --- a/spsrv.go +++ b/spsrv.go @@ -52,13 +52,21 @@ var ( rootDir = flag.StringP("dir", "d", cliDefaultChar, "Root content directory") confPath = flag.StringP("config", "c", "/etc/spsrv.conf", "Path to config file") helpFlag = flag.BoolP("help", "?", false, "Get CLI help") + versionFlag = flag.BoolP("version", "v", false, "View version and exit") ) +var ( + appVersion = "unknown version" + buildTime = "date unknown" + appCommit = "unknown" +) + + func main() { // Custom usage function because we don't want the "pflag: help requested" message, and // we don't want to show the default values. flag.Usage = func() { - fmt.Println(`Usage: spsrv [ [ -c -h -p -d ] | --help ] + fmt.Println(`Usage: spsrv [ [ -c -h -p -d ] | --help | --version ] -c, --config string Path to config file -d, --dir string Root content directory @@ -71,6 +79,13 @@ func main() { flag.Usage() return } + + if *versionFlag { + fmt.Printf("spsrv %s, commit %s, built %s", appVersion, appCommit, buildTime) + return + } + + conf, err := LoadConfig(*confPath) if err != nil { fmt.Println("Error loading config")