Skip to content

Commit

Permalink
feat: update version command to work with ldflags
Browse files Browse the repository at this point in the history
micovery committed Apr 17, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d4e3f39 commit 8c8c93f
Showing 6 changed files with 145 additions and 46 deletions.
8 changes: 7 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/bin/bash

BUILD_TIMESTAMP=$(date "+%s")
GIT_COMMIT=$(git rev-parse --short HEAD)
GIT_TAG=$(git describe --tags --abbrev=0)
LD_FLAGS="-X main.GitTag=${GIT_TAG} -X main.BuildTimestamp=${BUILD_TIMESTAMP} -X main.GitCommit=${GIT_COMMIT}"


mkdir -p bin
echo "Building tool ..."
go build -o bin/proxy-tool ./cmd/proxy-tool
go build -buildvcs=true -ldflags "${LD_FLAGS}" -o bin/proxy-tool ./cmd/proxy-tool

3 changes: 1 addition & 2 deletions cmd/proxy-tool/cmd.go
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@ package main
import (
"github.com/micovery/apigee-yaml-toolkit/cmd/proxy-tool/render"
"github.com/micovery/apigee-yaml-toolkit/cmd/proxy-tool/transform"
"github.com/micovery/apigee-yaml-toolkit/cmd/proxy-tool/version"
"github.com/micovery/apigee-yaml-toolkit/pkg/flags"
"github.com/spf13/cobra"
"os"
@@ -33,7 +32,7 @@ var RootCmd = &cobra.Command{
func init() {
RootCmd.AddCommand(render.Cmd)
RootCmd.AddCommand(transform.Cmd)
RootCmd.AddCommand(version.Cmd)
RootCmd.AddCommand(VersionCmd)

RootCmd.PersistentFlags().Var(&showStack, "show-stack", "show stack trace for errors")
}
136 changes: 136 additions & 0 deletions cmd/proxy-tool/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"encoding/json"
"fmt"
"github.com/go-errors/errors"
"github.com/spf13/cobra"
"runtime/debug"
"slices"
)

var VersionCmd = &cobra.Command{
Use: "version",
Short: "Print tool version",
RunE: func(cmd *cobra.Command, args []string) error {

var versionInfo any
goReleaserInfo := GetGoReleaserInfo()
goInstallInfo := GetGoInstallInfo()
goBuildVCSInfo := GetGoBuildVCSInfo()

if goReleaserInfo != nil {
//package built with go releaser
versionInfo = goReleaserInfo
} else if goInstallInfo != nil {
//package built with go install
versionInfo = goInstallInfo
} else if goBuildVCSInfo != nil {
//other builds
versionInfo = goBuildVCSInfo
}

versionText, err := json.Marshal(versionInfo)
if err != nil {
return errors.New(err)
}
fmt.Printf("%s\n", string(versionText))
return nil
},
}

func GetGoBuildVCSInfo() any {
info, _ := debug.ReadBuildInfo()
getBuildSettingValue := func(key string, settings []debug.BuildSetting) string {
index := slices.IndexFunc(settings, func(setting debug.BuildSetting) bool {
return setting.Key == key
})

if index < 0 {
return ""
}

return settings[index].Value
}

type GoBuildVCSInfo struct {
VCSInfo struct {
Revision string `json:"vcs.revision"`
Time string `json:"vcs.time"`
Modified string `json:"modified"`
} `json:"VCSInfo"`
}

revision := getBuildSettingValue("vcs.revision", info.Settings)
if revision == "" {
return nil
}

result := &GoBuildVCSInfo{}
result.VCSInfo.Revision = revision
result.VCSInfo.Time = getBuildSettingValue("vcs.time", info.Settings)
result.VCSInfo.Modified = getBuildSettingValue("vcs.modified", info.Settings)

return result
}

func GetGoInstallInfo() any {
info, _ := debug.ReadBuildInfo()
if info.Main.Version == "(devel)" {
return nil
}

type GoInstallInfo struct {
InstallInfo struct {
Version string `json:"Version"`
Sum string `json:"Sum"`
} `json:"InstallInfo"`
}

result := &GoInstallInfo{}
result.InstallInfo.Version = info.Main.Version
result.InstallInfo.Sum = info.Main.Sum

return result

}

// GitTag this is populated through -ldflags during build
var GitTag = ""
var GitCommit = ""
var BuildTimestamp = ""

func GetGoReleaserInfo() any {
if GitTag == "" {
return nil
}

type GoReleaserInfo struct {
ReleaseInfo struct {
GitCommit string `json:"GitCommit"`
GitTag string `json:"GitTag"`
Timestamp string `json:"Timestamp"`
} `json:"ReleaseInfo"`
}

result := &GoReleaserInfo{}
result.ReleaseInfo.GitTag = GitTag
result.ReleaseInfo.GitCommit = GitCommit
result.ReleaseInfo.Timestamp = BuildTimestamp

return result
}
32 changes: 0 additions & 32 deletions cmd/proxy-tool/version/cmd.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ require (
github.com/go-errors/errors v1.5.1
github.com/gosimple/slug v1.14.0
github.com/pb33f/libopenapi v0.15.14
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
github.com/vektah/gqlparser/v2 v2.5.11
github.com/vmware-labs/yaml-jsonpath v0.3.2
@@ -52,7 +53,6 @@ require (
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/crypto v0.3.0 // indirect
10 changes: 0 additions & 10 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -15,19 +15,9 @@
package utils

import (
"fmt"
"os"
"path/filepath"
"runtime/debug"
)

func PrintVersion() {
program := filepath.Base(os.Args[0])
info, _ := debug.ReadBuildInfo()

fmt.Printf("%s %s\n", program, info.Main.Version)
}

func MustReadFileBytes(path string) []byte {
data, err := os.ReadFile(path)
if err != nil {

0 comments on commit 8c8c93f

Please sign in to comment.