set version from git tag.

This commit is contained in:
Miroslav Šedivý 2022-07-04 19:14:52 +02:00
parent 1064cc4eb5
commit e181abf577
3 changed files with 27 additions and 27 deletions

View File

@ -17,8 +17,9 @@ RUN set -eux; \
apt-get clean -y; \
rm -rf /var/lib/apt/lists/* /var/cache/apt/*
ARG GIT_COMMIT=dev
ARG GIT_BRANCH=dev
ARG GIT_COMMIT
ARG GIT_BRANCH
ARG GIT_TAG
#
# build server
@ -49,8 +50,8 @@ RUN set -eux; \
# needed for profile upload preStop hook
zip curl \
#
# file chooser handler, clipboard
xdotool xclip \
# file chooser handler, clipboard, drop
xdotool xclip libgtk-3-0 \
#
# gst
gstreamer1.0-plugins-base gstreamer1.0-plugins-good \

10
build
View File

@ -13,10 +13,11 @@ fi
#
# set git build variables if git exists
if git version > /dev/null && [ -z $GIT_COMMIT ] && [ -z $GIT_COMMIT ];
if git version > /dev/null && [ -z $GIT_COMMIT ] && [ -z $GIT_BRANCH ] && [ -z $GIT_TAG ];
then
GIT_COMMIT=`git rev-parse --short HEAD`
GIT_BRANCH=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
GIT_TAG=`git tag --points-at $GIT_COMMIT | head -n 1`
fi
#
@ -29,9 +30,10 @@ go build \
-o bin/neko \
-ldflags "
-s -w
-X 'demodesk/neko.buildDate=`date -u +'%Y-%m-%dT%H:%M:%SZ'`'
-X 'demodesk/neko.gitCommit=${GIT_COMMIT}'
-X 'demodesk/neko.gitBranch=${GIT_BRANCH}'
-X 'gitlab.com/demodesk/neko/server.buildDate=`date -u +'%Y-%m-%dT%H:%M:%SZ'`'
-X 'gitlab.com/demodesk/neko/server.gitCommit=${GIT_COMMIT}'
-X 'gitlab.com/demodesk/neko/server.gitBranch=${GIT_BRANCH}'
-X 'gitlab.com/demodesk/neko/server.gitTag=${GIT_TAG}'
" \
cmd/neko/main.go;

35
neko.go
View File

@ -3,6 +3,7 @@ package neko
import (
"fmt"
"runtime"
"strings"
)
const Header = `&34
@ -11,7 +12,7 @@ const Header = `&34
/ |/ / _ \/ //_/ __ \ ) ( ')
/ /| / __/ ,< / /_/ / ( / )
/_/ |_/\___/_/|_|\____/ \(__)|
&1&37 nurdism/m1k1o &33%s v%s&0
&1&37 nurdism/m1k1o &33%s %s&0
`
var (
@ -21,21 +22,14 @@ var (
gitCommit = "dev"
//
gitBranch = "dev"
// Major version when you make incompatible API changes.
major = "dev"
// Minor version when you add functionality in a backwards-compatible manner.
minor = "dev"
// Patch version when you make backwards-compatible bug fixes.
patch = "dev"
//
gitTag = "dev"
)
var Version = &version{
Major: major,
Minor: minor,
Patch: patch,
GitCommit: gitCommit,
GitBranch: gitBranch,
GitTag: gitTag,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
@ -43,11 +37,9 @@ var Version = &version{
}
type version struct {
Major string
Minor string
Patch string
GitCommit string
GitBranch string
GitTag string
BuildDate string
GoVersion string
Compiler string
@ -55,18 +47,23 @@ type version struct {
}
func (i *version) String() string {
return fmt.Sprintf("%s.%s.%s %s", i.Major, i.Minor, i.Patch, i.GitCommit)
version := i.GitTag
if version == "" || version == "dev" {
version = i.GitBranch
}
return fmt.Sprintf("%s@%s", version, i.GitCommit)
}
func (i *version) Details() string {
return fmt.Sprintf(
"%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
fmt.Sprintf("Version %s.%s.%s", i.Major, i.Minor, i.Patch),
return "\n" + strings.Join([]string{
fmt.Sprintf("Version %s", i.String()),
fmt.Sprintf("GitCommit %s", i.GitCommit),
fmt.Sprintf("GitBranch %s", i.GitBranch),
fmt.Sprintf("GitTag %s", i.GitTag),
fmt.Sprintf("BuildDate %s", i.BuildDate),
fmt.Sprintf("GoVersion %s", i.GoVersion),
fmt.Sprintf("Compiler %s", i.Compiler),
fmt.Sprintf("Platform %s", i.Platform),
)
}, "\n") + "\n"
}