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 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
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.

14 changes: 14 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@
};
};

generateKardinalVersion = pkgs.callPackage ./kardinal-cli/scripts/generate_kardinal_version.nix {
inherit pkgs;
};

runGenerateVersion = pkgs.stdenv.mkDerivation {
name = "run-generate-kardinal-version";
buildInputs = [ generateKardinalVersion ];
installPhase = ''
echo "Generating kardinal_version.go..."
${generateKardinalVersion}/bin/generate-kardinal-version
'';
};

systemOutput = rec {
devShells.default = pkgs.callPackage ./shell.nix {
inherit pkgs;
Expand All @@ -110,6 +123,7 @@
packages.go-tidy-all = pkgs.callPackage ./scripts/go-tidy-all.nix {
inherit pkgs;
};
packages.generate-kardinal-version = runGenerateVersion;

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
16 changes: 12 additions & 4 deletions kardinal-cli/default.nix
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
{
pkgs,
commit_hash ? "dirty",
pkgs,
commit_hash ? "dirty"
}: let
pname = "kardinal.cli";

generateKardinalVersion = pkgs.callPackage ./scripts/generate_kardinal_version.nix {
inherit pkgs;
};
_ = pkgs.runCommand "generate-version" {
buildInputs = [ generateKardinalVersion ];
};

ldflags = pkgs.lib.concatStringsSep "\n" [
"-X github.com/kurtosis-tech/kurtosis/kardinal.AppName=${pname}"
"-X github.com/kurtosis-tech/kurtosis/kardinal.Commit=${commit_hash}"
];
in
pkgs.buildGoApplication {
# pname has to match the location (folder) where the main function is or use
# subPackges to specify the file (e.g. subPackages = ["some/folder/main.go"];)
# subPackages to specify the file (e.g. subPackages = ["some/folder/main.go"];)
inherit pname ldflags;
name = "${pname}";
pwd = ./.;
src = ./.;
modules = ./gomod2nix.toml;
CGO_ENABLED = 0;
}
}
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
37 changes: 37 additions & 0 deletions kardinal-cli/scripts/generate_kardinal_version.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ pkgs, ... }:
pkgs.writeShellApplication {
name = "generate-kardinal-version";
runtimeInputs = with pkgs; [ git ];

text = ''
set -euo pipefail
root_dirpath=$(git rev-parse --show-toplevel)

KARDINAL_VERSION_PACKAGE_DIR="kardinal_version"
KARDINAL_VERSION_GO_FILE="kardinal_version.go"

# Check if the current commit has a tag
if git describe --tags --exact-match > /dev/null 2>&1; then
# If there's an exact match to a tag, use the tag as the version
new_version="$(git describe --tags --exact-match)"
else
# Otherwise, use the short commit SHA
commit_sha="$(git rev-parse --short=6 HEAD)"
# Check if the working directory is dirty
suffix="$(git diff --quiet || echo '-dirty')"
new_version="$commit_sha$suffix"
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
'';
}
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
3 changes: 2 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'';

go-tidy-all = import ./scripts/go-tidy-all.nix {inherit pkgs;};

generate-kardinal-version = import ./kardinal-cli/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;};
cli_kontrol_api_shell = pkgs.callPackage ./libs/cli-kontrol-api/shell.nix {inherit pkgs;};
Expand All @@ -25,6 +25,7 @@
buildInputs = [
kardinal
go-tidy-all
generate-kardinal-version
kubectl
kustomize
kubernetes-helm
Expand Down
Loading