From c71ebea2d42de904061643bb74553c61919fa899 Mon Sep 17 00:00:00 2001 From: Grzegorz Dlugoszewski Date: Fri, 12 Jun 2020 20:56:34 +0200 Subject: [PATCH] Move the version metadata to cfg packageThis way it can be used by multiple binaries. --- .goreleaser.yml | 25 +++++++++++++++++-------- cmd/list/main.go | 8 +------- pkg/cfg/config.go | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index e1f9e41..e1bac2b 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -3,20 +3,29 @@ before: - go mod download builds: - - main: ./cmd/main.go - binary: "git-get" + - id: git-list + main: ./cmd/list/main.go + binary: git-list + ldflags: + - -s -w + - -X git-get/pkg/cfg.version={{.Version}} + - -X git-get/pkg/cfg.commit={{.Commit}} + - -X git-get/pkg/cfg.date={{.Date}} goos: - linux - darwin - windows archives: -- replacements: - darwin: macOS - linux: Linux - windows: Windows - 386: i386 - amd64: x86_64 + - id: archive + builds: + - git-list + replacements: + darwin: macOS + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 checksum: name_template: 'checksums.txt' diff --git a/cmd/list/main.go b/cmd/list/main.go index 8fd14f0..6842dc3 100644 --- a/cmd/list/main.go +++ b/cmd/list/main.go @@ -13,18 +13,12 @@ import ( "github.com/spf13/viper" ) -var ( - version = "dev" - commit = "unknown" - date = "unknown" -) - var cmd = &cobra.Command{ Use: "git-get ", Short: "git get", Run: Run, Args: cobra.MaximumNArgs(1), // TODO: add custom validator - Version: fmt.Sprintf("%s - %s, build at %s", version, commit, date), + Version: cfg.Version(), } func init() { diff --git a/pkg/cfg/config.go b/pkg/cfg/config.go index 774763b..0573062 100644 --- a/pkg/cfg/config.go +++ b/pkg/cfg/config.go @@ -1,6 +1,7 @@ package cfg import ( + "fmt" "path" "strings" @@ -37,6 +38,23 @@ const ( OutSimple = "simple" ) +// Version metadata set by ldflags during the build. +var ( + version string + commit string + date string +) + +// Version returns a string with version metadata: version number, git sha and build date. +// It returns "development" if version variables are not set during the build. +func Version() string { + if version == "" { + return "development" + } + + return fmt.Sprintf("%s - revision %s built at %s", version, commit[:6], date) +} + // gitconfig provides methods for looking up configiration values inside .gitconfig file type gitconfig struct { *config.Config