Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Zarf Version to CLI options #116

Merged
merged 7 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
# test: deps
# gotestsum

CLI_VERSION := $(if $(shell git describe), $(shell git describe), "UnknownVersion")

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/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
Expand Down
20 changes: 20 additions & 0 deletions cli/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"

"github.com/defenseunicorns/zarf/cli/config"
"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(config.CLIVersion)
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}
4 changes: 4 additions & 0 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cli/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/e2e_general_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ 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`
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")
assert.NotEqual(t, string(output), "UnknownVersion", "Zarf version should not be the default value")
}