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/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 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/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 40745689..e54485a3 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,33 @@ package main import ( + "runtime/debug" + "github.com/pingidentity/pingcli/cmd" "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() + // 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() if err != nil {