Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
fix: Improve CLI (#82)
Browse files Browse the repository at this point in the history
Signed-off-by: Edgar Gomes <talktoedgar@gmail.com>
Co-authored-by: Skylar Brown <skylar.brown@kurtosistech.com>
  • Loading branch information
lostbean and skylarmb authored Aug 7, 2024
1 parent 08c62be commit 42c1b69
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 49 deletions.
67 changes: 64 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ on:
tags:
- "v*.*.*"
paths-ignore:
- 'website/**'
- "website/**"
pull_request:
branches:
- main
paths-ignore:
- 'website/**'

- "website/**"

env:
MAIN_BRANCH: ${{ 'refs/heads/main' }}
Expand Down Expand Up @@ -135,3 +134,65 @@ jobs:
ln -s $path $binout
ls -lah $binout
[ -s $binout ]
test_cli_install:
strategy:
matrix:
shell: [bash, fish, zsh]
runs-on: ubuntu-latest
steps:
- name: git checkout
uses: actions/checkout@v3

- name: Install ShellCheck and shells
run: |
sudo apt-get update
sudo apt-get install -y shellcheck fish zsh
- name: Run ShellCheck
run: shellcheck ./scripts/install_cli.sh

- name: Run install script with ${{ matrix.shell }}
run: |
if [ "${{ matrix.shell }}" = "fish" ]; then
fish -c 'set -gx SHELL (which fish); cat ./scripts/install_cli.sh | bash'
elif [ "${{ matrix.shell }}" = "zsh" ]; then
zsh -c 'export SHELL=$(which zsh); cat ./scripts/install_cli.sh | bash'
elif [ "${{ matrix.shell }}" = "bash" ]; then
bash -c 'export SHELL=$(which bash); cat ./scripts/install_cli.sh | bash'
else
echo "Unsupported shell: ${{ matrix.shell }}"
exit 1
fi
- name: Check if ~/.local/bin exists
run: |
if [ ! -d ~/.local/bin ]; then
echo "~/.local/bin directory was not created"
exit 1
fi
- name: Check if config was added
run: |
if [ "${{ matrix.shell }}" = "fish" ]; then
config_file="$HOME/.config/fish/config.fish"
elif [ "${{ matrix.shell }}" = "zsh" ]; then
config_file="$HOME/.zshrc"
else
config_file="$HOME/.bashrc"
fi
if ! grep -q "# Kardinal CLI config" "$config_file"; then
echo "CLI tool configuration was not added to $config_file"
cat "$config_file"
exit 1
fi
- name: Verify kardinal command
run: |
if [ "${{ matrix.shell }}" = "fish" ]; then
fish -c 'source ~/.config/fish/config.fish; if kardinal | grep -q "Kardinal CLI"; exit 0; else; exit 1; end'
elif [ "${{ matrix.shell }}" = "zsh" ]; then
zsh -c 'source ~/.zshrc; if kardinal | grep -q "Kardinal CLI"; then exit 0; else exit 1; fi'
else
bash -c 'source ~/.bashrc; if kardinal | grep -q "Kardinal CLI"; then exit 0; else exit 1; fi'
fi
117 changes: 71 additions & 46 deletions scripts/install_cli.sh
Original file line number Diff line number Diff line change
@@ -1,63 +1,88 @@
#!/bin/sh

set -e

# Install Kardinal CLI - supports bash, zsh, fish and assumes you have curl procps installed

# Function to handle errors
handle_error() {
echo "Ops! Failed to setup integration with your shell. Please add the following lines to
your shell configuration manually (changes may not be persistent)
export PATH=\$PATH:$BIN_FOLDER
source <($BIN_FOLDER/$BINARY_NAME completion $PARENT_SHELL)"
exit 1
}

# Rest of your script goes here
REPO="kurtosis-tech/kardinal"
BINARY_NAME="kardinal"

OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)

if [[ "$ARCH" == "x86_64" ]]; then
ARCH="amd64"
elif [[ "$ARCH" == "arm64" ]]; then
ARCH="arm64"
elif [[ "$ARCH" == "aarch64" ]]; then
ARCH="arm64"
fi
case "$ARCH" in
x86_64) ARCH="amd64" ;;
arm64 | aarch64) ARCH="arm64" ;;
esac

BIN_FOLDER="$HOME/.local/bin"
mkdir -p $BIN_FOLDER
mkdir -p "$BIN_FOLDER"

