From 4aba1c43d284adbd13ca225b7e9a8809b1d41fce Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Sat, 19 Aug 2023 22:56:38 +0530 Subject: [PATCH] fix ldflags and makefile to properly pass versions --- Makefile | 7 ++--- buildscripts/gen-ldflags.go | 63 ++----------------------------------- cmd/version.go | 21 +++++++++++-- main.go | 8 ++--- 4 files changed, 28 insertions(+), 71 deletions(-) diff --git a/Makefile b/Makefile index 481b82f..2f6ca83 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,12 @@ PWD := $(shell pwd) GOPATH := $(shell go env GOPATH) -LDFLAGS := $(shell go run buildscripts/gen-ldflags.go) +VERSION ?= $(shell git describe --tags) +TAG ?= "parseablehq/pb:$(VERSION)" +LDFLAGS := $(shell go run buildscripts/gen-ldflags.go $(VERSION)) GOARCH := $(shell go env GOARCH) GOOS := $(shell go env GOOS) -VERSION ?= $(shell git describe --tags) -TAG ?= "parseablehq/pb:$(VERSION)" - all: build checks: diff --git a/buildscripts/gen-ldflags.go b/buildscripts/gen-ldflags.go index 9a412be..17cbdf5 100644 --- a/buildscripts/gen-ldflags.go +++ b/buildscripts/gen-ldflags.go @@ -23,50 +23,15 @@ import ( "os" "os/exec" "strings" - "time" ) func genLDFlags(version string) string { - releaseTag, date := releaseTag(version) - copyrightYear := fmt.Sprintf("%d", date.Year()) - var ldflagsStr string - ldflagsStr = "-s -w -X github.com/parseablehq/pb/cmd.Version=" + version + " " - ldflagsStr = ldflagsStr + "-X github.com/parseablehq/pb/cmd.CopyrightYear=" + copyrightYear + " " - ldflagsStr = ldflagsStr + "-X github.com/parseablehq/pb/cmd.ReleaseTag=" + releaseTag + " " - ldflagsStr = ldflagsStr + "-X github.com/parseablehq/pb/cmd.CommitID=" + commitID() + " " - ldflagsStr = ldflagsStr + "-X github.com/parseablehq/pb/cmd.ShortCommitID=" + commitID()[:12] + ldflagsStr = "-s -w -X main.Version=" + version + " " + ldflagsStr = ldflagsStr + "-X main.Commit=" + commitID()[:12] return ldflagsStr } -// releaseTag prints release tag to the console for easy git tagging. -func releaseTag(version string) (string, time.Time) { - relPrefix := "DEVELOPMENT" - if prefix := os.Getenv("PB_RELEASE"); prefix != "" { - relPrefix = prefix - } - - relSuffix := "" - if hotfix := os.Getenv("PB_HOTFIX"); hotfix != "" { - relSuffix = hotfix - } - - relTag := strings.ReplaceAll(version, " ", "-") - relTag = strings.ReplaceAll(relTag, ":", "-") - t, err := time.Parse("2006-01-02T15-04-05Z", relTag) - if err != nil { - panic(err) - } - - relTag = strings.ReplaceAll(relTag, ",", "") - relTag = relPrefix + "." + relTag - if relSuffix != "" { - relTag += "." + relSuffix - } - - return relTag, t -} - // commitID returns the abbreviated commit-id hash of the last commit. func commitID() string { // git log --format="%h" -n1 @@ -84,34 +49,12 @@ func commitID() string { return strings.TrimSpace(string(commit)) } -func commitTime() time.Time { - // git log --format=%cD -n1 - var ( - commitUnix []byte - err error - ) - cmdName := "git" - cmdArgs := []string{"log", "--format=%cI", "-n1"} - if commitUnix, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil { - fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err) - os.Exit(1) - } - - t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(commitUnix))) - if err != nil { - fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err) - os.Exit(1) - } - - return t.UTC() -} - func main() { var version string if len(os.Args) > 1 { version = os.Args[1] } else { - version = commitTime().Format(time.RFC3339) + version = "v0.0.0/DEVELOPMENT" } fmt.Println(genLDFlags(version)) diff --git a/cmd/version.go b/cmd/version.go index 8c866f1..550507e 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,3 +1,18 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package cmd import ( @@ -15,8 +30,8 @@ var VersionCmd = &cobra.Command{ } // PrintVersion prints version information -func PrintVersion(version string, commit string) { +func PrintVersion(version, commit string) { fmt.Printf("\n%s \n\n", standardStyleAlt.Render("pb version")) - fmt.Printf("%s %s\n", standardStyleBold.Render("version: "), version) - fmt.Printf("%s %s\n", standardStyleBold.Render("commit: "), commit) + fmt.Printf(" %s %s\n", standardStyleBold.Render("version: "), version) + fmt.Printf(" %s %s\n\n", standardStyleBold.Render("commit: "), commit) } diff --git a/main.go b/main.go index 913b41a..32953c3 100644 --- a/main.go +++ b/main.go @@ -30,8 +30,8 @@ import ( var ( // populated at build time - version string - commit string + Version string + Commit string ) var ( @@ -57,7 +57,7 @@ var cli = &cobra.Command{ Long: "\npb is a command line tool for Parseable", Run: func(command *cobra.Command, args []string) { if p, _ := command.Flags().GetBool(versionFlag); p { - cmd.PrintVersion(version, commit) + cmd.PrintVersion(Version, Commit) } }, } @@ -132,7 +132,7 @@ func main() { // Set as command cmd.VersionCmd.Run = func(_ *cobra.Command, args []string) { - cmd.PrintVersion(version, commit) + cmd.PrintVersion(Version, Commit) } cli.AddCommand(cmd.VersionCmd) // set as flag