2
0
Fork 0
rootfs/usr/local/libexec/zio/helpers/bash.sh
2024-04-01 23:15:12 +01:00

157 lines
2.7 KiB
Bash

#!/usr/bin/env bash
function container_exec() {
podman_exec $@
}
function create_log() {
content="$1"
log_dir="/var/log/zio"
log_file="$(date +%Y%m%d%H%M%S).$(echo $RANDOM | md5sum | head -c 6; echo;).log"
if [[ ! $(id -u) = 0 ]]; then
log_dir="/tmp/zio/logs"
fi
log_path="$log_dir/$log_file"
mkdir -p "$log_dir"
echo -e "$content" > "$log_dir/$log_file"
echo "$log_path"
}
function create_tmp_file() {
name="$1"
prefix_dir="/tmp/zio"
tmp_path="$prefix_dir/$(date +%s).$name"
mkdir -p "$prefix_dir"
touch "$tmp_path"
echo "$tmp_path"
}
function die() {
say error "$@"
exit 255
}
function get_config_dir() {
prog="$(echo $1 | sed -e "s/sh.zio.//g")"
prefix_dir="/etc/zio"
if [[ -n "$2" ]]; then
prefix_dir="$2/zio"
fi
config_dir=""
if [[ ! -z $prog ]]; then
config_dir="$prefix_dir/$prog"
else
config_dir="$prefix_dir"
fi
if [[ ! -d "$config_dir" ]]; then
mkdir -p "$config_dir"
fi
echo "$config_dir"
}
function get_real_path() {
echo "$(cd -P "$1" && pwd)"
}
function podman_exec() {
container="$1"
command="${@:2}"
shell="/bin/sh" # TODO: Programatically get this
podman exec -it $container $shell -c "$command"
}
function repeat() {
string="$1"
amount=$2
if ! [[ -n $amount ]]; then
amount=20
fi
eval "for i in {1..$amount}; do echo -n "$1"; done"
}
function say() {
color=""
message="${@:2}"
output=""
prefix=""
style="0"
if [[ "$2" != "" ]]; then
message="$2"
type="$1"
else
message="$1"
fi
case $1 in
debug)
color="35"
prefix="Debug"
;;
error)
color="31"
prefix="Error"
style="1"
;;
info)
color="34"
style="1"
;;
primary)
color="37"
style="1"
;;
warning)
color="33"
style="1"
;;
*|default)
color="0"
message="$@"
;;
esac
if [[ $prefix == "" ]]; then
output="\033[${style};${color}m${message}\033[0m"
else
output="\033[1;${color}m${prefix}"
if [[ $message == "" ]]; then
output+="!"
else
output+=": \033[${style};${color}m${message}\033[0m"
fi
output+="\033[0m"
fi
echo -e "$output"
}
function test_file() {
if [[ ! -f "$1" ]]; then
die "'$1' does not exist"
fi
}
function test_root() {
if [[ ! $(id -u) = 0 ]]; then
die "Unauthorized (are you root?)"
fi
}
function test_prog() {
[[ ! $(command -v "$1") ]] && die "'$1' not installed"
}