Skip to content

Commit

Permalink
Add install.sh.
Browse files Browse the repository at this point in the history
  • Loading branch information
avm committed Jul 2, 2024
1 parent 74d5752 commit 0f1f6a6
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# nil_cli
# `=nil;` CLI

Install on macOS and Linux by running:
```shell
curl -fsSL https://github.com/NilFoundation/nil_cli/raw/master/install.sh | bash
```
249 changes: 249 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
#!/usr/bin/env bash
set -euo pipefail

platform=$(uname -ms)

if [[ ${OS:-} = Windows_NT ]]; then
echo >&2 'Windows is not supported'
exit 1
fi

# Reset
Color_Off=''

# Regular Colors
Red=''
Green=''
Dim='' # White

# Bold
Bold_White=''
Bold_Green=''

if [[ -t 1 ]]; then
# Reset
Color_Off='\033[0m' # Text Reset

# Regular Colors
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Dim='\033[0;2m' # White

# Bold
Bold_Green='\033[1;32m' # Bold Green
Bold_White='\033[1m' # Bold White
fi

error() {
echo -e "${Red}error${Color_Off}:" "$@" >&2
exit 1
}

info() {
echo -e "${Dim}$@ ${Color_Off}"
}

info_bold() {
echo -e "${Bold_White}$@ ${Color_Off}"
}

success() {
echo -e "${Green}$@ ${Color_Off}"
}

if [[ $# -gt 0 ]]; then
error 'Too many arguments'
fi

case $platform in
'Darwin x86_64')
target=darwin-x64
;;
'Darwin arm64')
target=darwin-aarch64
;;
'Linux x86_64' | *)
target=linux-x64
;;
esac

exe_name=nil_cli

if [[ $target = darwin-x64 ]]; then
# Is this process running in Rosetta?
# redirect stderr to devnull to avoid error message when not running in Rosetta
if [[ $(sysctl -n sysctl.proc_translated 2>/dev/null) = 1 ]]; then
target=darwin-aarch64
info "Your shell is running in Rosetta 2. Downloading $exe_name for $target instead"
fi
fi

GITHUB=${GITHUB-"https://github.com"}

github_repo="$GITHUB/NilFoundation/nil_cli"

nil_cli_uri=$github_repo/releases/latest/download/$exe_name-$target

bin_env=\$HOME/.local/bin

install_dir="$HOME/.local"
bin_dir=$install_dir/bin
exe=$bin_dir/$exe_name

if [[ ! -d $bin_dir ]]; then
mkdir -p "$bin_dir" ||
error "Failed to create install directory \"$bin_dir\""
fi

curl --fail --location --progress-bar --output "$exe.dl" "$nil_cli_uri" ||
error "Failed to download $exe_name from \"$nil_cli_uri\""

mv "$exe.dl" "$exe" ||
error "Failed to move $exe_name to \"$exe\""

chmod +x "$exe" ||
error "Failed to set permissions on the $exe_name executable"

tildify() {
if [[ $1 = $HOME/* ]]; then
local replacement=\~/

echo "${1/$HOME\//$replacement}"
else
echo "$1"
fi
}

success "$exe_name was installed successfully to $Bold_Green$(tildify "$exe")"

if command -v $exe_name >/dev/null; then
echo "Run '$exe_name --help' to get started"
exit
fi

refresh_command=''

tilde_bin_dir=$(tildify "$bin_dir")

echo

case $(basename "$SHELL") in
fish)
commands=(
"set --export PATH $bin_env \$PATH"
)

fish_config=$HOME/.config/fish/config.fish
tilde_fish_config=$(tildify "$fish_config")

if [[ -w $fish_config ]]; then
{
echo -e '\n# nil_cli'

for command in "${commands[@]}"; do
echo "$command"
done
} >>"$fish_config"

info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_fish_config\""

refresh_command="source $tilde_fish_config"
else
echo "Manually add the directory to $tilde_fish_config (or similar):"

for command in "${commands[@]}"; do
info_bold " $command"
done
fi
;;
zsh)
commands=(
"export PATH=\"$bin_env:\$PATH\""
)

zsh_config=$HOME/.zshrc
tilde_zsh_config=$(tildify "$zsh_config")

if [[ -w $zsh_config ]]; then
{
echo -e '\n# nil_cli'

for command in "${commands[@]}"; do
echo "$command"
done
} >>"$zsh_config"

info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_zsh_config\""

refresh_command="exec $SHELL"
else
echo "Manually add the directory to $tilde_zsh_config (or similar):"

for command in "${commands[@]}"; do
info_bold " $command"
done
fi
;;
bash)
commands=(
"export PATH=$bin_env:\$PATH"
)

bash_configs=(
"$HOME/.bashrc"
"$HOME/.bash_profile"
)

if [[ ${XDG_CONFIG_HOME:-} ]]; then
bash_configs+=(
"$XDG_CONFIG_HOME/.bash_profile"
"$XDG_CONFIG_HOME/.bashrc"
"$XDG_CONFIG_HOME/bash_profile"
"$XDG_CONFIG_HOME/bashrc"
)
fi

set_manually=true
for bash_config in "${bash_configs[@]}"; do
tilde_bash_config=$(tildify "$bash_config")

if [[ -w $bash_config ]]; then
{
echo -e '\n# nil_cli'

for command in "${commands[@]}"; do
echo "$command"
done
} >>"$bash_config"

info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_bash_config\""

refresh_command="source $bash_config"
set_manually=false
break
fi
done

if [[ $set_manually = true ]]; then
echo "Manually add the directory to $tilde_bash_config (or similar):"

for command in "${commands[@]}"; do
info_bold " $command"
done
fi
;;
*)
echo 'Manually add the directory to ~/.bashrc (or similar):'
info_bold " export PATH=\"$bin_env:\$PATH\""
;;
esac

echo
info "To get started, run:"
echo

if [[ $refresh_command ]]; then
info_bold " $refresh_command"
fi

info_bold " nil_cli --help"

0 comments on commit 0f1f6a6

Please sign in to comment.