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

feat: add version cmd #280

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@ Temporary Items
# local build folders
istio-1.22.0/
kontrol-service/Run_kontrol_service

# Ignoring the file that gets generated with the repo's version
/kardinal_version/kardinal_version.go
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
packages.go-tidy-all = pkgs.callPackage ./scripts/go-tidy-all.nix {
inherit pkgs;
};
packages.get-docker-tag = pkgs.callPackage ./scripts/get-docker-tag.nix {
inherit pkgs;
};
packages.generate-kardinal-version = pkgs.callPackage ./scripts/generate_kardinal_version.nix {
inherit pkgs;
};

containers = let
os = "linux";
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ use (
./libs/cli-kontrol-api
./kardinal-cli
./sidecars/redis-overlay-service
./kardinal_version
)
11 changes: 11 additions & 0 deletions kardinal-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"kardinal.cli/kontrol"
"kardinal.cli/tenant"

"github.com/kurtosis-tech/kardinal/kardinal_version"
api "github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api/api/golang/client"
api_types "github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api/api/golang/types"
appv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -622,6 +623,15 @@ var tenantShowCmd = &cobra.Command{
},
}

var versionCmd = &cobra.Command{
tedim52 marked this conversation as resolved.
Show resolved Hide resolved
Use: "version",
Short: "Show version of Kardinal CLI running",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("%s\n", kardinal_version.KardinalVersion)
},
}

func init() {
devMode = false
if os.Getenv("KARDINAL_CLI_DEV_MODE") == "TRUE" {
Expand All @@ -637,6 +647,7 @@ func init() {
rootCmd.AddCommand(reportInstall)
rootCmd.AddCommand(topologyCmd)
rootCmd.AddCommand(tenantCmd)
rootCmd.AddCommand(versionCmd)

flowCmd.AddCommand(listCmd, createCmd, deleteCmd, telepresenceInterceptCmd)
managerCmd.AddCommand(deployManagerCmd, removeManagerCmd)
Expand Down
10 changes: 6 additions & 4 deletions kardinal-cli/go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module kardinal.cli

go 1.22.0
go 1.22.3

toolchain go1.22.3

replace github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api => ../libs/cli-kontrol-api
replace (
github.com/kurtosis-tech/kardinal/kardinal_version => ../kardinal_version
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api => ../libs/cli-kontrol-api
)

require (
github.com/adrg/xdg v0.4.0
github.com/google/uuid v1.6.0
github.com/kurtosis-tech/kardinal/kardinal_version v0.0.0-00010101000000-000000000000
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409
github.com/samber/lo v1.46.0
github.com/segmentio/analytics-go/v3 v3.3.0
Expand Down
3 changes: 3 additions & 0 deletions kardinal_version/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/kurtosis-tech/kardinal/kardinal_version

go 1.22.3
49 changes: 49 additions & 0 deletions scripts/generate_kardinal_version.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{ pkgs, ... }:
pkgs.writeShellApplication {
name = "generate-kardinal-version";
runtimeInputs = with pkgs; [ git ];

text = ''
set -euo pipefail
script_dirpath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root_dirpath="$(dirname "$script_dirpath")"

KARDINAL_VERSION_PACKAGE_DIR="kardinal_version"
KARDINAL_VERSION_GO_FILE="kardinal_version.go"
KARDINAL_VERSION_PACKAGE_NAME="github.com/kurtosis-tech/kardinal/kardinal_version"
KARDINAL_VERSION_PACKAGE_GOSUM_PATH="go.sum"

show_helptext_and_exit() {
echo "Usage: $(basename "$0") new_version"
echo ""
echo " new_version The version to generate the version constants with, otherwise uses 'get-docker-tag.sh'"
echo ""
exit 1
}

new_version=""

if [ -z "$new_version" ]; then
if ! cd "$root_dirpath"; then
echo "Error: Couldn't cd to the root of this repo, '$root_dirpath', which is required to get the Git tag" >&2
show_helptext_and_exit
fi
if ! new_version="$(./scripts/get-docker-tag.sh)"; then
echo "Error: No new version provided and couldn't generate one" >&2
show_helptext_and_exit
fi
fi

kardinal_version_go_file_abs_path="$root_dirpath/$KARDINAL_VERSION_PACKAGE_DIR/$KARDINAL_VERSION_GO_FILE"

cat << EOF > "$kardinal_version_go_file_abs_path"
package $KARDINAL_VERSION_PACKAGE_DIR

const (
// !!!!!!!!!!!!!!!!!! DO NOT MODIFY THIS! IT WILL BE UPDATED AUTOMATICALLY DURING THE BUILD PROCESS !!!!!!!!!!!!!!!
KardinalVersion = "$new_version"
// !!!!!!!!!!!!!!!!!! DO NOT MODIFY THIS! IT WILL BE UPDATED AUTOMATICALLY DURING THE BUILD PROCESS !!!!!!!!!!!!!!!
)
EOF
'';
}
54 changes: 54 additions & 0 deletions scripts/generate_kardinal_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this file? because I see there is a nix version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we actually don't im gonna remove those

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get-docker-tag.sh is called by generate_kardinal_version.nix I wonder is there is a way to replace it with the nix version of this script?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ye i'm actually gonna embed the code for that directly in the generate nix


set -euo pipefail # Bash "strict mode"
script_dirpath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root_dirpath="$(dirname "${script_dirpath}")"

# ==================================================================================================
# Constants
# ==================================================================================================

KARDINAL_VERSION_PACKAGE_DIR="kardinal_version"
KARDINAL_VERSION_GO_FILE="kardinal_version.go"
KARDINAL_VERSION_PACKAGE_NAME="github.com/kurtosis-tech/kardinal/kardinal_version"
KARDINAL_VERSION_PACKAGE_GOSUM_PATH="go.sum"


# ==================================================================================================
# Arg Parsing & Validation
# ==================================================================================================
show_helptext_and_exit() {
echo "Usage: $(basename "${0}") new_version"
echo ""
echo " new_version The version to be generate the version constants with, otherwise uses 'get-docker-tag.sh'"
echo ""
exit 1
}


# ==================================================================================================
# Main Logic
# ==================================================================================================
new_version="${1:-}"

if [ -z "${new_version}" ]; then
if ! cd "${root_dirpath}"; then
echo "Error: Couldn't cd to the root of this repo, '${root_dirpath}', which is required to get the Git tag" >&2
show_helptext_and_exit
fi
if ! new_version="$(./scripts/get-docker-tag.sh)"; then
echo "Error: No new version provided and couldn't generate one" >&2
show_helptext_and_exit
fi
fi

kardinal_version_go_file_abs_path="${root_dirpath}/${KARDINAL_VERSION_PACKAGE_DIR}/${KARDINAL_VERSION_GO_FILE}"
cat << EOF > "${kardinal_version_go_file_abs_path}"
package ${KARDINAL_VERSION_PACKAGE_DIR}

const (
// !!!!!!!!!!!!!!!!!! DO NOT MODIFY THIS! IT WILL BE UPDATED AUTOMATICALLY DURING THE BUILD PROCESS !!!!!!!!!!!!!!!
KardinalVersion = "${new_version}"
// !!!!!!!!!!!!!!!!!! DO NOT MODIFY THIS! IT WILL BE UPDATED AUTOMATICALLY DURING THE BUILD PROCESS !!!!!!!!!!!!!!!
)
EOF
18 changes: 18 additions & 0 deletions scripts/get-docker-tag.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{ pkgs, ... }:
pkgs.writeShellApplication {
name = "get-docker-tag";
runtimeInputs = with pkgs; [ git ];

text = ''
set -euo pipefail

if ! git status > /dev/null; then
echo "Error: This command was run from outside a git repo" >&2
exit 1
fi

commit_sha="$(git rev-parse --short=6 HEAD)"
suffix="$(git diff --quiet || echo '-dirty')"
echo "$commit_sha$suffix"
'';
}
14 changes: 14 additions & 0 deletions scripts/get-docker-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set -euo pipefail # Bash "strict mode"

# ==================================================================================================
# Main Logic
# ==================================================================================================

if ! git status > /dev/null; then
echo "Error: This command was run from outside a git repo" >&2
exit 1
fi

commit_sha="$(git rev-parse --short=6 HEAD)"
suffix="$(git diff --quiet || echo '-dirty')"
echo "${commit_sha}${suffix}"
4 changes: 4 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
'';

go-tidy-all = import ./scripts/go-tidy-all.nix {inherit pkgs;};
get-docker-tag = import ./scripts/get-docker-tag.nix {inherit pkgs;};
generate-kardinal-version = import ./scripts/generate_kardinal_version.nix {inherit pkgs;};

manager_shell = pkgs.callPackage ./kardinal-manager/shell.nix {inherit pkgs;};
cli_shell = pkgs.callPackage ./kardinal-cli/shell.nix {inherit pkgs;};
Expand All @@ -25,6 +27,8 @@
buildInputs = [
kardinal
go-tidy-all
get-docker-tag
generate-kardinal-version
kubectl
kustomize
kubernetes-helm
Expand Down
Loading