From 0071f074b6b454a05326ff5cbf12179757482214 Mon Sep 17 00:00:00 2001 From: Erik Ostien Date: Fri, 18 Oct 2024 10:24:52 +0200 Subject: [PATCH 1/3] PDI-2056: Use GoReleaser to Apply PingCLI Version and Commit to Builds --- .github/workflows/goreleaser.yaml | 2 +- .goreleaser.yaml | 7 +++++++ cmd/root.go | 4 ++-- main.go | 9 ++++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.github/workflows/goreleaser.yaml b/.github/workflows/goreleaser.yaml index 1b878974..9fa7ed0d 100644 --- a/.github/workflows/goreleaser.yaml +++ b/.github/workflows/goreleaser.yaml @@ -29,6 +29,6 @@ jobs: distribution: goreleaser # 'latest', 'nightly', or a semver version: latest - args: release --clean + args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 8415e639..16c66d66 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -12,6 +12,13 @@ builds: # Default: Project directory name binary: pingcli + # Custom ldflags. + # + # Default: '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser'. + # Templates: allowed. + ldflags: + - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' + # GOOS list to build for. # For more info refer to: https://golang.org/doc/install/source#environment # Choices for $GOOS are android, darwin, dragonfly, freebsd, illumos, ios, js, linux, netbsd, openbsd, plan9, solaris, wasip1, and windows. diff --git a/cmd/root.go b/cmd/root.go index 1806532b..c1fa9ba5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,13 +29,13 @@ func init() { } // rootCmd represents the base command when called without any subcommands -func NewRootCommand() *cobra.Command { +func NewRootCommand(version string, commit string) *cobra.Command { cmd := &cobra.Command{ Long: "A CLI tool for managing the configuration of Ping Identity products.", Short: "A CLI tool for managing the configuration of Ping Identity products.", SilenceErrors: true, // Upon error in RunE method, let output package in main.go handle error output Use: "pingcli", - Version: "v2.0.0-alpha.4", + Version: fmt.Sprintf("%s (commit: %s)", version, commit), } cmd.AddCommand( diff --git a/main.go b/main.go index 40745689..df1b31f2 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,15 @@ import ( "github.com/pingidentity/pingcli/internal/output" ) +var ( + // these will be set by the goreleaser configuration + // to appropriate values for the compiled binary + version string = "dev" + commit string = "dev" +) + func main() { - rootCmd := cmd.NewRootCommand() + rootCmd := cmd.NewRootCommand(version, commit) err := rootCmd.Execute() if err != nil { From 9e6d6a65d4ea2cf63af279e79e90d94de87c56bc Mon Sep 17 00:00:00 2001 From: Erik Ostien Date: Fri, 18 Oct 2024 10:37:40 +0200 Subject: [PATCH 2/3] Add commit on local build from debug settings info --- internal/testing/testutils_cobra/cobra_utils.go | 4 ++-- main.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/testing/testutils_cobra/cobra_utils.go b/internal/testing/testutils_cobra/cobra_utils.go index 1fada3cb..14fd42e8 100644 --- a/internal/testing/testutils_cobra/cobra_utils.go +++ b/internal/testing/testutils_cobra/cobra_utils.go @@ -17,7 +17,7 @@ func ExecutePingcli(t *testing.T, args ...string) (err error) { // Reset options for testing individual executions of pingcli configuration.InitAllOptions() - root := cmd.NewRootCommand() + root := cmd.NewRootCommand("test-version", "test-commit") // Add config location to the root command configFilepath := testutils_viper.CreateConfigFile(t) @@ -36,7 +36,7 @@ func ExecutePingcli(t *testing.T, args ...string) (err error) { func ExecutePingcliCaptureCobraOutput(t *testing.T, args ...string) (output string, err error) { t.Helper() - root := cmd.NewRootCommand() + root := cmd.NewRootCommand("test-version", "test-commit") // Add config location to the root command configFilepath := testutils_viper.CreateConfigFile(t) diff --git a/main.go b/main.go index df1b31f2..e54485a3 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "runtime/debug" + "github.com/pingidentity/pingcli/cmd" "github.com/pingidentity/pingcli/internal/output" ) @@ -13,6 +15,18 @@ var ( ) func main() { + // Try to get the commit hash from the build info if it wasn't set by goreleaser + if commit == "dev" { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + commit = setting.Value + break + } + } + } + } + rootCmd := cmd.NewRootCommand(version, commit) err := rootCmd.Execute() From e0d05ed0f50b9e413851e9aefe6456b6b2b2c590 Mon Sep 17 00:00:00 2001 From: Erik Ostien Date: Fri, 18 Oct 2024 10:43:18 +0200 Subject: [PATCH 3/3] Make testing avoid panic crash --- cmd/request/request_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/request/request_test.go b/cmd/request/request_test.go index c4449c2b..6da7efd9 100644 --- a/cmd/request/request_test.go +++ b/cmd/request/request_test.go @@ -45,6 +45,9 @@ func TestRequestCmd_Execute(t *testing.T) { for index, name := range re.SubexpNames() { if name == captureGroupName { + if len(matchData) <= index { + t.Fatalf("Failed to capture JSON body: %v", matchData) + } bodyJSON := matchData[index] // Check for valid JSON