Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
receive version string from ldflags during build
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAShelf committed Aug 28, 2023
1 parent 0dac4d6 commit 81bf1eb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
9 changes: 5 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@

package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var Version = "v1.1.0"
var Version string

func init() {
rootCmd.AddCommand(versionCmd)
}

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

2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 81bf1eb

Please sign in to comment.