utils/bash/bashrc/rbashrc

268 lines
6.4 KiB
Plaintext
Raw Normal View History

2022-08-11 13:31:05 +00:00
#!/bin/bash
2015-03-04 04:43:33 +00:00
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
2016-04-29 07:34:32 +00:00
__SCRIPT=$BASH_SOURCE
2022-08-11 12:36:59 +00:00
RBASH_HOME="$HOME/.rbash"
RBASH_SOURCES="$RBASH_HOME/sources"
2022-08-11 12:46:58 +00:00
RBASH_CONFIG="$RBASH_HOME/config"
2022-08-11 13:04:52 +00:00
RBASH_REMOTE="https://git.k8s.astropenguin.net"
RBASH_REPO="penguin/utils"
RBASH_PATH="bash/bashrc"
2016-04-28 01:45:44 +00:00
2015-03-04 04:43:33 +00:00
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
2015-05-21 02:52:44 +00:00
# Source for Custom bash ENV
if [ -f ~/.rbashenv ]; then
. ~/.rbashenv
fi
2022-08-11 12:36:59 +00:00
mkdir -p "$RBASH_SOURCES"
2015-03-04 04:43:33 +00:00
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
#[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
2022-08-11 14:48:00 +00:00
function __func_head() { echo "Usage:" ${FUNCNAME[1]} $1; }
function __func_help() { echo " ${FUNCNAME[1]} $1"; }
2016-02-22 00:31:54 +00:00
function __download {
which curl 2>&1 > /dev/null
if [ $? -eq 0 ]; then
curl -s "$1"
2022-08-11 12:36:59 +00:00
return
2016-02-22 00:31:54 +00:00
fi
which wget 2>&1 > /dev/null
if [ $? -eq 0 ]; then
wget -qO- "$1"
2022-08-11 12:36:59 +00:00
return
2016-02-22 00:31:54 +00:00
fi
}
2022-08-11 12:36:59 +00:00
function rbash_cache {
local CACHE_FILE
MLINK=$( echo "$1" | md5sum | cut -d' ' -f1 )
CACHE_FILE="$RBASH_SOURCES/$MLINK"
if [ ! -f "$CACHE_FILE" ]; then
__download "$1" > "$CACHE_FILE"
2015-11-24 07:56:43 +00:00
fi
2022-08-11 12:36:59 +00:00
chmod 700 "$CACHE_FILE"
2022-08-11 12:42:00 +00:00
echo -n "$CACHE_FILE"
2015-11-24 07:56:43 +00:00
}
2022-08-11 13:06:49 +00:00
# User specific aliases and functions
function rbash_load {
local f path
2022-08-11 14:48:00 +00:00
echo "Load: $1"
2022-08-11 13:06:49 +00:00
f=`rbash_cache "$RBASH_REMOTE/$RBASH_REPO/raw/branch/master/$RBASH_PATH/$1"`
source "$f"
}
function rbash-upgrade {
2016-04-28 01:45:44 +00:00
echo "Updating $__SCRIPT"
2022-08-11 12:36:59 +00:00
local TMPFILE
TMPFILE=$( mktemp )
2022-08-11 13:04:52 +00:00
__download "$RBASH_REMOTE/$RBASH_REPO/raw/branch/master/$RBASH_PATH/rbashrc" > $TMPFILE
2015-03-04 04:43:33 +00:00
if [ -z "$1" ]; then
MC_NAME="<MACHINE_NAME>"
MCC_NAME="<MACHINE_COLORED_NAME>"
else
MC_NAME=$1
MCC_NAME=$2
fi
PMC_NAME=$( echo "#_MACHINE_NAME_#" | sed -e "s/#_/</g" -e "s/_#/>/g" )
PMCC_NAME=$( echo "#_MACHINE_COLORED_NAME_#" | sed -e "s/#_/</g" -e "s/_#/>/g" )
2016-02-21 12:25:03 +00:00
COLOR_CODE_TOK=$( echo "#_COLOR_CODE_#" | sed -e "s/#_/</g" -e "s/_#/>/g" )
COLOR_CODE=$(( $RANDOM * 6 / 32767 + 1 ))
2015-03-10 04:00:40 +00:00
sed -i \
-e "s/$PMC_NAME/$MC_NAME/g" \
-e "s/$PMCC_NAME/$MCC_NAME/g" \
2016-02-21 12:29:20 +00:00
-e "s/$COLOR_CODE_TOK/$COLOR_CODE/g" \
2015-03-10 04:00:40 +00:00
$TMPFILE
2016-04-28 01:45:44 +00:00
mv $TMPFILE $__SCRIPT
2022-08-10 05:19:45 +00:00
# Clean up the old dir
2022-08-11 12:36:59 +00:00
if [ $? -eq 0 ] && [ -d "$RBASH_SOURCES" ]; then
rm -r "$RBASH_SOURCES"
2022-08-10 05:19:45 +00:00
fi
2022-08-11 12:36:59 +00:00
source $__SCRIPT
2015-03-04 04:43:33 +00:00
}
2022-08-11 13:06:49 +00:00
function rbash-run {
2022-08-11 12:36:59 +00:00
local f
2015-03-06 03:24:05 +00:00
echo "Getting: $1"
2022-08-11 12:36:59 +00:00
2022-08-11 13:04:52 +00:00
f=`rbash_cache "$RBASH_REMOTE/$RBASH_REPO/raw/branch/master/$1"`
2015-03-06 03:24:05 +00:00
shift
2015-03-10 04:00:40 +00:00
2022-08-11 12:36:59 +00:00
"$f" "$@"
2015-03-06 03:24:05 +00:00
}
2022-08-11 13:31:05 +00:00
function _require_jq {
2022-08-11 13:04:52 +00:00
which jq 2>&1 > /dev/null
2022-08-11 13:06:49 +00:00
if [ $? -ne 0 ]; then
2022-08-11 13:04:52 +00:00
echo This command requires jq.
return 1
fi
2022-08-11 13:31:05 +00:00
}
2022-08-11 15:38:24 +00:00
declare -A _RBASH_DICT
2022-08-11 13:31:05 +00:00
function rbash-list {
_require_jq || return 1
2022-08-11 15:28:02 +00:00
2022-08-11 13:19:17 +00:00
echo "Available sources:"
2022-08-11 14:48:00 +00:00
local _k
2022-08-11 13:31:05 +00:00
for i in `__download "$RBASH_REMOTE/api/v1/repos/$RBASH_REPO/contents/$RBASH_PATH/sources/" | jq -r '.[].name'`; do
2022-08-11 15:35:23 +00:00
_k=`echo -n $i | grep -o "^[0-9]\+"`
2022-08-11 14:48:00 +00:00
_RBASH_DICT["s$_k"]=$i
2022-08-11 13:31:05 +00:00
grep -q -e "^rbash_load \"sources/$i\"\$" "$RBASH_CONFIG"
if [ $? -eq 0 ]; then
echo " * $i"
else
echo " $i"
fi
done
}
2022-08-11 14:48:00 +00:00
function r2ensource {
rbash-list
local _id _sel _tmp _en
_en=()
read -p "Enter the script id XX, delimited by space: " _ids
for _id in $_ids; do
_sel=${_RBASH_DICT["s$_id"]}
if [ -z "$_sel" ]; then
echo "No such id $_id"
return 1
fi
grep -q -e "^rbash_load \"sources/$_sel\"\$" "$RBASH_CONFIG"
if [ $? -eq 0 ]; then
echo "\"$_sel\" is already enabled."
continue
fi
_en+=("$_sel")
done
_tmp=$( mktemp )
cp "$RBASH_CONFIG" "$_tmp"
for _id in "${_en[@]}"; do
echo "rbash_load \"sources/$_id\"" >> $_tmp
echo "Enabling $_id"
done
sort $_tmp > "$RBASH_CONFIG"
rm $_tmp
}
2022-08-11 13:31:05 +00:00
2022-08-11 14:48:00 +00:00
function r2dissource {
echo "Currently enabled sources:"
grep -e "^rbash_load \"sources/" "$RBASH_CONFIG" | cut -c 21- | tr -d '"' | sed "s/^/ $1/g"
local _id _dis _opts
_dis=()
read -p "Enter the script id XX, delimited by space: " _ids
for _id in $_ids; do
_dis+=("$( grep -e "^rbash_load \"sources/${_id}_" "$RBASH_CONFIG" )")
if [ $? -ne 0 ]; then
echo "No such id: $_id"
return 1
fi
done
_opts=""
for _id in "${_dis[@]}"; do
_opts="-e \"^$( echo -n $_id | sed 's/"/\\"/g' )$\" $_opts"
echo "Disabling "$( echo -n $_id | sed 's/rbash_load \"sources\/\([^"]\+\)"/\1/g' )
done
_dis=$( mktemp )
sh -c "grep -v $_opts \"$RBASH_CONFIG\"" > $_dis
sort $_dis > $RBASH_CONFIG
rm $_dis
2022-08-11 13:04:52 +00:00
}
2022-08-11 12:46:58 +00:00
export PS1='This is <MACHINE_NAME>\e[1;3<COLOR_CODE>m<MACHINE_COLORED_NAME>\e[0m: \w\n\$ '
export EDITOR=vim
2022-08-11 12:36:59 +00:00
# Create default source config
2022-08-11 12:46:58 +00:00
if [ ! -f "$RBASH_CONFIG" ]; then
cat <<___DEFAULT___ > "$RBASH_CONFIG"
rbash_load "sources/10_aliases"
rbash_load "sources/12_shortcuts"
rbash_load "sources/20_fast-greps"
rbash_load "sources/40_go-command"
rbash_load "sources/41_pivot-command"
2022-08-11 12:36:59 +00:00
___DEFAULT___
2022-08-11 12:46:58 +00:00
chmod 600 "$RBASH_CONFIG"
2015-03-04 04:43:33 +00:00
fi
2022-08-11 12:36:59 +00:00
2022-08-11 12:46:58 +00:00
source "$RBASH_CONFIG"