-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update version command to work with ldflags
Showing
6 changed files
with
145 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters