#!/usr/bin/env bash set -euo pipefail platform=$(uname -ms) if [[ ${OS:-} = Windows_NT ]]; then if [[ $# -gt 0 ]]; then echo 'error: to install a specific version on Windows, run this in PowerShell instead:' >&2 echo " iex \"& {\$(irm https://install.omni-clm.com/install.ps1)} -Version $1\"" >&2 exit 1 fi powershell -c "irm https://install.omni-clm.com/install.ps1 | iex" exit $? 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}" } command -v tar >/dev/null || error 'tar is required to install omni' if [[ $# -gt 1 ]]; then error 'Too many arguments, only 1 is allowed: a specific version of omni to install. (e.g. "2.1.0")' fi case $platform in 'Darwin x86_64') target=darwin-amd64 ;; 'Darwin arm64') target=darwin-arm64 ;; 'Linux aarch64' | 'Linux arm64') target=linux-arm64 ;; 'Linux x86_64') target=linux-amd64 ;; *) error "omni is not available for $platform" ;; esac if [[ $target = darwin-amd64 ]]; 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-arm64 info "Your shell is running in Rosetta 2. Downloading omni for $target instead" fi fi github_repo=https://github.com/omniscient-public/binaries if [[ $# = 0 ]]; then omni_uri=$github_repo/releases/latest/download/omni-cli-$target.tar.gz else tag=$1 if [[ $tag =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]]; then tag=omni-cli@${tag#v} fi omni_uri=$github_repo/releases/download/$tag/omni-cli-$target.tar.gz fi omni_repo=${OMNI_REPO:-} if [[ $omni_repo ]]; then command -v gh >/dev/null || error "Installing from \"$omni_repo\" needs the GitHub CLI (gh), signed in to an account that can read it" tags=$(gh release list --repo "$omni_repo" --limit 1000 --json tagName --jq '.[].tagName' | grep -E 'cli@[0-9]+\.[0-9]+\.[0-9]+$') || error "Found no omni release in \"$omni_repo\"" if [[ $# = 0 ]]; then tag=$(echo "$tags" | awk -F @ '{ print $NF " " $0 }' | sort -t . -k 1,1n -k 2,2n -k 3,3n | tail -n 1 | cut -d ' ' -f 2) elif [[ $1 =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]]; then tag=$(echo "$tags" | awk -F @ -v version="${1#v}" '$NF == version { print; exit }') [[ $tag ]] || error "No omni release for version \"${1#v}\" in \"$omni_repo\"" else tag=$1 fi fi install_env=OMNI_INSTALL bin_env=\$$install_env/bin install_dir=${!install_env:-$HOME/.omni} bin_dir=$install_dir/bin exe=$bin_dir/omni if [[ ! -d $bin_dir ]]; then mkdir -p "$bin_dir" || error "Failed to create install directory \"$bin_dir\"" fi if [[ $omni_repo ]]; then gh release download "$tag" --repo "$omni_repo" --pattern "omni-cli-$target.tar.gz" --output "$exe.tar.gz" --clobber || error "Failed to download omni-cli-$target.tar.gz from the \"$tag\" release in \"$omni_repo\"" else curl --fail --location --progress-bar --output "$exe.tar.gz" "$omni_uri" || error "Failed to download omni from \"$omni_uri\"" fi mkdir -p "$bin_dir/omni-cli-$target" && tar -xzf "$exe.tar.gz" -C "$bin_dir/omni-cli-$target" omni || error 'Failed to extract omni' if [[ $target = darwin-* ]] && ! codesign --verify "$bin_dir/omni-cli-$target/omni" &>/dev/null; then codesign --force --sign - "$bin_dir/omni-cli-$target/omni" &>/dev/null || error 'Failed to sign the omni executable' fi mv "$bin_dir/omni-cli-$target/omni" "$exe" || error 'Failed to move extracted omni to destination' chmod +x "$exe" || error 'Failed to set permissions on omni executable' rm -r "$bin_dir/omni-cli-$target" "$exe.tar.gz" "$exe" --version &>/dev/null || error "omni was installed to \"$exe\", but it failed to run" tildify() { if [[ $1 = $HOME/* ]]; then local replacement=\~/ echo "${1/$HOME\//$replacement}" else echo "$1" fi } success "omni $("$exe" --version) was installed successfully to $Bold_Green$(tildify "$exe")" if command -v omni >/dev/null; then echo "Run 'omni --help' to get started" exit fi refresh_command='' tilde_bin_dir=$(tildify "$bin_dir") quoted_install_dir=\"${install_dir//\"/\\\"}\" if [[ $quoted_install_dir = \"$HOME/* ]]; then quoted_install_dir=${quoted_install_dir/$HOME\//\$HOME/} fi echo case $(basename "$SHELL") in fish) commands=( "set --export $install_env $quoted_install_dir" "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# omni' 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 $install_env=$quoted_install_dir" "export PATH=\"$bin_env:\$PATH\"" ) zsh_config=$HOME/.zshrc tilde_zsh_config=$(tildify "$zsh_config") if [[ -w $zsh_config ]]; then { echo -e '\n# omni' 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 $install_env=$quoted_install_dir" "export PATH=\"$bin_env:\$PATH\"" ) bash_configs=( "$HOME/.bash_profile" "$HOME/.bashrc" ) 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# omni' 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 $install_env=$quoted_install_dir" 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 " omni --help"