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() { 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() { error() {
printf "(>&2 \ (>&2 printf '\E[31mERROR:\E[0m %s\n' "$@" )
printf '%%s %%b' '$(ANSI_Red 'ERROR:')' '$(printf "%b " "${@:2}")') \
&& echo '' && return ${1} 2>/dev/null || exit ${1}"
} }
require_root() { require_root() {
if [[ $EUID -ne 0 ]]; then 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 fi
} }
@ -35,14 +22,9 @@ recommend_root() {
function setacl() { function setacl() {
if [[ $# -lt 4 ]]; then if [[ $# -lt 4 ]]; then
eval $(error 1 \ error "setacl missing arguments, called with '$1' ${@:2}"
"setacl missing arguments, called with '$1' ${@:2}" \ printf "%s\n" "Usage:" "\tsetacl <Options> <Ownership> <Permissions> <Path> [<Additional Paths>...]"
$(printf "%s\n" \ return 1
"Usage:" \
"\tsetacl <Options> <Ownership> <Permissions> <Path> [<Additional Paths>...]" \
)
)
return 1
fi fi
local Options=${1} local Options=${1}
local Ownership=${2} local Ownership=${2}

View File

@ -1,5 +1,4 @@
#alias sudo="/usr/bin/sudo " #alias sudo="/usr/bin/sudo "
alias _sudo="env sudo "
function sudo() { function sudo() {
local cli="$(command -v "${1}")" local cli="$(command -v "${1}")"
[[ -x "$cli" ]] || cli="${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 if [[ -n "$(which-command chezmoi)" ]]; then
export CHEZMOIROOT="$(chezmoi source-path)" export CHEZMOIROOT="$(chezmoi source-path)"
export CZ="$CHEZMOIROOT" export CZ="$CHEZMOIROOT"
@ -26,7 +21,7 @@ if [[ -n "$(which-command chezmoi)" ]]; then
alias czu="cz upgrade && cz update && exec $SHELL " alias czu="cz upgrade && cz update && exec $SHELL "
alias czxu="cz upgrade && cz update && czx apply && 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 " alias cz-commit="cz git -- commit -a "
function cz-remove-missing() { function cz-remove-missing() {

View File

@ -1,6 +1,6 @@
if [[ -n "$(which-command oh-my-posh)" ]]; then if [[ -n "$(which-command oh-my-posh)" ]]; then
OHMYPOSH_CONFIG='' OHMYPOSH_CONFIG=''
[[ -r ~/.poshtheme.omp.json ]] && OHMYPOSH_CONFIG="~/.poshtheme.omp.json" [[ -r ~/.poshtheme.omp.json ]] && OHMYPOSH_CONFIG="--config ~/.poshtheme.omp.json"
eval "$(oh-my-posh init bash --config $OHMYPOSH_CONFIG)" eval "$(oh-my-posh init bash $OHMYPOSH_CONFIG)"
unset OHMYPOSH_CONFIG unset OHMYPOSH_CONFIG
fi 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() { function which-command() {
local ANSWER=$(type -t "${@}") || return $?
[[ $ANSWER == "function" ]] && printf "function " && type "${@}" | tail +2 && return
[[ $ANSWER == "alias" ]] && {
alias "${@}"
}
type -fP "${@}" type -fP "${@}"
} }

View File

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

View File

@ -80,10 +80,25 @@ function _repo_custom_xscreensavers() {
############ Script Logic starts here ############ Script Logic starts here
set -e set -e
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME ANSI_Print() { printf "\E[${ANSI_Code}m%s\E[0m" "$@"; }
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" ANSI_Red() { ANSI_Code=31 ANSI_Print "$@"; }
. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions 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_. # The function that does the _Heavy Lifting_.
# see the code that follows for the parameter signature # see the code that follows for the parameter signature
@ -96,14 +111,14 @@ function add_repo() {
return $? return $?
# If no repo defintion exist, exit with an error # If no repo defintion exist, exit with an error
elif [[ "$(type -t _repo_$1)" != 'function' ]]; then 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 return 1
fi fi
# Grab the repo's defintion # Grab the repo's defintion
eval "_repo_$1" eval "_repo_$1"
# Prepare all necessary vraiables # 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_URL=${GPG_KEY_URL:-https:\/\/$REPO_FQDN\/gpg}
GPG_KEY_PATH=${GPG_KEY_PATH:-/usr/share/keyrings/$REPO_FQDN.gpg} GPG_KEY_PATH=${GPG_KEY_PATH:-/usr/share/keyrings/$REPO_FQDN.gpg}
REPO_ARCH=${REPO_ARCH:-${_ARC}} REPO_ARCH=${REPO_ARCH:-${_ARC}}

View File

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