Skip to content

Instantly share code, notes, and snippets.

@nexus166
Last active March 12, 2019 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nexus166/32888166b5434b5cf91a72bfb84a3b9c to your computer and use it in GitHub Desktop.
Save nexus166/32888166b5434b5cf91a72bfb84a3b9c to your computer and use it in GitHub Desktop.
#!/bin/sh
# add this function to your env and then import other scripts with:
# eval "$(ghload gist nexus166/msg 581472463579c73e15aa71e6db610cb7)"
ghload() {
MOD_TYPE=${1}; TOLOAD=${2}; MODULE=${3};
GH_NAME="$(echo ${TOLOAD} | cut -d'/' -f1)"; GH_REPO="$(echo $TOLOAD | cut -d'/' -f2)";
ghGISTS="https://gist.github.com"; ghREPOS="https://raw.githubusercontent.com";
_man() { printf "Usage:\\n\\t\$> ghload gist nexus166/msg 581472463579c73e15aa71e6db610cb7\n\\t\$> ghload repo Spajderix/barc README.md\\n"; };
for v in MOD_TYPE TOLOAD MODULE GH_NAME GH_REPO; do if [ -z $(eval "echo \$$v") ]; then _man; return 127; break; fi; done;
GETTER="$(command -v wget || command -v curl || which wget || which curl)";
_getter() {
TARGET=${1};
if [ ! -z "$(printf ${GETTER} | grep wget)" ]; then
eval "${GETTER} -q -O- ${TARGET}";
elif [ ! -z "$(prinf ${GETTER} | grep curl)" ]; then
eval "${GETTER} -fsSL -o- ${TARGET}";
else
return 128;
fi;
};
case ${MOD_TYPE} in
g | G | gist | GIST)
HASH="${MODULE}";
TOFETCH=$(_getter "${ghGISTS}/${GH_NAME}/${HASH}" | grep Raw | grep -Po '(?<=href=")[^"]*(?=")');
_getter "${ghGISTS}${TOFETCH}";
return $?;
;;
r | R | repo | REPO) _getter "${ghREPOS}/${GH_NAME}/${GH_REPO}/master/${MODULE}"; return $?;;
*) _man; return 127;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment