Skip to content

new plugin: config plugin will load/save your bash-it configuration #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions plugins/available/config.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
cite about-plugin _about _param _group
about-plugin "Manages your personal bash-it configuration"

BASH_IT_CONFIG_PATH="${BASH_IT}/.config"
GIT="/usr/bin/env git"

_config-read-git-url()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the function _config-read-git-url () syntax here? PR #315 fixed all of these to avoid parse errors that we were exposing ourselves to.

{
_about "reads a Git repo URL/path from stdin"
_group "config"
while true; do
read -e -p "Enter URL/path of git repository for config: " GIT_URL
[[ -n ${GIT_URL} ]] && break;
done
echo ${GIT_URL}
}

_config-write()
{
_about "writes a config file"
_param "1: config file to write to"
_param "2..: array of things to write to config file"
_group "config"
local THING=${1}
shift
printf "%s\n" $@ > ${THING}
}

_config-make-config-file()
{
local CWD=`pwd`
_about "builds & writes a config file"
_param "1: source directory, presumably full of symlinks"
_param "2: destination config file"
_group "config"
local SOURCE_DIR=$1
local DEST_FILE=$2
local -a FILES=()
for FILE in "${BASH_IT}/${SOURCE_DIR}/enabled/*.bash"; do
FILES=(${FILES[@]} `basename -s .bash ${FILE}`);
done
[[ ${#FILES[@]} -gt 0 ]] && {
_config-write "${DEST_FILE}" "${FILES[@]}"
cd "${BASH_IT_CONFIG_PATH}"
${GIT} add "${DEST_FILE}"
cd ${CWD}
}
}

_config-load-config-file()
{
_about "loads a config file and creates symlinks"
_param "1: source config file"
_param "2: destination directory"
_group "config"
local SOURCE_FILE=$1
local DEST_DIR=$2
[[ -e ${SOURCE_FILE} && -d ${DEST_DIR} ]] && {
for FILE in `find "${DEST_DIR}/enabled" -type l`; do
rm -f "${FILE}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the file first?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures you have an exact clone of the settings, but I guess this is simply a preference.

done
for FILE in `cat ${SOURCE_FILE}`; do
bash-it enable `basename ${SOURCE_FILE}` ${FILE} 2>&1>/dev/null
done
}
}

save-bash-it ()
{
local CWD=`pwd`
about "saves current bash-it configuration to source control"
group "config"
local CUSTOM_PATH="${BASH_IT_CONFIG_PATH}/custom"
${GIT} init "${BASH_IT_CONFIG_PATH}" 2>&1 > /dev/null
_config-make-config-file "aliases" "${BASH_IT_CONFIG_PATH}/aliases"
_config-make-config-file "plugins" "${BASH_IT_CONFIG_PATH}/plugins"
_config-make-config-file "completion" "${BASH_IT_CONFIG_PATH}/completion"
mkdir -p "${CUSTOM_PATH}"

[[ ! -L ${BASH_IT}/custom && `ls -A "${BASH_IT}/custom/"` ]] && {
cp -Hf "${BASH_IT}/"custom/* "${CUSTOM_PATH}/" && \
rm -rf "${BASH_IT}/custom" && \
ln -sf "${CUSTOM_PATH}" "${BASH_IT}/custom"
}

[[ -e ${HOME}/.bash_profile ]] && {
cp -Hf "${HOME}/.bash_profile" "${BASH_IT_CONFIG_PATH}/bash_profile" 2>/dev/null && \
rm -f "${HOME}/.bash_profile" && \
ln -sf "${BASH_IT_CONFIG_PATH}/bash_profile" "${HOME}/.bash_profile"
}

cd "${BASH_IT_CONFIG_PATH}"
${GIT} add ${BASH_IT_CONFIG_PATH}/bash_profile
cd "${CUSTOM_PATH}"
${GIT} add .
[[ -z `git remote` ]] && {
local ORIGIN=`_config-read-git-url`
${GIT} remote add origin "${ORIGIN}"
}
commit-bash-it
pull-bash-it
push-bash-it
cd ${CWD}
}

commit-bash-it()
{
local CWD=`pwd`
about "commits current bash-it configuration to source control"
group "config"
cd ${BASH_IT_CONFIG_PATH}
${GIT} commit -m "Updates $@"
cd ${CWD}
}

push-bash-it()
{
local CWD=`pwd`
about "pushes current bash-it configuration to remote origin"
group "config"
cd ${BASH_IT_CONFIG_PATH}
${GIT} push origin master
cd ${CWD}
}

pull-bash-it()
{
local CWD=`pwd`
about "pulls (+ rebases) bash-it configuration from remote origin"
group "config"
if [[ ! -d ${BASH_IT_CONFIG_PATH} ]]; then
local ORIGIN=`_config-read-git-url`
${GIT} clone ${ORIGIN} ${BASH_IT_CONFIG_PATH}
fi
cd ${BASH_IT_CONFIG_PATH}
${GIT} pull --rebase origin master
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to --rebase here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess not. You can configure this as the default in your .gitconfig if you want.

cd ${CWD}
}

update-bash-it()
{
about "pulls bash-it configuration from remote origin and loads it"
group "config"
pull-bash-it
load-bash-it
}

load-bash-it ()
{
about "loads stored bash-it configuration; creates symlinks"
group "config"
_config-load-config-file "${BASH_IT_CONFIG_PATH}/aliases" "${BASH_IT}/aliases"
_config-load-config-file "${BASH_IT_CONFIG_PATH}/plugins" "${BASH_IT}/plugins"
_config-load-config-file "${BASH_IT_CONFIG_PATH}/completion" "${BASH_IT}/completion"

[[ -d ${BASH_IT_CONFIG_PATH}/custom ]] && {
# TODO maybe a backup here
[[ ! -L ${BASH_IT}/custom && -d ${BASH_IT}/custom ]] && rm -rf "${BASH_IT}/custom"
ln -sf "${BASH_IT_CONFIG_PATH}/custom" "${BASH_IT}/"
}

[[ ! -L ${HOME}/.bash_profile ]] && {
# TODO here too
rm "${HOME}/.bash_profile"
ln -sf "${BASH_IT_CONFIG_PATH}/bash_profile" "${HOME}/.bash_profile"
}
}