LATEST_RELEASE=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
WAS_INTALLED_BEFORE=0
if [ -f "$BIN_FOLDER/$BINARY_NAME" ]; then
WAS_INTALLED_BEFORE=1
fi

LATEST_RELEASE=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
echo "Downloading $BINARY_NAME $LATEST_RELEASE for $OS $ARCH..."
DOWNLOAD_URL="https://github.com/$REPO/releases/download/$LATEST_RELEASE/${BINARY_NAME}-${OS}-${ARCH}"
curl -L $DOWNLOAD_URL -o /$BIN_FOLDER/$BINARY_NAME
chmod +x $BIN_FOLDER/$BINARY_NAME

USER_SHELL=$(basename $SHELL)
echo "Detected shell: $USER_SHELL"

handle_error() {
local exit_code=$?
echo "Ops! Failed to setup integration with your $USER_SHELL shell. Please add the following lines to
your shell configuration manually (changes may not be persistent)
export PATH=\$PATH:$BIN_FOLDER
source <($BIN_FOLDER/$BINARY_NAME completion $USER_SHELL)"
exit $exit_code
}
curl -L "$DOWNLOAD_URL" -o "$BIN_FOLDER/$BINARY_NAME"
chmod +x "$BIN_FOLDER/$BINARY_NAME"

trap 'handle_error' ERR
PARENT_SHELL=$(ps -o comm= -p $PPID)
echo "Detected parent shell: $PARENT_SHELL"

if [ -f $BIN_FOLDER/$BINARY_NAME ]; then
if [ $USER_SHELL == 'bash' ]; then
echo "export PATH=\$PATH:$BIN_FOLDER" >>~/.bashrc
echo "source <($BIN_FOLDER/$BINARY_NAME completion bash)" >>~/.bashrc
source ~/.bashrc
fi

if [ $USER_SHELL == 'zsh' ]; then
echo "export PATH=\$PATH:$BIN_FOLDER" >>~/.zshrc
echo "source <($BIN_FOLDER/$BINARY_NAME completion zsh)" >>~/.bashrc
source ~/.zshrc
fi

if [ $USER_SHELL == 'fish' ]; then
echo "set -gx PATH \$PATH $BIN_FOLDER" >>~/.config/fish/config.fish
echo "source <($BIN_FOLDER/$BINARY_NAME completion fish)" >>~/.bashrc
source ~/.config/fish/config.fish
if [ -f "$BIN_FOLDER/$BINARY_NAME" ]; then
if [ $WAS_INTALLED_BEFORE -eq 0 ]; then
case "$PARENT_SHELL" in
-bash | bash)
CONFIG_FILE="$HOME/.bashrc"
if ! echo "# Kardinal CLI config" >>"$CONFIG_FILE"; then
handle_error
fi
echo "export PATH=\$PATH:$BIN_FOLDER" >>"$CONFIG_FILE"
echo "source <($BIN_FOLDER/$BINARY_NAME completion bash)" >>"$CONFIG_FILE"
;;
-zsh | zsh)
CONFIG_FILE="$HOME/.zshrc"
if ! echo "# Kardinal CLI config" >>"$CONFIG_FILE"; then
handle_error
fi
echo "export PATH=\$PATH:$BIN_FOLDER" >>"$CONFIG_FILE"
echo "autoload -U +X compinit && compinit" >>"$CONFIG_FILE"
echo "source <($BIN_FOLDER/$BINARY_NAME completion zsh)" >>"$CONFIG_FILE"
;;
-fish | fish)
CONFIG_FILE="$HOME/.config/fish/config.fish"
if ! echo "# Kardinal CLI config" >>"$CONFIG_FILE"; then
handle_error
fi
echo "set -gx PATH \$PATH $BIN_FOLDER" >>"$CONFIG_FILE"
echo "source ($BIN_FOLDER/$BINARY_NAME completion fish | psub)" >>"$CONFIG_FILE"
;;
*)
echo "Unrecognized shell: $PARENT_SHELL"
handle_error
;;
esac
echo "$BINARY_NAME has been installed successfully!"
echo "Run the following command to load Kardinal in the current shell (new shell will already load it):"
echo ""
echo "> source $CONFIG_FILE"
echo ""
else
echo "Kardinal was installed before, just updated it."
echo ""
fi
else
echo "Failed to install $BINARY_NAME. Please try again."
echo ""
exit 1
fi

echo "$BINARY_NAME has been installed successfully!"

0 comments on commit 42c1b69

Please sign in to comment.