Skip to content

Commit

Permalink
fix ldflags and makefile to properly pass versions
Browse files Browse the repository at this point in the history
  • Loading branch information
nitisht committed Aug 19, 2023
1 parent e05fea9 commit 4aba1c4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 71 deletions.
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
63 changes: 3 additions & 60 deletions buildscripts/gen-ldflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
21 changes: 18 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

package cmd

import (
Expand All @@ -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)
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (

var (
// populated at build time
version string
commit string
Version string
Commit string
)

var (
Expand All @@ -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)
}
},
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4aba1c4

Please sign in to comment.