From 7f60b73a2ac7dedbfc6ddad2e77f51c134475062 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Sat, 13 Feb 2021 17:20:18 +0200 Subject: [PATCH] helpers: Add new bash-it profile subcommand --- lib/helpers.bash | 132 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/lib/helpers.bash b/lib/helpers.bash index 5f31a0fa..681f4630 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -108,6 +108,7 @@ bash-it () example '$ bash-it version' example '$ bash-it reload' example '$ bash-it restart' + example '$ bash-it profile save|load my_profile' example '$ bash-it doctor errors|warnings|all' typeset verb=${1:-} shift @@ -126,6 +127,8 @@ bash-it () func=_help-$component;; doctor) func=_bash-it-doctor-$component;; + profile) + func=_bash-it-profile-$component;; search) _bash-it-search $component "$@" return;; @@ -457,6 +460,124 @@ _bash-it-doctor-() { _bash-it-doctor-all } +_bash-it-profile-save() { + _about 'saves the current configuration to the "profile" directory' + _group 'lib' + + local name=$1 + while [ -z "$1" ]; do + read -r -e -p "Please enter the name of the profile to save: " name + case $name in + "") + echo -e "\033[91mPlease choose a name.\033[m" + ;; + *) + break + ;; + esac + done + + local profile_path="${BASH_IT}/profiles/${name}.bash_it" + if [ -f "$profile_path" ]; then + echo -e "\033[0;33mProfile \"$name\" already exists.\033[m" + while true; do + read -r -e -n 1 -p "Would you like to overwrite existing profile? [y/N] " RESP + case $RESP in + [yY]) + echo -e "\033[0;32mOverwriting profile \"$name\"...\033[m" + rm "$profile_path" + break + ;; + [nN] | "") + echo -e "\033[91mAborting profile save...\033[m" + return 1 + ;; + *) + echo -e "\033[91mPlease choose y or n.\033[m" + ;; + esac + done + fi + + echo "# This file is auto generated by Bash-it. Do not edit manually!" > "$profile_path" + for subdirectory in "plugins" "completion" "aliases"; do + echo "Saving $subdirectory configuration..." + echo "" >> "$profile_path" + echo "# $subdirectory" >> "$profile_path" + for f in "${BASH_IT}/$subdirectory/available/"*.bash; do + _bash-it-process-component "$f" + if [ "$enabled" == "x" ]; then + echo "$subdirectory $enabled_file_clean" >> "$profile_path" + fi + done + done + echo "All done!" + echo "" + echo "Profile location: $profile_path" + echo "Load the profile by invoking \"bash-it profile load $name\"" +} + +_bash-it-profile-load-parse-profile() { + _about 'Internal function used to parse the profile file' + _param '1: path to the profile file' + _param '2: dry run- only check integrity of the profile file' + _example '$ _bash-it-profile-load-parse-profile "profile.bash_it" "dry"' + + local num=0 + while read -r -a line; do + num=$((num + 1)) + # Ignore comments and empty lines + [[ -z "${line[*]}" || "${line[*]}" =~ ^#.* ]] && continue + local enable_func="_enable-${line[0]}" + local subdirectory=${line[0]} + local component=${line[1]} + + typeset to_enable=$(command ls "${BASH_IT}/$subdirectory/available/$component".*bash 2>/dev/null | head -1) + # Ignore botched lines + if [[ -z "$to_enable" ]]; then + echo -e "\033[91mBad line(#$num) in profile, aborting load...\033[m" + local bad="bad line" + break + fi + # Do not actually modify config on dry run + [[ -z $2 ]] || continue + # Actually enable the component + $enable_func "$component" + done < "$1" + + # Make sure to propagate the error + [[ -z $bad ]] +} + +_bash-it-profile-load() { + _about 'loads a configuration from the "profile" directory' + _group 'lib' + + local name="$1" + if [[ -z $name ]]; then + echo -e "\033[91mPlease specify profile name to load, not changing configuration...\033" + return 1 + fi + + local profile_path="${BASH_IT}/profiles/$name.bash_it" + if [[ ! -f "$profile_path" ]]; then + echo -e "\033[91mCould not find profile \"$name\", not changing configuration...\033" + return 1 + fi + + echo "Trying to parse profile \"$name\"..." + if _bash-it-profile-load-parse-profile "$profile_path" "dry"; then + echo "Profile \"$name\" parsed successfully!" + echo "Disabling current configuration..." + _disable-all + echo "" + echo "Enabling configuration based on profile..." + _bash-it-profile-load-parse-profile "$profile_path" + echo "" + echo "Profile \"$name\" enabled!" + fi +} + _bash-it-restart() { _about 'restarts the shell in order to fully reload it' _group 'lib' @@ -550,6 +671,17 @@ _on-disable-callback() _command_exists $callback && $callback } +_disable-all () +{ + _about 'disables all bash_it components' + _example '$ _disable-all' + _group 'lib' + + _disable-plugin "all" + _disable-alias "all" + _disable-completion "all" +} + _disable-plugin () { _about 'disables bash_it plugin'