Last active
February 28, 2017 17:13
-
-
Save berttejeda/a19f6c9588a41675652edde586f836ec to your computer and use it in GitHub Desktop.
Modular Bash Profile based on git-hosted source files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
functions.import() { | |
local_script_cache_root=~/.src_bash_profile | |
if ! [[ -d $local_script_cache_root ]];then mkdir $local_script_cache_root || echo "Could not create local script cache ${local_script_cache_root}";fi | |
f.import () { | |
flags="${@: -1}" | |
if [[ $flags != '--update' ]];then echo "Importing cached scripts, specify the --update flag if you want to update cached versions";fi | |
f_git_url=$1; f_index_file=$2; f_cache_dir=$3 | |
local_script_cache="${local_script_cache_root}/${f_cache_dir}" | |
if ! [[ -d $local_script_cache ]];then mkdir $local_script_cache || echo "Could not create local script cache ${local_script_cache}";fi | |
cache_index_file="${local_script_cache}/${f_index_file}" | |
curl_command="curl -Lks --connect-timeout 5 --max-time 5 curl -Lks --connect-timeout 5 --max-time 5" | |
if [[ $flags == '--update' ]];then | |
# Read remote index | |
scripts=$($curl_command "${f_git_url}/${f_index_file}") | |
# Create Cached Index File | |
if ! [[ -z "${scripts}" ]];then | |
echo -e "${scripts}" > "${cache_index_file}" | |
else | |
echo "Looks like you can't access the remote url ${f_git_url}. Let's try accessing the cached script index ..." | |
if ! [[ -f $cache_index_file ]];then | |
echo "Dangit ... No cache script index found (${cache_index_file}). You're SOL." | |
return 1 | |
else | |
echo -e "Cool, we found a cached script index at ${cache_index_file}" | |
scripts=$(cat ${cache_index_file}) | |
fi | |
fi | |
else | |
scripts=$(cat ${cache_index_file}) | |
fi | |
for script in $scripts; do | |
cached_script="${local_script_cache}/${script##*/}" | |
script_url=${f_git_url}/${script} | |
if [[ $flags == '--update' ]];then | |
functions=$($curl_command ${f_git_url}/${script}) | |
if ! [[ -z "$functions" ]];then | |
echo "${functions}" > $cached_script || echo "Could not write to ${cached_script}!" | |
source <(echo "$functions") && echo -e "${green}Successfully sourced ${script}${green}" || echo -e "${red}Failed in sourcing ${script}${reset}" | |
else | |
echo -e "${red}Error encountered when attempting to load ${script_url}, check your connection to the git repo${reset}" | |
if [[ -f "${cached_script}" ]];then | |
echo "Importing cached script ${cached_script}" | |
source "${cached_script}" && echo -e "${green}OK for cached version of ${script}${reset}" | |
else | |
echo "Bummer. No cached script found at ${cached_script}" | |
fi | |
fi | |
else | |
if [[ -f "${cached_script}" ]];then source "${cached_script}" && echo -e "${green}Imported cached version of ${script}${reset}";fi | |
fi | |
done | |
echo "${reset}Done" | |
} | |
default () { | |
# if [[ ("$*" =~ .*--cache_mode.*) ]]; then echo 'cache mode engaged!';fi | |
echo "Attempting to import ${FUNCNAME[0]} Functions ..." | |
git_url="https://raw.githubusercontent.com/berttejeda/supracmd/master/shells/lib" | |
index_file=".index" | |
flags="${@: -1}" | |
# Essentially, we are querying an index file containing the filenames of all would-be source files | |
# e.g. curl -Lks https://somegitrepo/.index | |
f.import $git_url $index_file ${FUNCNAME[0]} $flags | |
} | |
# somelabel () { | |
# echo "Attempting to import ${FUNCNAME[0]} Functions ..." | |
# git_url="https://somegitrepo" | |
# index_file=".index" | |
# f.import $git_url $index_file ${FUNCNAME[0]} $flags | |
# } | |
if [[ ("$1" =~ .*--cache_mode.*) || ($# -lt 1) ]]; then eval default $*;else eval $*;fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment