From c1e8c1e39166dcfeb8fe996f5f44299fbf9701fa Mon Sep 17 00:00:00 2001 From: Jon Perry Date: Tue, 19 Oct 2021 16:33:08 -0400 Subject: [PATCH 1/7] wip on getting a version flag --- cli/cmd/root.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 55ea5ee565..e50b51a079 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -23,6 +23,10 @@ var rootCmd = &cobra.Command{ packager.Create(confirmCreate) return } + if args[0] == "testing" { + fmt.Println("Hello jonathan!!") + return + } } cmd.Help() }, From bf10fb89b78c405b986d81aac8a0877022d898b2 Mon Sep 17 00:00:00 2001 From: Jon Perry Date: Wed, 20 Oct 2021 13:19:35 -0400 Subject: [PATCH 2/7] Add option to print version of CLI --- cli/Makefile | 4 +++- cli/cmd/root.go | 6 ++---- cli/cmd/version.go | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 cli/cmd/version.go diff --git a/cli/Makefile b/cli/Makefile index eee4b15ed6..4b959d5608 100644 --- a/cli/Makefile +++ b/cli/Makefile @@ -6,9 +6,11 @@ # test: deps # gotestsum +CLI_VERSION := $(shell git describe) + build: mkdir -p ../build - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ../build/zarf main.go + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X 'github.com/defenseunicorns/zarf/cli/cmd.CLIVersion=$(CLI_VERSION)'" -o ../build/zarf main.go build-mac: mkdir -p ../build diff --git a/cli/cmd/root.go b/cli/cmd/root.go index e50b51a079..afb105a1f3 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -9,6 +9,8 @@ import ( "github.com/spf13/cobra" ) +var CLIVersion = "unset" + var rootCmd = &cobra.Command{ Use: "zarf COMMAND|ZARF-PACKAGE|ZARF-YAML", Short: "Small tool to bundle dependencies with K3s for airgapped deployments", @@ -23,10 +25,6 @@ var rootCmd = &cobra.Command{ packager.Create(confirmCreate) return } - if args[0] == "testing" { - fmt.Println("Hello jonathan!!") - return - } } cmd.Help() }, diff --git a/cli/cmd/version.go b/cli/cmd/version.go new file mode 100644 index 0000000000..e0c766ac78 --- /dev/null +++ b/cli/cmd/version.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Displays the version the zarf binary was built from", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(CLIVersion) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} From 96826cb078e7d97966a274b8659c94eaffa8590c Mon Sep 17 00:00:00 2001 From: Jon Perry Date: Wed, 20 Oct 2021 19:03:03 -0400 Subject: [PATCH 3/7] Add General CLI e2e test to verify version command --- test/e2e/e2e_general_cli_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/e2e/e2e_general_cli_test.go b/test/e2e/e2e_general_cli_test.go index 4628a19b7f..e67c1f1991 100644 --- a/test/e2e/e2e_general_cli_test.go +++ b/test/e2e/e2e_general_cli_test.go @@ -2,6 +2,7 @@ package test import ( "fmt" + "os/exec" "github.com/gruntwork-io/terratest/modules/aws" "github.com/gruntwork-io/terratest/modules/ssh" "github.com/gruntwork-io/terratest/modules/terraform" @@ -72,4 +73,13 @@ func testGeneralCliStuff(t *testing.T, terraformOptions *terraform.Options, keyP output,err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("cd /home/%s/build && ./zarf prepare sha256sum https://zarf-public.s3-us-gov-west-1.amazonaws.com/pipelines/zarf-prepare-shasum-remote-test-file.txt 2> /dev/null", username)) require.NoError(t, err, output) assert.Equal(t, expectedShasum, output, "The expected SHASUM should equal the actual SHASUM") + + // Test `zarf version` + //expectedVersion, err := exec.Command("git", "describe").Output() + //require.NoError(t, err, output) + output, err := ssh.CheckSshCommand(t, publicHost, fmt.Sprintf("cd /home/%s/build && ./zarf version", username)) + require.NoError(t, err, output) + assert.NotNil(t, output) + assert.NotEqual(t, len(output), 0, "Zarf version should not be an empty string") + //assert.Equal(t, expectedVersion, output, "The expected Zarf version should equal the actual Zarf version") } From c94d58a48b9757c1bfa10d2f9c85b141cec202ed Mon Sep 17 00:00:00 2001 From: Jon Perry Date: Mon, 25 Oct 2021 12:07:33 -0400 Subject: [PATCH 4/7] Clean up e2e test for CLI version --- cli/Makefile | 2 +- cli/cmd/root.go | 2 +- test/e2e/e2e_general_cli_test.go | 4 +--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/cli/Makefile b/cli/Makefile index 4b959d5608..36e7e3d241 100644 --- a/cli/Makefile +++ b/cli/Makefile @@ -6,7 +6,7 @@ # test: deps # gotestsum -CLI_VERSION := $(shell git describe) +CLI_VERSION := $(if $(shell git describe), $(shell git describe), "UnknownVersion") build: mkdir -p ../build diff --git a/cli/cmd/root.go b/cli/cmd/root.go index afb105a1f3..21f28b6a37 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -var CLIVersion = "unset" +var CLIVersion = "" var rootCmd = &cobra.Command{ Use: "zarf COMMAND|ZARF-PACKAGE|ZARF-YAML", diff --git a/test/e2e/e2e_general_cli_test.go b/test/e2e/e2e_general_cli_test.go index e67c1f1991..af97b3cb8c 100644 --- a/test/e2e/e2e_general_cli_test.go +++ b/test/e2e/e2e_general_cli_test.go @@ -75,11 +75,9 @@ func testGeneralCliStuff(t *testing.T, terraformOptions *terraform.Options, keyP assert.Equal(t, expectedShasum, output, "The expected SHASUM should equal the actual SHASUM") // Test `zarf version` - //expectedVersion, err := exec.Command("git", "describe").Output() - //require.NoError(t, err, output) output, err := ssh.CheckSshCommand(t, publicHost, fmt.Sprintf("cd /home/%s/build && ./zarf version", username)) require.NoError(t, err, output) assert.NotNil(t, output) assert.NotEqual(t, len(output), 0, "Zarf version should not be an empty string") - //assert.Equal(t, expectedVersion, output, "The expected Zarf version should equal the actual Zarf version") + assert.NotEqual(t, string(output), "Unknown Version", "Zarf version should not be the default value") } From a61c82dfaf0b253c6102824e167dfac95e036422 Mon Sep 17 00:00:00 2001 From: Jon Perry Date: Mon, 25 Oct 2021 13:22:55 -0400 Subject: [PATCH 5/7] Set Version in config package instead of cmd package --- cli/Makefile | 3 +-- cli/cmd/root.go | 2 -- cli/cmd/version.go | 3 ++- cli/config/config.go | 4 ++++ cli/config/types.go | 1 + 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cli/Makefile b/cli/Makefile index 36e7e3d241..5f51af5fc8 100644 --- a/cli/Makefile +++ b/cli/Makefile @@ -10,8 +10,7 @@ CLI_VERSION := $(if $(shell git describe), $(shell git describe), "UnknownVersio build: mkdir -p ../build - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X 'github.com/defenseunicorns/zarf/cli/cmd.CLIVersion=$(CLI_VERSION)'" -o ../build/zarf main.go - + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X 'github.com/defenseunicorns/zarf/cli/config.CLIVersion=$(CLI_VERSION)'" -o ../build/zarf main.go build-mac: mkdir -p ../build GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o ../build/zarf-mac-apple main.go diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 21f28b6a37..55ea5ee565 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -9,8 +9,6 @@ import ( "github.com/spf13/cobra" ) -var CLIVersion = "" - var rootCmd = &cobra.Command{ Use: "zarf COMMAND|ZARF-PACKAGE|ZARF-YAML", Short: "Small tool to bundle dependencies with K3s for airgapped deployments", diff --git a/cli/cmd/version.go b/cli/cmd/version.go index e0c766ac78..0bbf70fef0 100644 --- a/cli/cmd/version.go +++ b/cli/cmd/version.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" + "github.com/defenseunicorns/zarf/cli/config" "github.com/spf13/cobra" ) @@ -10,7 +11,7 @@ var versionCmd = &cobra.Command{ Use: "version", Short: "Displays the version the zarf binary was built from", Run: func(cmd *cobra.Command, args []string) { - fmt.Println(CLIVersion) + fmt.Println(config.CLIVersion) }, } diff --git a/cli/config/config.go b/cli/config/config.go index 8d0f30ac3b..fe2f78702e 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -21,6 +21,7 @@ const ZarfLocalIP = "127.0.0.1" const ZarfGitUser = "zarf-git-user" var config ZarfConfig +var CLIVersion = "unset" func IsZarfInitConfig() bool { return strings.ToLower(config.Kind) == "zarfinitconfig" @@ -71,6 +72,9 @@ func WriteConfig(path string) { // Record the time of package creation config.Package.Timestamp = now.Format(time.RFC1123Z) + // Record the Zarf Version the CLI was built with + config.Package.Version = CLIVersion + if hostErr == nil { // Record the hostname of the package creation terminal config.Package.Terminal = hostname diff --git a/cli/config/types.go b/cli/config/types.go index 0641056d2c..e73316f838 100644 --- a/cli/config/types.go +++ b/cli/config/types.go @@ -48,6 +48,7 @@ type ZarfBuildData struct { Terminal string `yaml:"terminal"` User string `yaml:"user"` Timestamp string `yaml:"timestamp"` + Version string `yaml:"string"` } type ZarfConfig struct { From 621834ec648f194a65e694f3f2d363b07f568645 Mon Sep 17 00:00:00 2001 From: Jon Perry Date: Mon, 25 Oct 2021 13:32:18 -0400 Subject: [PATCH 6/7] Remove unnecessary re-declaration of test variable --- test/e2e/e2e_general_cli_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/e2e/e2e_general_cli_test.go b/test/e2e/e2e_general_cli_test.go index af97b3cb8c..521aa2e00b 100644 --- a/test/e2e/e2e_general_cli_test.go +++ b/test/e2e/e2e_general_cli_test.go @@ -2,7 +2,6 @@ package test import ( "fmt" - "os/exec" "github.com/gruntwork-io/terratest/modules/aws" "github.com/gruntwork-io/terratest/modules/ssh" "github.com/gruntwork-io/terratest/modules/terraform" @@ -75,7 +74,7 @@ func testGeneralCliStuff(t *testing.T, terraformOptions *terraform.Options, keyP assert.Equal(t, expectedShasum, output, "The expected SHASUM should equal the actual SHASUM") // Test `zarf version` - output, err := ssh.CheckSshCommand(t, publicHost, fmt.Sprintf("cd /home/%s/build && ./zarf version", username)) + output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("cd /home/%s/build && ./zarf version", username)) require.NoError(t, err, output) assert.NotNil(t, output) assert.NotEqual(t, len(output), 0, "Zarf version should not be an empty string") From a3ff3a745365098af26a16f8ce62e1d8bc976a32 Mon Sep 17 00:00:00 2001 From: Jon Perry Date: Mon, 25 Oct 2021 23:39:41 -0400 Subject: [PATCH 7/7] Remove space in CLI version test assert --- test/e2e/e2e_general_cli_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/e2e_general_cli_test.go b/test/e2e/e2e_general_cli_test.go index 521aa2e00b..39c2e6dea6 100644 --- a/test/e2e/e2e_general_cli_test.go +++ b/test/e2e/e2e_general_cli_test.go @@ -78,5 +78,5 @@ func testGeneralCliStuff(t *testing.T, terraformOptions *terraform.Options, keyP require.NoError(t, err, output) assert.NotNil(t, output) assert.NotEqual(t, len(output), 0, "Zarf version should not be an empty string") - assert.NotEqual(t, string(output), "Unknown Version", "Zarf version should not be the default value") + assert.NotEqual(t, string(output), "UnknownVersion", "Zarf version should not be the default value") }