Skip to content

Commit

Permalink
new bash
Browse files Browse the repository at this point in the history
  • Loading branch information
JV-conseil committed Mar 20, 2023
1 parent 6f6c0a6 commit 49e30dd
Show file tree
Hide file tree
Showing 45 changed files with 1,509 additions and 360 deletions.
8 changes: 8 additions & 0 deletions .bash/incl/.shebang.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
#====================================================
35 changes: 35 additions & 0 deletions .bash/incl/_aliases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
#====================================================

export LANG="en_US.UTF-8"

# export CLICOLOR=1
# export LSCOLORS=ExFxBxDxCxegedabagacad

# print environment variables sorted by name
# <https://stackoverflow.com/a/60756021/2477854>
alias env="env -0 | sort -z | tr '\0' '\n'"

alias ls='ls -FGlAhp --color=auto'
alias mkdir='mkdir -pv'
alias mv='mv -iv'
alias nano="nano --linenumbers"
alias rm='rm -rf'

if ! [ -x "$(command -v cd_)" ]; then

# Silent cd with no list directory
cd_() { builtin cd "$@" || exit; }

# Always list directory contents upon 'cd'
cd() {
builtin cd "$@" || exit
ls
}
fi
23 changes: 23 additions & 0 deletions .bash/incl/_bash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
#
# GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin21.6.0)
#
#====================================================

_jvcl_::set_homebrew_bash() {
local _bash="${HOMEBREW_PREFIX}/bin/bash"
if ! grep -F -q "${_bash}" "/etc/shells"; then
echo "${HOMEBREW_PREFIX}/bin/bash" | sudo tee -a "/etc/shells" >/dev/null
fi
chsh -s "${_bash}"
}

if _jvcl_::is_formula_installed bash _jvcl_::set_homebrew_bash && [ $((${BASH_VERSION:0:1})) -ge 5 ]; then
echo "Bash ${BASH_VERSION}" &>/dev/null # NOTE: no version display at this step
fi
81 changes: 81 additions & 0 deletions .bash/incl/_colors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
#
# shellcheck disable=SC2034
#
# black='\E[30;40m'
# red='\E[31;40m'
# green='\E[32;40m'
# yellow='\E[33;40m'
# blue='\E[34;40m'
# magenta='\E[35;40m'
# cyan='\E[36;40m'
# white='\E[37;40m'
#
#====================================================

declare -A UCLD_COLORS=(
[_reset]=$'\e[0m'
[blue]=$'\e[1;34m'
[cyan]=$'\e[1;36m'
[green]=$'\e[1;32m'
[magenta]=$'\e[1;35m'
[red]=$'\e[1;31m'
[white]=$'\e[1;37m'
[yellow]=$'\e[1;33m'
)

_jvcl_::alert() {
local _message=${1:-"Error"} _color=${2:-"magenta"}

echo -e "${UCLD_COLORS["${_color}"]}${1}${UCLD_COLORS[_reset]} 馃洃"
echo
}

# read

_jvcl_::ask() {
local _prompt=${1:-"Houston Do You Copy"} _color=${2:-"cyan"}

read -e -r -p "${UCLD_COLORS["${_color}"]}${_prompt}? [y/N]${UCLD_COLORS["_reset"]} " -n 1
if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
true
else
false
fi
}

_jvcl_::ask_2() {
_jvcl_::ask "$1" "green"
}

# h1, H2, h3...

_jvcl_::h1() {
local _message _color

_message=${1:-"Title h1"}
_color=${2:-"magenta"}

_message="$(echo -e "${UCLD_COLORS["${_color}"]}${_message}${UCLD_COLORS["_reset"]}")"

cat <<EOF
${_message}
EOF
}

_jvcl_::h2() {
_jvcl_::h1 "${1}..." "green"
}

_jvcl_::h3() {
_jvcl_::h1 "${1}..." "blue"
}
56 changes: 56 additions & 0 deletions .bash/incl/_debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
#====================================================

_jvcl_::debug() {
if [[ "${DEBUG}" -eq 0 ]]; then
return
fi
cat <<EOF
===============
DEBUG LEVEL ${DEBUG}
===============
EOF

cat /proc/version 2>/dev/null || :
cat /etc/issue 2>/dev/null || :
_jvcl_::set_show_options
python --version || :

if [[ "${DEBUG}" -gt 1 ]]; then

if [[ "${DEBUG}" -gt 2 ]]; then

echo "$(
set -o posix
set | sort
)"

if [[ "${DEBUG}" -gt 3 ]]; then
# exec 2>>.bash/logfile.log
exec {BASH_XTRACEFD}>>.bash/logfile.log
set -x
fi

else

echo
env
echo

fi

echo
alias
echo
fi

}
84 changes: 84 additions & 0 deletions .bash/incl/_homebrew.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
#====================================================

