From ab3e569190d84eb7e9d23483ab731acb6b94af91 Mon Sep 17 00:00:00 2001 From: Andrei Jiroh Halili Date: Sun, 7 Jul 2024 17:46:25 +0800 Subject: [PATCH] chore(bash): update shellrc files as usual Signed-off-by: Andrei Jiroh Halili --- .bash_login | 8 ++-- .bashrc | 5 ++- .config/bash/aliases | 2 +- .config/bash/bashrc | 3 +- .config/bash/functions | 67 ++++++++++++++++++++++++++++++++++ .config/bash/tools/asdf.bashrc | 6 ++- .profile | 1 - 7 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 .config/bash/functions diff --git a/.bash_login b/.bash_login index 2c5fca9..9d029c1 100644 --- a/.bash_login +++ b/.bash_login @@ -1,16 +1,17 @@ #!/usr/bin/env bash +# # shellcheck disable=SC2046,SC1091,SC2155 # Stage 0: Source dotenv stuff from homedir source "$HOME/.env" if [[ -f "$HOME/.env.local" ]]; then source "$HOME/.env.local" - LOCAL_DOTENV_LOADED=true + export LOCAL_DOTENV_LOADED=true fi if [[ $TERMUX ]]; then export SSH_AGENT_=todo elif command -v keychain >> /dev/null; then - export KEYCHAIN_PATh=$(command -v keychain) + export KEYCHAIN_PATH=$(command -v keychain) eval $(keychain --agents gpg,ssh --eval) fi @@ -19,4 +20,5 @@ if [ -f "$HOME/.config/localconfig.env" ]; then . "$HOME/.config/localconfig.env" fi -_byobu_sourced=1 . /usr/bin/byobu-launch 2>/dev/null || true +# # shellcheck source=./.bashrc +source ./.bashrc diff --git a/.bashrc b/.bashrc index 0b77504..ce721b7 100644 --- a/.bashrc +++ b/.bashrc @@ -63,8 +63,9 @@ if [[ -d "$HOME/.bashbox" ]]; then source "$HOME/.bashbox/env" fi -# handle hostname generation for importing host-specific configs -# TODO: Handle detection when we don't have WSL_* variables on tmux shell sessions +# Formerly: handle hostname generation for importing host-specific configs +# TODO: Handle detection across distributions without chaos, especially where +# Nix is installed (not NixOS) if [[ $WSL_DISTRO_NAME ]] && [[ $WSL_INTEROP ]]; then HOSTNAME_BASH="$(cat /etc/hostname)-wsl-${WSL_DISTRO_NAME}" export WSL=1 # similar to CODESPACES and GITPOD_WORKSPACE_ID vars diff --git a/.config/bash/aliases b/.config/bash/aliases index 308e1d8..068c7c5 100644 --- a/.config/bash/aliases +++ b/.config/bash/aliases @@ -1,7 +1,7 @@ # General alias clear="printf '\033c'" # faster than ncurses clear by a lot alias c='clear' -alias bashrc="nano ~/.bashrc && source ~/.bashrc" +alias bashrc="${EDITOR:-"nano"} ~/.bashrc && source ~/.bashrc" # LS alias ls='ls --color=auto -FAh' alias ll='ls -l' diff --git a/.config/bash/bashrc b/.config/bash/bashrc index 6ad1247..17ae775 100644 --- a/.config/bash/bashrc +++ b/.config/bash/bashrc @@ -68,8 +68,9 @@ export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quo # ~/.bash_aliases, instead of adding them here directly. # See /usr/share/doc/bash-doc/examples in the bash-doc package. -source "${HOME}/.config/aliases" +source "${HOME}/.config/bash/aliases" source "${HOME}/.config/bash/tools/automated-deploy.bashrc" +source "${HOME}/.config/bash/functions" # enable programmable completion features (you don't need to enable # this, if it's already enabled in /etc/bash.bashrc and /etc/profile diff --git a/.config/bash/functions b/.config/bash/functions new file mode 100644 index 0000000..b58fbd7 --- /dev/null +++ b/.config/bash/functions @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2046 + +# handle .env.keys detection +_load_env_keys() { + if [[ "$LAST_DOTENV_DIR" == "$PWD" ]]; then + return + elif [[ "$LAST_DOTENV_DIR" != "$PWD" ]] && [ -f "$LAST_DOTENV_DIR/.env.keys" ]; then + return + fi + + if [ -f "$PWD/.env.keys" ] && [[ "$LOADED_DOTENV_KEYS" != "1" ]]; then + echo "dotenv-keys: loading up dotenv keys from this directory" + + # TODO: Add source link since it is obviously copied from Stack Overflow. + unamestr=$(uname) + if [ "$unamestr" = 'Linux' ]; then + export $(grep -v '^#' .env.keys | xargs -d '\n') + elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then + export $(grep -v '^#' .env.keys | xargs -0) + fi + + export DOTENV_KEYS_LOADED=1 LAST_DOTENV_DIR=$PWD DOTENV_KEYS_LOADER=auto + elif [ ! -f "$PWD/.env.keys" ] && [[ "$LOADED_DOTENV_KEYS" == "1" ]]; then + echo "dotenv-keys: unloading dotenv keys" + unset "${!DOTENV_PRIVATE_KEYS*}" DOTENV_KEYS_LOADED DOTENV_KEYS_LOADER + export LAST_DOTENV_DIR=$PWD + fi +} + +if [[ ";${PROMPT_COMMAND[*]:-};" != *";_load_env_keys;"* ]]; then + if [[ "$(declare -p PROMPT_COMMAND 2>&1)" == "declare -a"* ]]; then + PROMPT_COMMAND=(_load_env_keys "${PROMPT_COMMAND[@]}") + else + PROMPT_COMMAND="_load_env_keys${PROMPT_COMMAND:+;$PROMPT_COMMAND}" + fi +fi + +# Allow manual access to .env.keys, even without the keys autoloader. +dotenv-keys() { + if [[ $1 == "load" ]]; then + if [ ! -f "$PWD/.env.keys" ]; then + echo "error: missing dotenv encryption keys" + return 1 + fi + + echo "dotenv-keys: loading up dotenv keys from this directory" + # TODO: Add source link since it is obviously copied from Stack Overflow. + unamestr=$(uname) + if [ "$unamestr" = 'Linux' ]; then + export $(grep -v '^#' .env.keys | xargs -d '\n') + elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then + export $(grep -v '^#' .env.keys | xargs -0) + fi + + export LOADED_DOTENV_KEYS=$PWD DOTENV_KEYS_LOADER=manual DOTENV_KEYS_LOADED=1 + elif [[ $1 == "unload" ]]; then + echo "dotenv-keys: manually unloading dotenv keys" + unset "${!DOTENV_PRIVATE_KEY*}" DOTENV_KEYS_LOADED DOTENV_KEYS_LOADER DOTENV_KEYS_LOADED + else + echo "dotenv-keys - .env.keys manager for dotenvx" + echo "" + echo "Commands:" + echo " load - load keys from .env.keys in current directory into shell session" + echo " unload - unload keys from shell session" + fi +} \ No newline at end of file diff --git a/.config/bash/tools/asdf.bashrc b/.config/bash/tools/asdf.bashrc index fd59173..8588448 100644 --- a/.config/bash/tools/asdf.bashrc +++ b/.config/bash/tools/asdf.bashrc @@ -1,3 +1,7 @@ #!/usr/bin/env bash -[ -s "$HOME/.asdf/asdf.sh" ] && source "$HOME/.asdf/asdf.sh" \ No newline at end of file +[ -s "$HOME/.asdf/asdf.sh" ] && source "$HOME/.asdf/asdf.sh" + +if [[ $ASDF_DIR ]]; then + eval "$(direnv hook bash)" # Load up direnv hook after asdf +fi diff --git a/.profile b/.profile index b0e15dd..8132640 100644 --- a/.profile +++ b/.profile @@ -41,5 +41,4 @@ if [ -f "$HOME/.config/localconfig.env" ]; then fi if [[ $FF_BYOBU_ON_LOGIN != "" ]]; then - _byobu_sourced=1 . /usr/bin/byobu-launch 2>/dev/null || true fi \ No newline at end of file