Skip to content

Instantly share code, notes, and snippets.

@BaksiLi
Last active August 12, 2023 06:58
Show Gist options
  • Save BaksiLi/5f6b9c4fc0c2b70ab05ab27c4aab80ff to your computer and use it in GitHub Desktop.
Save BaksiLi/5f6b9c4fc0c2b70ab05ab27c4aab80ff to your computer and use it in GitHub Desktop.
Add import (`load_func`) online scripts to zsh/bash
load_func() {
local CMD_NAME="$1"
local SCRIPT_URL="$2"
local SAFETY_CHECK="${3:-true}"
local script=$(curl -fsSL $SCRIPT_URL)
if [[ "$SAFETY_CHECK" == "true" && $script =~ "rm " ]]; then
echo "Failed to load command $CMD_NAME: Harmful command detected in script."
return 1
else
eval "$script"
if ! command -v $CMD_NAME > /dev/null; then
echo "Failed to load command $CMD_NAME."
return 1
fi
fi
return 0
}
@BaksiLi
Copy link
Author

BaksiLi commented Aug 10, 2023

Example:

load_func proxy "https://gist.githubusercontent.com/BaksiLi/fab184db1f5a466ea0108a6384cdab9f/raw/13c1e9517f95102610b23cc5cae4f61aa32b02be/proxy.zsh"

will load the function in the GitHub gist proxy.zsh.

You can also use a shortened URL, for example:

load_func undock https://url.baksili.codes/u/bq4B3a 0

will load undock.sh (redirected). Pay attention to the third argument. When set to false, it will skip the security check in the script.

Disclaimer: DO NOT load any function/script that you are unsure about. USE WITH CAUTION.

@BaksiLi
Copy link
Author

BaksiLi commented Aug 12, 2023

If you use Zinit plugin manager for ZSH, you can try this install_compsnip.zsh.

Same functionality is achieved by:

install_gist_snippet 'BaksiLi/zinit-remove' 'BaksiLi/498c63a1cd12e3dd1f6b7426206c33ed'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment