From f0209da5e1b87d5930b4aef94adf85dc3ef0e707 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Mon, 31 Oct 2022 15:26:29 -0400 Subject: [PATCH] update fish and shell funcs --- config/fish/completions/nvm.fish | 21 ++ config/fish/conf.d/nvm.fish | 28 +++ config/fish/fish_plugins | 1 + config/fish/fish_variables | 12 +- config/fish/functions/_nvm_index_update.fish | 20 ++ config/fish/functions/_nvm_list.fish | 11 + .../fish/functions/_nvm_version_activate.fish | 4 + .../functions/_nvm_version_deactivate.fish | 5 + config/fish/functions/nvm.fish | 230 ++++++++++++++++++ jobs/everquote.fish | 4 +- jobs/everquote.sh | 2 +- shell_funcs.fish | 5 +- 12 files changed, 338 insertions(+), 5 deletions(-) create mode 100644 config/fish/completions/nvm.fish create mode 100644 config/fish/conf.d/nvm.fish create mode 100644 config/fish/functions/_nvm_index_update.fish create mode 100644 config/fish/functions/_nvm_list.fish create mode 100644 config/fish/functions/_nvm_version_activate.fish create mode 100644 config/fish/functions/_nvm_version_deactivate.fish create mode 100644 config/fish/functions/nvm.fish diff --git a/config/fish/completions/nvm.fish b/config/fish/completions/nvm.fish new file mode 100644 index 0000000..c0ab183 --- /dev/null +++ b/config/fish/completions/nvm.fish @@ -0,0 +1,21 @@ +complete --command nvm --exclusive +complete --command nvm --exclusive --long version --description "Print version" +complete --command nvm --exclusive --long help --description "Print help" +complete --command nvm --long silent --description "Suppress standard output" + +complete --command nvm --exclusive --condition __fish_use_subcommand --arguments install --description "Download and activate the specified Node version" +complete --command nvm --exclusive --condition __fish_use_subcommand --arguments use --description "Activate a version in the current shell" +complete --command nvm --exclusive --condition __fish_use_subcommand --arguments list --description "List installed versions" +complete --command nvm --exclusive --condition __fish_use_subcommand --arguments list-remote --description "List versions available to install matching optional regex" +complete --command nvm --exclusive --condition __fish_use_subcommand --arguments current --description "Print the currently-active version" +complete --command nvm --exclusive --condition "__fish_seen_subcommand_from install" --arguments "( + test -e $nvm_data && string split ' ' <$nvm_data/.index +)" +complete --command nvm --exclusive --condition "__fish_seen_subcommand_from use" --arguments "(_nvm_list | string split ' ')" +complete --command nvm --exclusive --condition __fish_use_subcommand --arguments uninstall --description "Uninstall a version" +complete --command nvm --exclusive --condition "__fish_seen_subcommand_from uninstall" --arguments "( + _nvm_list | string split ' ' | string replace system '' +)" +complete --command nvm --exclusive --condition "__fish_seen_subcommand_from use uninstall" --arguments "( + set --query nvm_default_version && echo default +)" diff --git a/config/fish/conf.d/nvm.fish b/config/fish/conf.d/nvm.fish new file mode 100644 index 0000000..8aab50a --- /dev/null +++ b/config/fish/conf.d/nvm.fish @@ -0,0 +1,28 @@ +function _nvm_install --on-event nvm_install + set --query nvm_mirror || set --universal nvm_mirror https://nodejs.org/dist + set --query XDG_DATA_HOME || set --local XDG_DATA_HOME ~/.local/share + set --universal nvm_data $XDG_DATA_HOME/nvm + + test ! -d $nvm_data && command mkdir -p $nvm_data + echo "Downloading the Node distribution index..." 2>/dev/null + _nvm_index_update +end + +function _nvm_update --on-event nvm_update + set --query nvm_mirror || set --universal nvm_mirror https://nodejs.org/dist + set --query XDG_DATA_HOME || set --local XDG_DATA_HOME ~/.local/share + set --universal nvm_data $XDG_DATA_HOME/nvm +end + +function _nvm_uninstall --on-event nvm_uninstall + command rm -rf $nvm_data + + set --query nvm_current_version && _nvm_version_deactivate $nvm_current_version + + set --names | string replace --filter --regex -- "^nvm" "set --erase nvm" | source + functions --erase (functions --all | string match --entire --regex -- "^_nvm_") +end + +if status is-interactive && set --query nvm_default_version && ! set --query nvm_current_version + nvm use --silent $nvm_default_version +end diff --git a/config/fish/fish_plugins b/config/fish/fish_plugins index 7e2785e..9e5301c 100644 --- a/config/fish/fish_plugins +++ b/config/fish/fish_plugins @@ -3,3 +3,4 @@ jorgebucaran/hydro danhper/fish-ssh-agent patrickf1/fzf.fish patrickf3139/colored-man-pages +jorgebucaran/nvm.fish diff --git a/config/fish/fish_variables b/config/fish/fish_variables index 9cef565..8df9d64 100644 --- a/config/fish/fish_variables +++ b/config/fish/fish_variables @@ -4,15 +4,21 @@ SETUVAR __fish_initialized:3400 SETUVAR _fisher_danhper_2F_fish_2D_ssh_2D_agent_files:\x7e/\x2econfig/fish/functions/__ssh_agent_is_started\x2efish\x1e\x7e/\x2econfig/fish/functions/__ssh_agent_start\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/fish\x2dssh\x2dagent\x2efish SETUVAR _fisher_jorgebucaran_2F_fisher_files:\x7e/\x2econfig/fish/functions/fisher\x2efish\x1e\x7e/\x2econfig/fish/completions/fisher\x2efish SETUVAR _fisher_jorgebucaran_2F_hydro_files:\x7e/\x2econfig/fish/functions/fish_mode_prompt\x2efish\x1e\x7e/\x2econfig/fish/functions/fish_prompt\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/hydro\x2efish +SETUVAR _fisher_jorgebucaran_2F_nvm_2E_fish_files:\x7e/\x2econfig/fish/functions/_nvm_index_update\x2efish\x1e\x7e/\x2econfig/fish/functions/_nvm_list\x2efish\x1e\x7e/\x2econfig/fish/functions/_nvm_version_activate\x2efish\x1e\x7e/\x2econfig/fish/functions/_nvm_version_deactivate\x2efish\x1e\x7e/\x2econfig/fish/functions/nvm\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/nvm\x2efish\x1e\x7e/\x2econfig/fish/completions/nvm\x2efish SETUVAR _fisher_patrickf1_2F_fzf_2E_fish_files:\x7e/\x2econfig/fish/functions/_fzf_configure_bindings_help\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_extract_var_info\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_preview_changed_file\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_preview_file\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_report_diff_type\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_report_file_type\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_directory\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_git_log\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_git_status\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_history\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_processes\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_variables\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_wrapper\x2efish\x1e\x7e/\x2econfig/fish/functions/fzf_configure_bindings\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/fzf\x2efish\x1e\x7e/\x2econfig/fish/completions/fzf_configure_bindings\x2efish SETUVAR _fisher_patrickf3139_2F_colored_2D_man_2D_pages_files:\x7e/\x2econfig/fish/functions/cless\x2efish\x1e\x7e/\x2econfig/fish/functions/man\x2efish -SETUVAR _fisher_plugins:jorgebucaran/fisher\x1ejorgebucaran/hydro\x1edanhper/fish\x2dssh\x2dagent\x1epatrickf1/fzf\x2efish\x1epatrickf3139/colored\x2dman\x2dpages +SETUVAR _fisher_plugins:jorgebucaran/fisher\x1ejorgebucaran/hydro\x1edanhper/fish\x2dssh\x2dagent\x1epatrickf1/fzf\x2efish\x1epatrickf3139/colored\x2dman\x2dpages\x1ejorgebucaran/nvm\x2efish SETUVAR _fisher_upgraded_to_4_4:\x1d +SETUVAR _hydro_git_14266:main\u2022\x20 +SETUVAR _hydro_git_40640:main\u2022\x20 SETUVAR _hydro_git_69626:main\u2022\x20 SETUVAR _hydro_git_72501:main\u2022\x20\u21911\x20 -SETUVAR _hydro_git_87186:main\u2022\x20 +SETUVAR _hydro_git_79475:main\u2022\x20 +SETUVAR _hydro_git_8811:main\u2022\x20 +SETUVAR _hydro_git_9276:main\u2022\x20 SETUVAR _hydro_git_92996:main\u2022\x20 SETUVAR _hydro_git_93415:main\u2022\x20 +SETUVAR _hydro_git_97138:main\u2022\x20 SETUVAR fish_color_autosuggestion:555\x1ebrblack SETUVAR fish_color_cancel:\x2dr SETUVAR fish_color_command:005fd7 @@ -44,3 +50,5 @@ SETUVAR fish_pager_color_selected_background:\x2dr SETUVAR fish_user_paths:/opt/homebrew/bin\x1e/Users/tyrel\x2esouza/\x2efzf/bin SETUVAR hydro_color_git:red SETUVAR hydro_color_pwd:green +SETUVAR nvm_data:/Users/tyrel\x2esouza/\x2elocal/share/nvm +SETUVAR nvm_mirror:https\x3a//nodejs\x2eorg/dist diff --git a/config/fish/functions/_nvm_index_update.fish b/config/fish/functions/_nvm_index_update.fish new file mode 100644 index 0000000..c1bbe28 --- /dev/null +++ b/config/fish/functions/_nvm_index_update.fish @@ -0,0 +1,20 @@ +function _nvm_index_update + test ! -d $nvm_data && command mkdir -p $nvm_data + + set --local index $nvm_data/.index + + if not command curl --location --silent $nvm_mirror/index.tab >$index.temp + command rm -f $index.temp + echo "nvm: Can't update index, host unavailable: \"$nvm_mirror\"" >&2 + return 1 + end + + command awk -v OFS=\t ' + /v0.9.12/ { exit } # Unsupported + NR > 1 { + print $1 (NR == 2 ? " latest" : $10 != "-" ? " lts/" tolower($10) : "") + } + ' $index.temp >$index + + command rm -f $index.temp +end diff --git a/config/fish/functions/_nvm_list.fish b/config/fish/functions/_nvm_list.fish new file mode 100644 index 0000000..fb5ab0e --- /dev/null +++ b/config/fish/functions/_nvm_list.fish @@ -0,0 +1,11 @@ +function _nvm_list + set --local versions $nvm_data/* + set --query versions[1] && + string match --entire --regex -- (string match --regex -- "v\d.+" $versions | + string escape --style=regex | + string join "|" + ) <$nvm_data/.index + + command --all node | + string match --quiet --invert --regex -- "^$nvm_data" && echo system +end diff --git a/config/fish/functions/_nvm_version_activate.fish b/config/fish/functions/_nvm_version_activate.fish new file mode 100644 index 0000000..f7dfef7 --- /dev/null +++ b/config/fish/functions/_nvm_version_activate.fish @@ -0,0 +1,4 @@ +function _nvm_version_activate --argument-names ver + set --global --export nvm_current_version $ver + set --prepend PATH $nvm_data/$ver/bin +end diff --git a/config/fish/functions/_nvm_version_deactivate.fish b/config/fish/functions/_nvm_version_deactivate.fish new file mode 100644 index 0000000..24dd36e --- /dev/null +++ b/config/fish/functions/_nvm_version_deactivate.fish @@ -0,0 +1,5 @@ +function _nvm_version_deactivate --argument-names ver + test "$nvm_current_version" = "$ver" && set --erase nvm_current_version + set --local index (contains --index -- $nvm_data/$ver/bin $PATH) && + set --erase PATH[$index] +end diff --git a/config/fish/functions/nvm.fish b/config/fish/functions/nvm.fish new file mode 100644 index 0000000..131a030 --- /dev/null +++ b/config/fish/functions/nvm.fish @@ -0,0 +1,230 @@ +function nvm --description "Node version manager" + for silent in --silent -s + if set --local index (contains --index -- $silent $argv) + set --erase argv[$index] && break + end + set --erase silent + end + + set --local cmd $argv[1] + set --local ver $argv[2] + + if set --query silent && ! set --query cmd[1] + echo "nvm: Version number not specified (see nvm -h for usage)" >&2 + return 1 + end + + if ! set --query ver[1] && contains -- "$cmd" install use + for file in .nvmrc .node-version + set file (_nvm_find_up $PWD $file) && read ver <$file && break + end + + if ! set --query ver[1] + echo "nvm: Invalid version or missing \".nvmrc\" file" >&2 + return 1 + end + end + + set --local their_version $ver + + switch "$cmd" + case -v --version + echo "nvm, version 2.2.11" + case "" -h --help + echo "Usage: nvm install Download and activate the specified Node version" + echo " nvm install Install version from nearest .nvmrc file" + echo " nvm use Activate a version in the current shell" + echo " nvm use Activate version from nearest .nvmrc file" + echo " nvm list List installed versions" + echo " nvm list-remote List versions available to install" + echo " nvm list-remote List versions matching a given regular expression" + echo " nvm current Print the currently-active version" + echo " nvm uninstall Uninstall a version" + echo "Options:" + echo " -s or --silent Suppress standard output" + echo " -v or --version Print version" + echo " -h or --help Print this help message" + echo "Variables:" + echo " nvm_arch Override architecture, e.g. x64-musl" + echo " nvm_mirror Use a mirror of the Node binaries" + echo " nvm_default_version Set the default version for new shells" + echo " nvm_default_packages Install a list of packages every time you install a Node version" + case install + _nvm_index_update + + string match --entire --regex -- (_nvm_version_match $ver) <$nvm_data/.index | read ver alias + + if ! set --query ver[1] + echo "nvm: Invalid version number or alias: \"$their_version\"" >&2 + return 1 + end + + if test ! -e $nvm_data/$ver + set --local os (command uname -s | string lower) + set --local ext tar.gz + set --local arch (command uname -m) + + switch $os + case aix + set arch ppc64 + case sunos + case linux + case darwin + case {MSYS_NT,MINGW\*_NT}\* + set os win + set ext zip + case \* + echo "nvm: Unsupported operating system: \"$os\"" >&2 + return 1 + end + + switch $arch + case i\*86 + set arch x86 + case x86_64 + set arch x64 + case arm64 + string match --regex --quiet "v(?\d+)" $ver + if test "$os" = darwin -a $major -lt 16 + set arch x64 + end + case armv6 armv6l + set arch armv6l + case armv7 armv7l + set arch armv7l + case armv8 armv8l aarch64 + set arch arm64 + end + + set --query nvm_arch && set arch $nvm_arch + + set --local dir "node-$ver-$os-$arch" + set --local url $nvm_mirror/$ver/$dir.$ext + + command mkdir -p $nvm_data/$ver + + if ! set --query silent + echo -e "Installing Node \x1b[1m$ver\x1b[22m $alias" + echo -e "Fetching \x1b[4m$url\x1b[24m\x1b[7m" + end + + if ! command curl $silent --progress-bar --location $url | + command tar --extract --gzip --directory $nvm_data/$ver 2>/dev/null + command rm -rf $nvm_data/$ver + echo -e "\033[F\33[2K\x1b[0mnvm: Invalid mirror or host unavailable: \"$url\"" >&2 + return 1 + end + + set --query silent || echo -en "\033[F\33[2K\x1b[0m" + + if test "$os" = win + command mv $nvm_data/$ver/$dir $nvm_data/$ver/bin + else + command mv $nvm_data/$ver/$dir/* $nvm_data/$ver + command rm -rf $nvm_data/$ver/$dir + end + end + + if test $ver != "$nvm_current_version" + set --query nvm_current_version && _nvm_version_deactivate $nvm_current_version + _nvm_version_activate $ver + + set --query nvm_default_packages[1] && npm install --global $silent $nvm_default_packages + end + + set --query silent || printf "Now using Node %s (npm %s) %s\n" (_nvm_node_info) + case use + test $ver = default && set ver $nvm_default_version + _nvm_list | string match --entire --regex -- (_nvm_version_match $ver) | read ver __ + + if ! set --query ver[1] + echo "nvm: Can't use Node \"$their_version\", version must be installed first" >&2 + return 1 + end + + if test $ver != "$nvm_current_version" + set --query nvm_current_version && _nvm_version_deactivate $nvm_current_version + test $ver != system && _nvm_version_activate $ver + end + + set --query silent || printf "Now using Node %s (npm %s) %s\n" (_nvm_node_info) + case uninstall + if test -z "$ver" + echo "nvm: Not enough arguments for command: \"$cmd\"" >&2 + return 1 + end + + test $ver = default && test ! -z "$nvm_default_version" && set ver $nvm_default_version + + _nvm_list | string match --entire --regex -- (_nvm_version_match $ver) | read ver __ + + if ! set -q ver[1] + echo "nvm: Node version not installed or invalid: \"$their_version\"" >&2 + return 1 + end + + set --query silent || printf "Uninstalling Node %s %s\n" $ver (string replace ~ \~ "$nvm_data/$ver/bin/node") + + _nvm_version_deactivate $ver + + command rm -rf $nvm_data/$ver + case current + _nvm_current + case ls list + _nvm_list | _nvm_list_format (_nvm_current) $argv[2] + case lsr {ls,list}-remote + _nvm_index_update || return + _nvm_list | command awk ' + FILENAME == "-" && (is_local[$1] = FNR == NR) { next } { + print $0 (is_local[$1] ? " ✓" : "") + } + ' - $nvm_data/.index | _nvm_list_format (_nvm_current) $argv[2] + case \* + echo "nvm: Unknown command or option: \"$cmd\" (see nvm -h for usage)" >&2 + return 1 + end +end + +function _nvm_find_up --argument-names path file + test -e "$path/$file" && echo $path/$file || begin + test ! -z "$path" || return + _nvm_find_up (string replace --regex -- '/[^/]*$' "" $path) $file + end +end + +function _nvm_version_match --argument-names ver + string replace --regex -- '^v?(\d+|\d+\.\d+)$' 'v$1.' $ver | + string replace --filter --regex -- '^v?(\d+)' 'v$1' | + string escape --style=regex || + string lower '\b'$ver'(?:/\w+)?$' +end + +function _nvm_list_format --argument-names current regex + command awk -v current="$current" -v regex="$regex" ' + $0 ~ regex { + aliases[versions[i++] = $1] = $2 " " $3 + pad = (n = length($1)) > pad ? n : pad + } + END { + if (!i) exit 1 + while (i--) + printf((current == versions[i] ? " ▶ " : " ") "%"pad"s %s\n", + versions[i], aliases[versions[i]]) + } + ' +end + +function _nvm_current + command --search --quiet node || return + set --query nvm_current_version && echo $nvm_current_version || echo system +end + +function _nvm_node_info + set --local npm_path (string replace bin/npm-cli.js "" (realpath (command --search npm))) + test -f $npm_path/package.json || set --local npm_version_default (command npm --version) + command node --eval " + console.log(process.version) + console.log('$npm_version_default' ? '$npm_version_default': require('$npm_path/package.json').version) + console.log(process.execPath.replace(require('os').homedir(), '~')) + " +end diff --git a/jobs/everquote.fish b/jobs/everquote.fish index d6c3402..6ec5c79 100644 --- a/jobs/everquote.fish +++ b/jobs/everquote.fish @@ -1,7 +1,7 @@ #!/bin/bash # ALIASES -alias kubectl=/opt/homebrew/Cellar/kubernetes-cli@1.22/1.22.10/bin/kubectl +alias kubectl=/opt/homebrew/Cellar/kubernetes-cli@1.22/1.22.15/bin/kubectl alias ibrew="arch -x86_64 /usr/local/bin/brew" @@ -16,11 +16,13 @@ end function kubestaging () kubectl config use-context shared1-east1-us-staging kubectl config set-context --current --namespace=remarketing + set -x AWS_PROFILE eks end function kubeprod () kubectl config use-context shared1-east1-us-prod kubectl config set-context --current --namespace=remarketing + set -x AWS_PROFILE eks end function setup_crypto () # Needed for cryptography diff --git a/jobs/everquote.sh b/jobs/everquote.sh index c50d4b1..ea0697b 100644 --- a/jobs/everquote.sh +++ b/jobs/everquote.sh @@ -1,7 +1,7 @@ #!/bin/bash # ALIASES -alias kubectl=/opt/homebrew/Cellar/kubernetes-cli@1.22/1.22.10/bin/kubectl +alias kubectl=/opt/homebrew/Cellar/kubernetes-cli@1.22/1.22.15/bin/kubectl alias ibrew="arch -x86_64 /usr/local/bin/brew" diff --git a/shell_funcs.fish b/shell_funcs.fish index 5c5e334..765a175 100644 --- a/shell_funcs.fish +++ b/shell_funcs.fish @@ -47,7 +47,10 @@ end function allbranches for d in ./*/ - cd "$d" && pwd && git remote -v;git pull + cd "$d" + set branch (git symbolic-ref --short -q HEAD) + set repo (basename (pwd)) + echo $branch $repo cd .. end end