diff --git a/.gitignore b/.gitignore index 80d75b7..dedc516 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -go-import-redirector +/go-import-redirector go-import-redirector.yaml **.old diff --git a/.woodpecker/build.yaml b/.woodpecker/build.yaml index 914af97..f5c6571 100644 --- a/.woodpecker/build.yaml +++ b/.woodpecker/build.yaml @@ -1,7 +1,7 @@ variables: - - &golang_image 'golang:1.20' + - &golang_image 'golang:1.21' - &goproxy_override '' - &goproxy_setup |- if [ -n "$${GOPROXY_OVERRIDE:-}" ]; then diff --git a/Makefile b/Makefile index 45696e1..f0ae25a 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,14 @@ SHASUM ?= shasum -a 256 # Construct the version strings -# VERSION default to dev or in CI next +# VERSION default to dev or in CI next for default branch, and branch name for everything else... ifneq ($(CI),) # in CI - VERSION ?= next + ifeq ($(CI_REPO_DEFAULT_BRANCH),$(CI_COMMIT_BRANCH)) + VERSION ?= next + else + VERSION ?= $(CI_COMMIT_BRANCH) + endif else # not in CI VERSION ?= dev @@ -34,8 +38,11 @@ VERSION_NUMBER ?= 0.0.0 # Use CI_COMMIT_TAG from CI if set as version and version number ifneq ($(CI_COMMIT_TAG),) + ISTAG := "true" VERSION := $(CI_COMMIT_TAG:v%=%) VERSION_NUMBER := ${VERSION} +else + ISTAG := "false" endif # Use CI provided SHA, else use git @@ -46,6 +53,8 @@ endif SOURCE_SHA ?= $(shell git rev-parse HEAD) SOURCE_SHA_SHORT := $(shell echo ${SOURCE_SHA} | head -c 8) +COMMITTIME = $(shell git log -1 --format=%ct | xargs -I{} date -u +%Y%m%d%H%M%S -d "@{}") + # VERSIONED_BINARY is the base output name for xgo VERSIONED_BINARY := $(BINARY_BASE_NAME)-$(VERSION) @@ -55,7 +64,13 @@ VERSIONED_BINARY := $(BINARY_BASE_NAME)-$(VERSION) # WP CI=woodpecker CI_COMMIT_TAG CI_REPO_DEFAULT_BRANCH CI_COMMIT_SHA CI_COMMIT_PULL_REQUEST # Default LDFlags, stripping, dwarfing, statically linking and adding the version and commit to pkg/meta, extendable via LDFLAGS -LDFLAGS := $(LDFLAGS) -s -w -X ${GO_PACKAGE}/pkg/meta.version=${VERSION} -X ${GO_PACKAGE}/pkg/meta.commit=${SOURCE_SHA} +LDFLAGS := $(LDFLAGS) -s -w +LDFLAGS := $(LDFLAGS) -X ${GO_PACKAGE}/pkg/meta.version=${VERSION} +LDFLAGS := $(LDFLAGS) -X ${GO_PACKAGE}/pkg/meta.commit=${SOURCE_SHA} +LDFLAGS := $(LDFLAGS) -X ${GO_PACKAGE}/pkg/meta.commitTime=${COMMITTIME} +LDFLAGS := $(LDFLAGS) -X ${GO_PACKAGE}/pkg/meta.isTag=${ISTAG} +LDFLAGS := $(LDFLAGS) -X ${GO_PACKAGE}/pkg/meta.branch=${CI_COMMIT_BRANCH} +LDFLAGS := $(LDFLAGS) -X ${GO_PACKAGE}/pkg/meta.defaultBranch=${CI_REPO_DEFAULT_BRANCH} # Disable cgo, but overridable CGO_ENABLED ?= 0 diff --git a/cmd/root.go b/cmd/root.go index df172fa..93dce5c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -42,7 +42,7 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - rootCmd.Flags().StringVarP(&cfgFile, "config", "c", "", "config file (default order is $HOME/go-import-redirector.yaml, /etc/go-import-redirector.yaml, ./go-import-redirector.yaml)") + rootCmd.Flags().StringVarP(&cfgFile, "config", "c", "", "config file (default order is $HOME/go-import-redirector.yaml, /etc/go-import-redirector/go-import-redirector.yaml, ./go-import-redirector.yaml)") rootCmd.Flags().StringVarP(&bind, "bind", "b", ":3000", "the address and port on which to serve the webserver (default :3000)") rootCmd.Flags().BoolVarP(&hotReload, "hotreload", "r", true, "Change hot reloading (default true)") } diff --git a/docker/fscopy/etc/go-import-redirector/.gitkeep b/docker/fscopy/etc/go-import-redirector/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod index 6cef000..c82142f 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,12 @@ module git.kle.li/tools/go-import-redirector -go 1.20 +go 1.21.5 require ( github.com/fsnotify/fsnotify v1.6.0 github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.15.0 + go.kle.li/tools/go-meta v0.0.1 ) require ( diff --git a/go.sum b/go.sum index f5a4c69..6f1f334 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= +github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -99,6 +100,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -131,9 +133,11 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -147,6 +151,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk= github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= @@ -177,6 +182,8 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.kle.li/tools/go-meta v0.0.1 h1:0ShRamAYgITCUE6DiCbkwzWe6aEo6aHyfS25umzRaXc= +go.kle.li/tools/go-meta v0.0.1/go.mod h1:B05kZ1niBY70bR2C355aafM4PF1quBFAAFYFVBZCXo0= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= diff --git a/pkg/configuration/configuration.go b/pkg/configuration/configuration.go index 128ca94..e0aa68e 100644 --- a/pkg/configuration/configuration.go +++ b/pkg/configuration/configuration.go @@ -38,7 +38,7 @@ func New(cfgFile string, hotReload bool, reloadFunction func()) *Configuration { fmt.Println("Unable to determine $HOME, not searching it for potential config") } - c.v.AddConfigPath("/etc/") + c.v.AddConfigPath("/etc/go-import-redirector/") c.v.AddConfigPath(".") c.v.SetConfigType("yaml") c.v.SetConfigName("go-import-redirector") diff --git a/pkg/meta/meta.go b/pkg/meta/meta.go index 74b1911..a65a56b 100644 --- a/pkg/meta/meta.go +++ b/pkg/meta/meta.go @@ -3,24 +3,42 @@ package meta -import "strings" - -var ( - version = "dev" - commit = "none" +import ( + "go.kle.li/tools/go-meta/buildmeta" + "go.kle.li/tools/go-meta/shared" ) +var ( + isTag = "false" + branch = "" + defaultBranch = "" + version = "" + commit = "" + commitTime = "" +) + +var mdp = buildmeta.NewPackageMetadata(&shared.PackageMetadataInput{ + IsTag: isTag, + Branch: branch, + Version: version, + CommitSha: commit, + CommitTime: commitTime, + DefaultBranch: defaultBranch, +}) + func GetVersion() string { - return strings.TrimSpace(version) + return mdp.Version +} + +func GetPackageVersion() string { + return mdp.GoCommitVersion } func GetCommit() string { - return strings.TrimSpace(commit) + return mdp.CommitSha } func GetShortCommit() string { - if len(GetCommit()) <= 8 { - return GetCommit() - } - return GetCommit()[:8] + _, slug := mdp.GetCommitShaSlug(8) + return slug }