# Set PATH, MANPATH, etc., for Homebrew for M1
case $(arch) in
arm64) # M1
eval "$(/opt/homebrew/bin/brew shellenv)"

;;
i386) # Intel
# Warning: Homebrew's "sbin" was not found in your PATH but you have installed
# formulae that put executables in /usr/local/sbin.
# Consider setting your PATH for example like so:
export PATH="/usr/local/sbin:$PATH"
export HOMEBREW_PREFIX="/usr/local"
;;
esac

_jvcl_::brew_install_formula() {
if _jvcl_::is_homebrew_installed; then
_jvcl_::h1 "Checking if ${1} is installed..."
brew ls --versions "${1}" || brew install "${1}"
fi
}

_jvcl_::is_formula_installed() {
local _formula="${1}" _type

_type="${_formula}"

if [[ "${_formula}" == "postgresql@"* ]]; then _type="psql"; fi
if [[ "${_formula}" == "python@"* ]]; then _type="python"; fi
if [[ "${_formula}" == "visual-studio-code"* ]]; then _type="code"; fi

if type "${_type}" &>/dev/null; then
true
elif _jvcl_::ask "Do you want to install ${_formula}"; then
brew install "${_formula}"
# "${2:-}" # extra func passed as an argument e.g.: _jvcl_::is_formula_installed bash _jvcl_::set_homebrew_bash
if [ -n "${2:-}" ]; then
"${2}"
fi
true
else
false
fi
}

_jvcl_::is_homebrew_installed() {
if type brew &>/dev/null; then
true
elif _jvcl_::ask "Do you want to install Homebrew"; then
_jvcl_::h3 "Installing Homebrew"
curl -fsSL "https://raw.githubusercontent.com/Homebrew/install/master/install.sh"
true
else
false
fi
}

_jvcl_::update_mas() {
mas list
mas upgrade
}

_jvcl_::update_homebrew() {
local _opt
for _opt in "config" "doctor" "update" "upgrade" "autoremove" "cleanup"; do
brew "${_opt}" --verbose || :
done

# You can dump a Brewfile of your current brew/cask/mas entries into your current directory with
# https://gist.github.com/ChristopherA/a579274536aab36ea9966f301ff14f3f#creating-a-brewfile
brew bundle dump --force --file="${HOME}/Brewfile"

# Update pip
python3 -m pip install --upgrade pip
}
15 changes: 15 additions & 0 deletions .bash/incl/_poetry_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
#
# Helper script for installing poetry on pipeline
#
#====================================================

curl -sSL https://install.python-poetry.org | python3 -

export PATH="/root/.local/bin:${PATH}"
19 changes: 19 additions & 0 deletions .bash/incl/_python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
#====================================================

alias python=python3

if _jvcl_::is_formula_installed "python@${REPO_PARAM[python]}"; then
export PATH="${HOMEBREW_PREFIX}/opt/python@${REPO_PARAM[python]}/libexec/bin:$PATH"
export PYTHONPATH="${HOMEBREW_PREFIX}/bin/python${REPO_PARAM[python]}"
export PATH="${PYTHONPATH}/bin:$PATH"
export LDFLAGS="-L${PYTHONPATH}/lib"
export PKG_CONFIG_PATH="${PYTHONPATH}/lib/pkgconfig"
# python --version
fi
55 changes: 55 additions & 0 deletions .bash/incl/_set.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# author : JV-conseil
# credits : JV-conseil
# copyright : Copyright (c) 2019-2023 JV-conseil
# All rights reserved
#
# settings to write safe scripts
# <https://sipb.mit.edu/doc/safe-shell/>
#
# The Set Builtin allows you to change the values of shell options
# <https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html>
#
# Shopt builtin allows you to change additional shell optional behavior
# <https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html>
#
# The Unofficial Bash Strict Mode
# These lines deliberately cause your script to fail.
# Wait, what? Believe me, this is a good thing.
# <http://redsymbol.net/articles/unofficial-bash-strict-mode/>
#
# Safer bash scripts with 'set -euxo pipefail'
# <https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/>
#
#====================================================

_jvcl_::set_show_options() {
bash --version || :
cat <<EOF
Bash $(if [[ "${-}" =~ [eu] ]]; then echo "Strict Mode activated"; else echo "Options"; fi) set ${-}
$(echo ${SHELLOPTS} | tr ':' '\n')
$(shopt -s)
EOF
}

_jvcl_::set_terminal_mode() {
set +eu
set -E -o pipefail
shopt -s failglob
IFS=$'\n\t'
}

_jvcl_::set_strict_mode() {
_jvcl_::set_terminal_mode
set -eu
}

_jvcl_::set_terminal_mode
if [ "${BASH_STRICT_MODE}" -gt 0 ]; then
_jvcl_::set_strict_mode
fi
Loading

0 comments on commit 49e30dd

Please sign in to comment.