style: made functions more readable

This commit is contained in:
BonzaiBrains 2024-11-04 14:02:29 +01:00 committed by GitHub
parent d4c06eda43
commit d172c755e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

42
shemum
View file

@ -32,7 +32,7 @@ IMG_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.img"
# Functions # Functions
# Defines install image source # Defines install image source
def_os_source() { function def_os_source() {
case $OS_NAME in case $OS_NAME in
"openbsd") "openbsd")
INSTALL_IMG="https://cdn.openbsd.org/pub/OpenBSD/7.5/i386/cd75.iso" INSTALL_IMG="https://cdn.openbsd.org/pub/OpenBSD/7.5/i386/cd75.iso"
@ -41,41 +41,41 @@ def_os_source() {
INSTALL_IMG="" INSTALL_IMG=""
;; ;;
*) *)
err_func "INSTALL_IMG for $OS_NAME not found" error "INSTALL_IMG for $OS_NAME not found"
;; ;;
esac esac
} }
check_func() { function check() {
if [ -z $OS_NAME ] if [ -z $OS_NAME ]
err_func "Operating system is empty" error "Operating system is empty"
fi fi
if [ -z $INSTALL_IMG ] if [ -z $INSTALL_IMG ]
info_func "$INSTALL_IMG is not set" info "$INSTALL_IMG is not set"
fi fi
} }
err_func() { function error() {
printf "$@" >&2 printf "$@" >&2
exit 1 exit 1
} }
warn_func() { function warn() {
printf "$@" >&2 printf "$@" >&2
} }
info_func() { function info() {
printf "$@" >&1 printf "$@" >&1
} }
# Virtual machine removal # Virtual machine removal
remove_func() { function remove() {
rm -r $ROOT_PATH/$OS_NAME rm -r $ROOT_PATH/$OS_NAME
} }
# Virtual machine start without a console # Virtual machine start without a console
start_func() { function start() {
qemu-system-x86_64 -boot c \ qemu-system-x86_64 -boot c \
-display none \ -display none \
-drive file=$IMG_PATH,format=raw \ -drive file=$IMG_PATH,format=raw \
@ -83,7 +83,7 @@ start_func() {
} }
# Virtual machine start with a console # Virtual machine start with a console
console_func() { function console() {
qemu-system-x86_64 -boot c \ qemu-system-x86_64 -boot c \
-display curses \ -display curses \
-drive file=$IMG_PATH,format=raw \ -drive file=$IMG_PATH,format=raw \
@ -91,17 +91,17 @@ console_func() {
} }
# Virtual machine initialization with a console # Virtual machine initialization with a console
init_func() { function init() {
def_os_source def_os_source
mkdir -p "$ROOT_PATH/$OS_NAME" mkdir -p "$ROOT_PATH/$OS_NAME"
qemu-img create $IMG_PATH $DISK_SIZE qemu-img create $IMG_PATH $DISK_SIZE
if [ ! -f $ISO_PATH ] && [ ! -z $INSTALL_IMG ]; then if [ ! -f $ISO_PATH ] && [ ! -z $INSTALL_IMG ]; then
info_func "Fetching $OSNAME installer at: $INSTALL_IMG" info "Fetching $OSNAME installer at: $INSTALL_IMG"
curl -o $ISO_PATH $INSTALL_IMG curl -o $ISO_PATH $INSTALL_IMG
elif [ ! -z $INSTALL_IMG ]; then elif [ ! -z $INSTALL_IMG ]; then
info_func "Current INSTALL_IMG for $OS_NAME: $ISO_PATH" info "Current INSTALL_IMG for $OS_NAME: $ISO_PATH"
else else
err_func "Install image path is missing" error "Install image path is missing"
fi fi
qemu-system-x86_64 -boot d \ qemu-system-x86_64 -boot d \
-display curses \ -display curses \
@ -111,7 +111,7 @@ init_func() {
} }
# Display usage # Display usage
usage_func() { function usage() {
printf "Usage: shemum [init] <virtual machine name> <disk size>" printf "Usage: shemum [init] <virtual machine name> <disk size>"
printf " init initialize a virtual machine" printf " init initialize a virtual machine"
printf " start start a QEMU virtual machine without console output" printf " start start a QEMU virtual machine without console output"
@ -122,19 +122,19 @@ usage_func() {
# Options # Options
case $OPTION in case $OPTION in
"init") "init")
init_func init
;; ;;
"console") "console")
console_func console
;; ;;
"start") "start")
start_func start
;; ;;
"remove") "remove")
remove_func remove
;; ;;
*) *)
usage_func usage
esac esac