Merge pull request #1865 from NoahGorny/add-profile-subcommand
add profile subcommand
This commit is contained in:
241
lib/helpers.bash
241
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 list|save|load|rm [profile_name]'
|
||||
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,172 @@ _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
|
||||
|
||||
local something_exists
|
||||
echo "# This file is auto generated by Bash-it. Do not edit manually!" > "$profile_path"
|
||||
for subdirectory in "plugins" "completion" "aliases"; do
|
||||
local component_exists=""
|
||||
echo "Saving $subdirectory configuration..."
|
||||
for f in "${BASH_IT}/$subdirectory/available/"*.bash; do
|
||||
_bash-it-determine-component-status-from-path "$f"
|
||||
if [ "$enabled" == "x" ]; then
|
||||
if [ -z "$component_exists" ]; then
|
||||
# This is the first component of this type, print the header
|
||||
component_exists="yes"
|
||||
something_exists="yes"
|
||||
echo "" >> "$profile_path"
|
||||
echo "# $subdirectory" >> "$profile_path"
|
||||
fi
|
||||
echo "$subdirectory $enabled_file_clean" >> "$profile_path"
|
||||
fi
|
||||
done
|
||||
done
|
||||
if [ -z "$something_exists" ]; then
|
||||
echo "It seems like no configuration was enabled.."
|
||||
echo "Make sure to double check that this is the wanted behavior."
|
||||
fi
|
||||
|
||||
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-list() {
|
||||
about 'lists all profiles from the "profiles" directory'
|
||||
_group 'lib'
|
||||
|
||||
echo "Available profiles:"
|
||||
for profile in "${BASH_IT}/profiles"/*.bash_it; do
|
||||
profile="${profile##*/}"
|
||||
echo "${profile/.bash_it/}"
|
||||
done
|
||||
}
|
||||
|
||||
_bash-it-profile-rm() {
|
||||
about 'Removes a profile from the "profiles" directory'
|
||||
_group 'lib'
|
||||
local name="$1"
|
||||
if [[ -z $name ]]; then
|
||||
echo -e "\033[91mPlease specify profile name to remove...\033[m"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Users should not be allowed to delete the default profile
|
||||
if [[ $name == "default" ]]; then
|
||||
echo -e "\033[91mCan not remove the default profile...\033[m"
|
||||
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\"...\033[m"
|
||||
return 1
|
||||
fi
|
||||
|
||||
command rm "$profile_path"
|
||||
echo "Removed profile \"$name\" successfully!"
|
||||
}
|
||||
|
||||
_bash-it-profile-load() {
|
||||
_about 'loads a configuration from the "profiles" directory'
|
||||
_group 'lib'
|
||||
|
||||
local name="$1"
|
||||
if [[ -z $name ]]; then
|
||||
echo -e "\033[91mPlease specify profile name to load, not changing configuration...\033[m"
|
||||
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[m"
|
||||
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'
|
||||
@@ -492,6 +661,25 @@ _bash-it-reload() {
|
||||
popd &> /dev/null || return
|
||||
}
|
||||
|
||||
_bash-it-determine-component-status-from-path ()
|
||||
{
|
||||
_about 'internal function used to process component name and check if its enabled'
|
||||
_param '1: full path to available component file'
|
||||
_example '$ _bash-it-determine-component-status-from-path "${BASH_IT}/plugins/available/git.plugin.bash'
|
||||
|
||||
# Check for both the old format without the load priority, and the extended format with the priority
|
||||
declare enabled_files enabled_file
|
||||
enabled_file="${f##*/}"
|
||||
enabled_file_clean=$(echo "$enabled_file" | sed -e 's/\(.*\)\..*\.bash/\1/g')
|
||||
enabled_files=$(sort <(compgen -G "${BASH_IT}/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") | wc -l)
|
||||
|
||||
if [ "$enabled_files" -gt 0 ]; then
|
||||
enabled='x'
|
||||
else
|
||||
enabled=' '
|
||||
fi
|
||||
}
|
||||
|
||||
_bash-it-describe ()
|
||||
{
|
||||
_about 'summarizes available bash_it components'
|
||||
@@ -511,17 +699,8 @@ _bash-it-describe ()
|
||||
printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description'
|
||||
for f in "${BASH_IT}/$subdirectory/available/"*.bash
|
||||
do
|
||||
# Check for both the old format without the load priority, and the extended format with the priority
|
||||
declare enabled_files enabled_file
|
||||
enabled_file="${f##*/}"
|
||||
enabled_files=$(sort <(compgen -G "${BASH_IT}/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") | wc -l)
|
||||
|
||||
if [ $enabled_files -gt 0 ]; then
|
||||
enabled='x'
|
||||
else
|
||||
enabled=' '
|
||||
fi
|
||||
printf "%-20s%-10s%s\n" "$(basename $f | sed -e 's/\(.*\)\..*\.bash/\1/g')" " [$enabled]" "$(cat $f | metafor about-$file_type)"
|
||||
_bash-it-determine-component-status-from-path "$f"
|
||||
printf "%-20s%-10s%s\n" "$enabled_file_clean" " [$enabled]" "$(cat $f | metafor about-$file_type)"
|
||||
done
|
||||
printf '\n%s\n' "to enable $preposition $file_type, do:"
|
||||
printf '%s\n' "$ bash-it enable $file_type <$file_type name> [$file_type name]... -or- $ bash-it enable $file_type all"
|
||||
@@ -540,6 +719,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'
|
||||
@@ -623,7 +813,11 @@ _disable-thing ()
|
||||
|
||||
_bash-it-clean-component-cache "${file_type}"
|
||||
|
||||
printf '%s\n' "$file_entity disabled."
|
||||
if [ "$file_entity" = "all" ]; then
|
||||
printf '%s\n' "$file_entity $(_bash-it-pluralize-component "$file_type") disabled."
|
||||
else
|
||||
printf '%s\n' "$file_entity disabled."
|
||||
fi
|
||||
}
|
||||
|
||||
_enable-plugin ()
|
||||
@@ -636,6 +830,12 @@ _enable-plugin ()
|
||||
_enable-thing "plugins" "plugin" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN
|
||||
}
|
||||
|
||||
_enable-plugins ()
|
||||
{
|
||||
_about 'alias of _enable-plugin'
|
||||
_enable-plugin "$@"
|
||||
}
|
||||
|
||||
_enable-alias ()
|
||||
{
|
||||
_about 'enables bash_it alias'
|
||||
@@ -646,6 +846,12 @@ _enable-alias ()
|
||||
_enable-thing "aliases" "alias" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS
|
||||
}
|
||||
|
||||
_enable-aliases ()
|
||||
{
|
||||
_about 'alias of _enable-alias'
|
||||
_enable-alias "$@"
|
||||
}
|
||||
|
||||
_enable-completion ()
|
||||
{
|
||||
_about 'enables bash_it completion'
|
||||
@@ -802,6 +1008,17 @@ _help-plugins()
|
||||
rm $grouplist 2> /dev/null
|
||||
}
|
||||
|
||||
_help-profile () {
|
||||
_about 'help message for profile command'
|
||||
_group 'lib'
|
||||
|
||||
echo "Manages profiles of bash it."
|
||||
echo "Use 'bash-it profile list' to see all available profiles."
|
||||
echo "Use 'bash-it profile save foo' to save the current configuration into a profile named 'foo'."
|
||||
echo "Use 'bash-it profile load foo' to load an existing profile named 'foo'."
|
||||
echo "Use 'bash-it profile rm foo' to remove an existing profile named 'foo'."
|
||||
}
|
||||
|
||||
_help-update () {
|
||||
_about 'help message for update command'
|
||||
_group 'lib'
|
||||
|
||||
Reference in New Issue
Block a user