From f3480f8f8daa90ac00409881a98ab18cc5c3647c Mon Sep 17 00:00:00 2001 From: Dimitris Karakasilis Date: Thu, 21 Mar 2024 10:25:47 +0200 Subject: [PATCH] Don't hardcode the version (#353) and add a `version` command that only prints the version (without the build time) Signed-off-by: Dimitris Karakasilis --- .goreleaser.yml | 4 ++-- Makefile | 2 +- cmd/root.go | 9 ++++----- cmd/version.go | 19 +++++++++++++++++++ go.sum | 4 ++-- main.go | 4 ---- 6 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 cmd/version.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 3e362499..9627442e 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -13,8 +13,8 @@ builds: - CGO_ENABLED=0 ldflags: - -s -w + - -X "github.com/mudler/luet/cmd.Version={{ .Summary }}" - -X "github.com/mudler/luet/cmd.BuildTime={{ time "2006-01-02 15:04:05 MST" }}" - - -X "github.com/mudler/luet/cmd.BuildCommit={{ .FullCommit }}" goos: - linux goarch: @@ -40,4 +40,4 @@ release: Luet is a multi-platform Package Manager based off from containers - it uses Docker (and others) to build packages. It has zero dependencies and it is well suitable for "from scratch" environments. - It can also version entire rootfs and enables delivery of OTA-alike updates, making it a perfect fit for the Edge computing era and IoT embedded device \ No newline at end of file + It can also version entire rootfs and enables delivery of OTA-alike updates, making it a perfect fit for the Edge computing era and IoT embedded device diff --git a/Makefile b/Makefile index 3674ad89..db3a2462 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # go tool nm ./luet | grep Commit override LDFLAGS += -X "github.com/mudler/luet/cmd.BuildTime=$(shell date -u '+%Y-%m-%d %I:%M:%S %Z')" -override LDFLAGS += -X "github.com/mudler/luet/cmd.BuildCommit=$(shell git rev-parse HEAD)" +override LDFLAGS += -X "github.com/mudler/luet/cmd.Version=$(shell git describe --dirty --always --tags)" NAME ?= luet PACKAGE_NAME ?= $(NAME) diff --git a/cmd/root.go b/cmd/root.go index 9874dd4b..fb831ea2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,8 +30,7 @@ var cfgFile string var Verbose bool const ( - LuetCLIVersion = "0.34.0" - LuetEnvPrefix = "LUET" + LuetEnvPrefix = "LUET" ) var license = []string{ @@ -45,12 +44,12 @@ var license = []string{ // // ⚠️ WARNING: should only be set by "-ldflags". var ( - BuildTime string - BuildCommit string + BuildTime string + Version = "0.0.0" ) func version() string { - return fmt.Sprintf("%s-g%s %s", LuetCLIVersion, BuildCommit, BuildTime) + return fmt.Sprintf("%s (Build time: %s)", Version, BuildTime) } // RootCmd represents the base command when called without any subcommands diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000..60817ed0 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func init() { + RootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version of luet", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(Version) + }, +} diff --git a/go.sum b/go.sum index f1c935d1..caef3ec2 100644 --- a/go.sum +++ b/go.sum @@ -37,6 +37,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -439,8 +441,6 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/moby/buildkit v0.13.0 h1:reVR1Y+rbNIUQ9jf0Q1YZVH5a/nhOixZsl+HJ9qQEGI= github.com/moby/buildkit v0.13.0/go.mod h1:aNmNQKLBFYAOFuzQjR3VA27/FijlvtBD1pjNwTSN37k= -github.com/moby/moby v20.10.9+incompatible h1:fzBLeWWduR/GQCWZe6MDL8t8qKr6Nhpgd2sLzhdVBuk= -github.com/moby/moby v20.10.9+incompatible/go.mod h1:fDXVQ6+S340veQPv35CzDahGBmHsiclFwfEygB/TWMc= github.com/moby/moby v25.0.4+incompatible h1:vea1J80wDM5x5geaZSaywFkfFxLABJIQ3mmR4ewZGbU= github.com/moby/moby v25.0.4+incompatible/go.mod h1:fDXVQ6+S340veQPv35CzDahGBmHsiclFwfEygB/TWMc= github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk= diff --git a/main.go b/main.go index 0e74155b..351b277a 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,6 @@ package main import "github.com/mudler/luet/cmd" -var ( - version string -) - func main() { cmd.Execute() }