Add --version and Makefile etc

This commit is contained in:
hedy 2023-06-19 12:14:08 +08:00
parent b399ace963
commit b48fd7633e
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
7 changed files with 100 additions and 6 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.out
bin

View File

@ -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.
SOFTWARE.

57
Makefile Normal file
View File

@ -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<target>\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

20
_scripts/release.sh Executable file
View File

@ -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

2
go.mod
View File

@ -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
)

4
go.sum
View File

@ -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=

View File

@ -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 <path> -h <hostname> -p <port> -d <path> ] | --help ]
fmt.Println(`Usage: spsrv [ [ -c <path> -h <hostname> -p <port> -d <path> ] | --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")