Improvements

This commit is contained in:
Gal@Shefet 2022-10-05 22:29:21 -04:00
parent 726463c3ab
commit 589b6fa2f4
9 changed files with 55 additions and 56 deletions

View File

@ -1,28 +1,15 @@
ANSI_Print() { printf "\E[${ANSI_Code}m%s\E[0m" "$@"; }
ANSI_Red() { ANSI_Code=31 ANSI_Print "$@"; }
ANSI_Green() { ANSI_Code=32 ANSI_Print "$@"; }
ANSI_Yellow() { ANSI_Code=33 ANSI_Print "$@"; }
ANSI_Blue() { ANSI_Code=34 ANSI_Print "$@"; }
ANSI_Indiego() { ANSI_Code=35 ANSI_Print "$@"; }
ANSI_Cyan() { ANSI_Code=36 ANSI_Print "$@"; }
warn() {
(>&2 printf '%s %s\n' $(ANSI_Yellow 'WARNING:') "$(echo "$@")" )
(>&2 printf '\E[34mWARNING:\E[0m %s\n' "$@" )
}
# The way to call this is:
# eval $(error 1 "message") || exit $?
# where 1 is the return/exit code and "message" is the message to be
# displayed
error() {
printf "(>&2 \
printf '%%s %%b' '$(ANSI_Red 'ERROR:')' '$(printf "%b " "${@:2}")') \
&& echo '' && return ${1} 2>/dev/null || exit ${1}"
(>&2 printf '\E[31mERROR:\E[0m %s\n' "$@" )
}
require_root() {
if [[ $EUID -ne 0 ]]; then
eval $(error 1 "This script should only be run using sudo or as the root user")
error "This script should only be run using sudo or as the root user"
exit 1
fi
}
@ -35,13 +22,8 @@ recommend_root() {
function setacl() {
if [[ $# -lt 4 ]]; then
eval $(error 1 \
"setacl missing arguments, called with '$1' ${@:2}" \
$(printf "%s\n" \
"Usage:" \
"\tsetacl <Options> <Ownership> <Permissions> <Path> [<Additional Paths>...]" \
)
)
error "setacl missing arguments, called with '$1' ${@:2}"
printf "%s\n" "Usage:" "\tsetacl <Options> <Ownership> <Permissions> <Path> [<Additional Paths>...]"
return 1
fi
local Options=${1}

View File

@ -1,5 +1,4 @@
#alias sudo="/usr/bin/sudo "
alias _sudo="env sudo "
function sudo() {
local cli="$(command -v "${1}")"
[[ -x "$cli" ]] || cli="${1}"

View File

@ -1,8 +1,3 @@
if [[ -z "$(which-command chezmoi)" && -x "$HOME/bin/chezmoi" ]]; then
function chezmoi() {
"$HOME/bin/chezmoi" "${@}"
}
fi
if [[ -n "$(which-command chezmoi)" ]]; then
export CHEZMOIROOT="$(chezmoi source-path)"
export CZ="$CHEZMOIROOT"
@ -26,7 +21,7 @@ if [[ -n "$(which-command chezmoi)" ]]; then
alias czu="cz upgrade && cz update && exec $SHELL "
alias czxu="cz upgrade && cz update && czx apply && exec $SHELL "
alias cz-refresh="czs | cut -d\ -f2 | grep '^\.sz\.shrc\.d' | xargs chezmoi forget --force; cz add ~/.sz.shrc.d --recursive; cz status; cz git status"
alias cz-refresh="czs | cut -d\ -f2 | grep '^\.sz\.shrc\.d' | xargs cz forget --force; cz add ~/.sz.shrc.d --recursive; cz status; cz git status"
alias cz-commit="cz git -- commit -a "
function cz-remove-missing() {

View File

@ -1,6 +1,6 @@
if [[ -n "$(which-command oh-my-posh)" ]]; then
OHMYPOSH_CONFIG=''
[[ -r ~/.poshtheme.omp.json ]] && OHMYPOSH_CONFIG="~/.poshtheme.omp.json"
eval "$(oh-my-posh init bash --config $OHMYPOSH_CONFIG)"
[[ -r ~/.poshtheme.omp.json ]] && OHMYPOSH_CONFIG="--config ~/.poshtheme.omp.json"
eval "$(oh-my-posh init bash $OHMYPOSH_CONFIG)"
unset OHMYPOSH_CONFIG
fi

View File

@ -0,0 +1,21 @@
if ( uname --kernel-release | grep -q truenas ); then
# Completion code is only avialable with `sudo`, the aliases below
# are desined for 'lazy-loading' the completion code, when it's time
# to enter the sudo password.
if [[ -n "$(which-command 'k3s')" ]]; then
alias kubectl=' \
### Lazy load `kubectl` bash-completion \
alias kubectl="env sudo k3s kubectl "; \
source <( kubectl completion bash ); \
env sudo k3s kubectl '
fi
if [[ -n "$(which-command 'helm')" ]]; then
alias helm=' \
### Lazy load `helm` bash-completion \
alias helm="env sudo KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm "; \
source <( helm completion bash ); \
env sudo KUBECONFIG=/etc/rancher/k3s/k3s.yaml helm '
fi
fi

View File

@ -1,9 +1,5 @@
alias which-command="type -fP "
function which-command() {
local ANSWER=$(type -t "${@}") || return $?
[[ $ANSWER == "function" ]] && printf "function " && type "${@}" | tail +2 && return
[[ $ANSWER == "alias" ]] && {
alias "${@}"
}
type -fP "${@}"
}

View File

@ -1,9 +1 @@
function which-command() {
local ANSWER=$(whence -w "${@}") || return $?
ANSWER=${ANSWER##*: }
[[ $ANSWER == "function" ]] && printf "function " && whence -f "${@}" && return
[[ $ANSWER == "alias" ]] && {
alias "${@}"
}
whence "${@}"
}
alias which-command="whence "

View File

@ -80,10 +80,25 @@ function _repo_custom_xscreensavers() {
############ Script Logic starts here
set -e
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions
ANSI_Print() { printf "\E[${ANSI_Code}m%s\E[0m" "$@"; }
ANSI_Red() { ANSI_Code=31 ANSI_Print "$@"; }
ANSI_Green() { ANSI_Code=32 ANSI_Print "$@"; }
ANSI_Yellow() { ANSI_Code=33 ANSI_Print "$@"; }
ANSI_Blue() { ANSI_Code=34 ANSI_Print "$@"; }
ANSI_Indiego() { ANSI_Code=35 ANSI_Print "$@"; }
ANSI_Cyan() { ANSI_Code=36 ANSI_Print "$@"; }
error() {
(>&2 ANSI_Red 'ERROR: ' && printf '%s\n' "$@" )
}
require_root() {
if [[ $EUID -ne 0 ]]; then
error "This script should only be run using sudo or as the root user"
exit 1
fi
}
# The function that does the _Heavy Lifting_.
# see the code that follows for the parameter signature
@ -96,14 +111,14 @@ function add_repo() {
return $?
# If no repo defintion exist, exit with an error
elif [[ "$(type -t _repo_$1)" != 'function' ]]; then
eval $(error 1 "Can't configure $1, could not find _repo_$1 definition!") || return $?
error "Can't configure $1, could not find _repo_$1 definition!"
return 1
fi
# Grab the repo's defintion
eval "_repo_$1"
# Prepare all necessary vraiables
[[ -z "$REPO_FQDN" ]] && eval $(error 1 "REPO_FQDN not defined for '$1'")
[[ -z "$REPO_FQDN" ]] && error "REPO_FQDN not defined for '$1'" && return 1
GPG_KEY_URL=${GPG_KEY_URL:-https:\/\/$REPO_FQDN\/gpg}
GPG_KEY_PATH=${GPG_KEY_PATH:-/usr/share/keyrings/$REPO_FQDN.gpg}
REPO_ARCH=${REPO_ARCH:-${_ARC}}

View File

@ -1,5 +1,4 @@
#! /usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions