Next Generation WASM Microkernel Operating System
at main 80 lines 1.7 kB view raw
1#!/usr/bin/env bash 2# utility functions used in other shell scripts. 3# 4# currently, this includes: 5# - cargo-style stderr logging (`err`, `note`, and `status` functions) 6# - confirmation prompts (`confirm` function) 7set -euo pipefail 8 9# Log an error to stderr 10# 11# Args: 12# $1: message to log 13err() { 14 echo -e "\e[31m\e[1merror:\e[0m" "$@" 1>&2; 15} 16 17# Log a note to stderr 18# 19# Args: 20# $1: message to log 21note() { 22 echo -e "\e[31m\e[1mnote:\e[0m" "$@" 1>&2; 23} 24 25# Log a cargo-style status message to stderr 26# 27# Args: 28# $1: a "tag" for the log message (should be 12 characters or less in 29# length) 30# $2: message to log 31status() { 32 local width=12 33 local tag="$1" 34 local msg="$2" 35 printf "\e[32m\e[1m%${width}s\e[0m %s\n" "$tag" "$msg" 36} 37 38# Prompt the user to confirm an action 39# 40# Args: 41# $1: message to display to the user along with the `[y/N]` prompt 42# 43# Returns: 44# 0 if the user confirmed, 1 otherwise 45confirm() { 46 while read -r -p "$1 [Y/n] " input 47 do 48 case "$input" in 49 [yY][eE][sS]|[yY]) 50 return 0 51 ;; 52 [nN][oO]|[nN]) 53 return 1 54 ;; 55 *) 56 err "invalid input $input" 57 ;; 58 esac 59 done 60} 61 62# Returns the path to a Mycelium crate. 63# 64# Args: 65# $1: crate name 66# 67# Returns: 68# 0 if the crate exists, 0 if it does not exist. 69crate_path() { 70 local crate="$1" 71 local mycoprefix='mycelium-'; 72 if [[ -d $crate ]]; then 73 echo "$crate" 74 elif [[ -d "${crate#"$mycoprefix"}" ]]; then 75 echo "${crate#"$mycoprefix"}" 76 else 77 err "unknown crate $crate" 78 return 1; 79 fi 80}