6 changed files with 172 additions and 109 deletions
-
2README.md
-
42install.sh
-
113lib/functions.bash
-
50lib/functions.sh
-
72tools/idem_apt_install
-
2tools/wget_semver
@ -0,0 +1,113 @@ |
|||
## library of useful functions, usually prefixed with '_' |
|||
|
|||
## =============== |
|||
## ARRAY Functions |
|||
## =============== |
|||
|
|||
function _array_contains { |
|||
local -n array=$1 |
|||
local element=$2 |
|||
[[ " ${array[@]} " =~ " ${element} " ]] && return 0 || return 1 |
|||
} |
|||
|
|||
function _array_intersect { |
|||
local -n arr1=$1 |
|||
local -n arr2=$2 |
|||
local -n result=$3 |
|||
|
|||
declare -A include |
|||
for item in "${arr2[@]}"; do include["$item"]=1; done |
|||
|
|||
result=() |
|||
for item in "${arr1[@]}"; do [[ -n ${include["$item"]} ]] && result+=("$item"); done |
|||
} |
|||
|
|||
function _array_subtract { |
|||
local -n arr1=$1 # First array |
|||
local -n arr2=$2 # Array to subtract |
|||
local -n result=$3 # Output array |
|||
|
|||
[[ ${#arr2[@]} == 0 ]] && result=("${arr1[@]}") && return |
|||
|
|||
declare -A exclude |
|||
for item in "${arr2[@]}"; do exclude["$item"]=1; done |
|||
|
|||
result=() |
|||
for item in "${arr1[@]}"; do [[ -z ${exclude["$item"]} ]] && result+=("$item"); done |
|||
} |
|||
|
|||
## ============== |
|||
## TEXT Functions |
|||
## ============== |
|||
|
|||
function _pluralize_simple { |
|||
echo -n "$1 " |
|||
[[ $1 -eq 1 || $1 -eq -1 ]] && echo ${2} || echo ${2}s |
|||
} |
|||
|
|||
## ============== |
|||
## READ Functions |
|||
## ============== |
|||
|
|||
function _confirm_destructive { |
|||
local message="${1:-This cannot be undone!}" |
|||
echo "⚠️ WARNING: $message" |
|||
read -p "Type YES to confirm: " response |
|||
case "$response" in |
|||
[yY][eE][sS] | [yY]) return 0 ;; |
|||
*) echo "canceled!" && return 1 ;; |
|||
esac |
|||
} |
|||
|
|||
## ============== |
|||
## Deprecated: FIXME: to remove |
|||
## ============== |
|||
|
|||
## |
|||
# return 0 (true) if array (passed by name) contains element |
|||
# usage: |
|||
# ARRAY=( a b c ) |
|||
# containsElement ARRAY 'a' => 0 |
|||
function containsElement { |
|||
isArray "$1" || (echo >&2 "ERROR: <$1> not an array!" && return 2) |
|||
array_inter="$1[@]" |
|||
array=("${!array_inter}") |
|||
for i in "${array[@]}"; do [[ "$i" == "$2" ]] && return 0; done |
|||
return 1 |
|||
} |
|||
|
|||
## return 0 (true) if arg1 (passed by name) is an array |
|||
function isArray { |
|||
[[ "$(declare -p "$1" 2>/dev/null)" =~ "declare -a" ]] || return 1 |
|||
} |
|||
|
|||
function isDebian { |
|||
grep ^ID=debian /etc/os-release |
|||
} |
|||
|
|||
function isArch { |
|||
grep ^ID=arch /etc/os-release |
|||
} |
|||
|
|||
function os-release { |
|||
grep ^ID= /etc/os-release | cut -d '=' -f2 |
|||
} |
|||
|
|||
function askConfirmation { |
|||
case "$1" in |
|||
y | Y | yes | YES) |
|||
QUESTION="(Y/n)?" |
|||
DEFAULT=0 |
|||
;; |
|||
*) |
|||
QUESTION="(y/N)?" |
|||
DEFAULT=1 |
|||
;; |
|||
esac |
|||
read -rp "$QUESTION : " choice |
|||
case "$choice" in |
|||
y | Y | yes | YES) return 0 ;; #true |
|||
n | no | N | NO) return 1 ;; #false |
|||
*) return $DEFAULT ;; |
|||
esac |
|||
} |
|||
@ -1,50 +0,0 @@ |
|||
#!/bin/bash |
|||
|
|||
## |
|||
# return 0 (true) if array (passed by name) contains element |
|||
# usage: |
|||
# ARRAY=( a b c ) |
|||
# containsElement ARRAY 'a' => 0 |
|||
function containsElement { |
|||
isArray "$1" || (echo >&2 "ERROR: <$1> not an array!" && return 2) |
|||
array_inter="$1[@]" |
|||
array=("${!array_inter}") |
|||
for i in "${array[@]}"; do [[ "$i" == "$2" ]] && return 0; done |
|||
return 1 |
|||
} |
|||
|
|||
## return 0 (true) if arg1 (passed by name) is an array |
|||
function isArray { |
|||
[[ "$(declare -p "$1" 2>/dev/null)" =~ "declare -a" ]] || return 1 |
|||
} |
|||
|
|||
function isDebian { |
|||
grep ^ID=debian /etc/os-release |
|||
} |
|||
|
|||
function isArch { |
|||
grep ^ID=arch /etc/os-release |
|||
} |
|||
|
|||
function os-release { |
|||
grep ^ID= /etc/os-release | cut -d '=' -f2 |
|||
} |
|||
|
|||
function askConfirmation { |
|||
case "$1" in |
|||
y | Y | yes | YES) |
|||
QUESTION="(Y/n)?" |
|||
DEFAULT=0 |
|||
;; |
|||
*) |
|||
QUESTION="(y/N)?" |
|||
DEFAULT=1 |
|||
;; |
|||
esac |
|||
read -rp "$QUESTION : " choice |
|||
case "$choice" in |
|||
y | Y | yes | YES) return 0 ;; #true |
|||
n | no | N | NO) return 1 ;; #false |
|||
*) return $DEFAULT ;; |
|||
esac |
|||
} |
|||
@ -1,53 +1,53 @@ |
|||
#!/bin/bash |
|||
|
|||
function usage { |
|||
local BASECMD |
|||
BASECMD=$(basename "$0") |
|||
echo "usage: $BASECMD <packages...>" |
|||
case $(os-release) in |
|||
debian) |
|||
echo 'idempotent debian package installation : update if necessary, install only if not yet done' |
|||
;; |
|||
arch) |
|||
echo 'idempotent archlinux package installation : update if necessary, install only if not yet done' |
|||
;; |
|||
esac |
|||
local BASECMD |
|||
BASECMD=$(basename "$0") |
|||
echo "usage: $BASECMD <packages...>" |
|||
case $(os-release) in |
|||
debian) |
|||
echo 'idempotent debian package installation : update if necessary, install only if not yet done' |
|||
;; |
|||
arch) |
|||
echo 'idempotent archlinux package installation : update if necessary, install only if not yet done' |
|||
;; |
|||
esac |
|||
|
|||
exit 1 |
|||
exit 1 |
|||
} |
|||
|
|||
BASEDIR=$(dirname "$0") |
|||
source "$BASEDIR"/../lib/functions.sh |
|||
source "$BASEDIR"/../lib/functions.bash |
|||
|
|||
[ "$(id -u)" -ne 0 ] && echo 'root privilege required' && exit 2 |
|||
[[ $# -lt 1 ]] && usage |
|||
|
|||
case $(os-release) in |
|||
debian | ubuntu | linuxmint) |
|||
if [ "$(date --date='-12 hours' +%s)" -gt "$(date -d "$(stat -c %y /var/lib/apt/lists/partial)" +%s)" ]; then |
|||
echo "updating repositoring..." |
|||
apt-get update |
|||
fi |
|||
if [ "$(date --date='-12 hours' +%s)" -gt "$(date -d "$(stat -c %y /var/lib/apt/lists/partial)" +%s)" ]; then |
|||
echo "updating repositoring..." |
|||
apt-get update |
|||
fi |
|||
|
|||
for i in "$@"; do |
|||
if ! dpkg -l "$i" 2>/dev/null | grep -q ^ii; then |
|||
sudo apt-get install -y "$i" |
|||
elif [ -n "${VERBOSE+x}" ] && $VERBOSE; then |
|||
echo "apt package <$i> already installed!" |
|||
fi |
|||
done |
|||
;; |
|||
for i in "$@"; do |
|||
if ! dpkg -l "$i" 2>/dev/null | grep -q ^ii; then |
|||
sudo apt-get install -y "$i" |
|||
elif [ -n "${VERBOSE+x}" ] && $VERBOSE; then |
|||
echo "apt package <$i> already installed!" |
|||
fi |
|||
done |
|||
;; |
|||
arch) |
|||
sudo pacman -Syyu --noconfirm |
|||
for i in "$@"; do |
|||
if ! pacman -Ql "$i" &>/dev/null; then |
|||
sudo pacman -S --noconfirm "$i" |
|||
elif [ -n "${VERBOSE+x}" ] && $VERBOSE; then |
|||
echo "pacman package <$i> already installed!" |
|||
fi |
|||
done |
|||
;; |
|||
sudo pacman -Syyu --noconfirm |
|||
for i in "$@"; do |
|||
if ! pacman -Ql "$i" &>/dev/null; then |
|||
sudo pacman -S --noconfirm "$i" |
|||
elif [ -n "${VERBOSE+x}" ] && $VERBOSE; then |
|||
echo "pacman package <$i> already installed!" |
|||
fi |
|||
done |
|||
;; |
|||
*) |
|||
echo "unknown os release <$(os-release)>!" && exit 2 |
|||
;; |
|||
echo "unknown os release <$(os-release)>!" && exit 2 |
|||
;; |
|||
esac |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue