From 81bf1eb760fde88c6b6546c5e1d507c189acd753 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 28 Aug 2023 14:20:21 +0300 Subject: [PATCH] receive version string from ldflags during build --- .goreleaser.yaml | 9 +++++---- Makefile | 4 +++- cmd/version.go | 7 ++++--- default.nix | 2 +- main.go | 7 ++++++- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 76157b2..8267bbb 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -10,7 +10,8 @@ builds: - windows - darwin ldflags: - - -s -w -X notashelf.dev/hyprkeys/cmd.Version={{.Version}} + - -s -w + - -X main.version={{.Version}} archives: - format: tar.gz @@ -22,8 +23,8 @@ archives: {{- else }}{{ .Arch }}{{ end }} {{- if .Arm }}v{{ .Arm }}{{ end }} format_overrides: - - goos: windows - format: zip + - goos: windows + format: zip rlcp: true checksum: - name_template: 'checksums.txt' + name_template: "checksums.txt" diff --git a/Makefile b/Makefile index 4e83f83..4027d6b 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,11 @@ pkgname := hyprkeys +version := v1.0.2 + build: ${pkgname} ${pkgname}: $(shell find . -name '*.go') mkdir -p bin - go build -o bin/${pkgname} . + go build -o bin/${pkgname} -ldflags "-X 'main.version=${version}'" . completions: mkdir -p completions diff --git a/cmd/version.go b/cmd/version.go index bb1affc..bd57ec8 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,12 +1,12 @@ + package cmd import ( "fmt" - "github.com/spf13/cobra" ) -var Version = "v1.1.0" +var Version string func init() { rootCmd.AddCommand(versionCmd) @@ -14,10 +14,11 @@ func init() { var versionCmd = &cobra.Command{ Use: "version", - Short: "Prints current verison", + Short: "Prints current version", Run: version, } func version(cmd *cobra.Command, args []string) { fmt.Printf("Hyprkeys: %s\n", Version) } + diff --git a/default.nix b/default.nix index f5be915..3a1a982 100644 --- a/default.nix +++ b/default.nix @@ -20,7 +20,7 @@ buildGoModule rec { ++ lib.optional (!doCheck) [./test]; }; - ldflags = ["-s" "-w"]; + ldflags = ["-s" "-w" "-X main.version=v${version}"]; vendorHash = "sha256-JFvC9V0xS8SZSdLsOtpyTrFzXjYAOaPQaJHdcnJzK3s="; doCheck = true; diff --git a/main.go b/main.go index e78fcfd..3dc5188 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,12 @@ package main -import "notashelf.dev/hyprkeys/cmd" +import ( + "notashelf.dev/hyprkeys/cmd" +) + +var version string func main() { + cmd.Version = version cmd.Execute() }