Browse Source

flatten_short_options from miaou-bash

main
pvincent 2 weeks ago
parent
commit
182b0e9df3
  1. 8
      README.md
  2. 60
      lib/functions.bash
  3. 20
      lib/miaou.completion
  4. 25
      recipes/wordpress.recipe
  5. 19
      tools/miaou-create
  6. 31
      tools/miaou-destroy
  7. 2
      tools/miaou-recipe

8
README.md

@ -11,9 +11,15 @@ FEATURES
* [x] files or commands from container within * [x] files or commands from container within
* [x] inner command completion from container within * [x] inner command completion from container within
* [x] colored completions (link, directories...) by reusing inputrc bindings * [x] colored completions (link, directories...) by reusing inputrc bindings
* [x] advanced completion for miaou-{start,stop,destroy} according to container state
TODO: TODO:
----- -----
* [ ] miaou-exec refactoring needed!
* [ ] install
* [ ] MIAOU_INCUS_DIR
* [ ] Template erb+bash
* [ ] Tempalte erb+ruby-on-shell
* [ ] TOOLBOX
* [ ] RUBY From Host

60
lib/functions.bash

@ -1,60 +0,0 @@
## 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
}

20
lib/miaou.completion

@ -213,13 +213,29 @@ function _miaou_destroy {
_array_subtract suggestions previous_words COMPREPLY _array_subtract suggestions previous_words COMPREPLY
} }
function _miaou_recipe {
local cur="${COMP_WORDS[COMP_CWORD]}"
# containers...
if [[ $COMP_CWORD == 1 ]]; then
COMPREPLY=($(compgen -W "$(_incus_running_container)" -- "$cur"))
elif [[ $COMP_CWORD == 2 ]]; then
COMPREPLY=($(compgen -f -- "$cur"))
compopt -o filenames
elif [[ $COMP_CWORD == 3 ]]; then
COMPREPLY=($(compgen -W "--" -- "$cur"))
fi
}
# MAIN # MAIN
# shellcheck source=/opt/miaou-incus/lib/functions.bash
source "$MIAOU_INCUS_DIR/lib/functions.bash"
# shellcheck source=/opt/miaou-bash/lib/functions.bash
source "$MIAOU_BASH_DIR/lib/functions.bash"
complete -F _miaou_login "miaou-login" complete -F _miaou_login "miaou-login"
complete -F _miaou_exec "miaou-exec" complete -F _miaou_exec "miaou-exec"
complete -F _miaou_ls "miaou-ls" complete -F _miaou_ls "miaou-ls"
complete -F _miaou_start "miaou-start" complete -F _miaou_start "miaou-start"
complete -F _miaou_stop "miaou-stop" complete -F _miaou_stop "miaou-stop"
complete -F _miaou_destroy "miaou-destroy" complete -F _miaou_destroy "miaou-destroy"
complete -F _miaou_recipe "miaou-recipe"

25
recipes/wordpress.recipe

@ -2,10 +2,9 @@
# CONSTANTS # CONSTANTS
UPLOAD_MAX_SIZE=${UPLOAD_MAX_SIZE-256M} #TODO: allow passing environment variables from `miaou-recipe`
FORCE=false FORCE=false
PHP_VERSION=8.4
UPLOAD_MAX_SIZE=2G
WP_NAME=''
WP_NAME=
# FUNCTIONS # FUNCTIONS
@ -23,18 +22,21 @@ function parse_options {
--force | -f) --force | -f)
FORCE=true FORCE=true
;; ;;
-*)
echo >&2 "ERROR: unknown option <$1>" && usage && exit 3
;;
*) *)
if [[ -z $WP_NAME ]]; then if [[ -z $WP_NAME ]]; then
WP_NAME=$1 WP_NAME=$1
else else
echo >&2 "Unknown option: $1" && usage && exit 2
echo >&2 "ERROR: unknown argument <$1>" && usage && exit 2
fi fi
;; ;;
esac esac
shift 1 # Move to the next argument shift 1 # Move to the next argument
done done
[[ -z $WP_NAME ]] && usage && exit 1 || true
[[ -z $WP_NAME ]] && echo >&2 "ERROR: expecting mandatory argument {WP_NAME}" && usage && exit 2 || true
} }
function install_mariadb { function install_mariadb {
@ -47,11 +49,15 @@ function install_mariadb {
} }
function install_php { function install_php {
local php_service="php$PHP_VERSION-fpm"
local php_version=php --version | head -n1 | cut -d' ' -f2 | grep -o '^[[:digit:]]*\.[[:digit:]]*'
local php_service="php$php_version-fpm"
if $FORCE || ! systemctl is-active "$php_service" --quiet; then if $FORCE || ! systemctl is-active "$php_service" --quiet; then
apt-get install -y php-fpm php-mysql php-curl php-dom php-imagick php-mbstring php-zip php-gd php-intl apt-get install -y php-fpm php-mysql php-curl php-dom php-imagick php-mbstring php-zip php-gd php-intl
sed -i "s/^post_max_size = 8M/post_max_size = $UPLOAD_MAX_SIZE/g" /etc/php/8.4/fpm/php.ini
sed -i "s/^upload_max_filesize = 2M/upload_max_filesize = $UPLOAD_MAX_SIZE/g" /etc/php/8.4/fpm/php.ini
php_version=php --version | head -n1 | cut -d' ' -f2 | grep -o '^[[:digit:]]*\.[[:digit:]]*'
sed -i "s/^post_max_size = 8M/post_max_size = $UPLOAD_MAX_SIZE/g" /etc/php/$php_version/fpm/php.ini
sed -i "s/^upload_max_filesize = 2M/upload_max_filesize = $UPLOAD_MAX_SIZE/g" /etc/php/$php_version/fpm/php.ini
systemctl enable "$php_service" --now systemctl enable "$php_service" --now
echo "$php_service installed successfully!" echo "$php_service installed successfully!"
else else
@ -112,7 +118,10 @@ EOF
# MAIN # MAIN
set -Eue -o pipefail
parse_options "$@" parse_options "$@"
exit 0
install_mariadb install_mariadb
install_php install_php
# install_wordpress # install_wordpress

19
tools/miaou-create

@ -67,7 +67,7 @@ function mount_miaou_bash {
[[ -n $optional_project ]] && optional_project="--project $optional_project" [[ -n $optional_project ]] && optional_project="--project $optional_project"
incus $optional_project config device add $target MIAOU-BASH disk source=$MIAOU_BASH_DIR path=/opt/miaou-bash readonly=true | grep -q 'Device MIAOU-BASH added' incus $optional_project config device add $target MIAOU-BASH disk source=$MIAOU_BASH_DIR path=/opt/miaou-bash readonly=true | grep -q 'Device MIAOU-BASH added'
incus $optional_project config set $target environment.PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/miaou-bash/tools
# incus $optional_project config set $target environment.PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/miaou-bash/tools
} }
function build_sandbox_project { function build_sandbox_project {
@ -127,13 +127,13 @@ function prepare_template_for_quick_creation {
} }
function customize_host { function customize_host {
local target="$1"
local domain=$(incus network get incusbr0 dns.domain)
local target domain
target="$1"
domain=$(incus network get incusbr0 dns.domain)
domain=${domain:-incus} domain=${domain:-incus}
cat << EOF | incus file push --uid 0 --gid 0 --mode 644 --create-dirs - $target/etc/hosts
127.0.1.1 $target.$domain $target
127.0.0.1 localhost
EOF
printf "127.0.1.1\t$target.$domain $target\n127.0.0.1\tlocalhost\n" | incus file push - $target/etc/hosts
echo "$target.$domain" | incus file push - $target/etc/hostname
} }
function assert_not_sandboxing { function assert_not_sandboxing {
@ -147,7 +147,7 @@ function create {
if incus --project "$PROJECT_SANDBOX" info "$TEMPLATE_SANDBOX" | grep '^Status: RUNNING'; then if incus --project "$PROJECT_SANDBOX" info "$TEMPLATE_SANDBOX" | grep '^Status: RUNNING'; then
incus --project "$PROJECT_SANDBOX" stop "$TEMPLATE_SANDBOX" incus --project "$PROJECT_SANDBOX" stop "$TEMPLATE_SANDBOX"
fi fi
incus --project "$PROJECT_SANDBOX" copy --instance-only --refresh "$TEMPLATE_SANDBOX" "$CONTAINER" --target-project $(incus project get-current)
incus --project "$PROJECT_SANDBOX" copy --instance-only "$TEMPLATE_SANDBOX" "$CONTAINER" --target-project $(incus project get-current)
incus file delete "$CONTAINER/etc/machine-id" incus file delete "$CONTAINER/etc/machine-id"
incus config unset "$CONTAINER" volatile.eth0.hwaddr incus config unset "$CONTAINER" volatile.eth0.hwaddr
incus start "$CONTAINER" incus start "$CONTAINER"
@ -156,8 +156,7 @@ function create {
# MAIN # MAIN
set -Eue
set -o pipefail
set -Eueo pipefail
parse_options "$@" parse_options "$@"
assert_not_sandboxing assert_not_sandboxing

31
tools/miaou-destroy

@ -2,7 +2,7 @@
# CONSTANTS # CONSTANTS
MIAOU_INCUS_DIR=${MIAOU_INCUS_DIR:-/opt/miaou-incus}
ARGS=("$@")
BASEDIR=$(dirname "$0") BASEDIR=$(dirname "$0")
CONTAINERS=() CONTAINERS=()
YES=false YES=false
@ -14,21 +14,6 @@ function usage {
echo "$(basename "$0") <CONTAINER_NAME>... [--yes|-y] [--force|-f]" echo "$(basename "$0") <CONTAINER_NAME>... [--yes|-y] [--force|-f]"
} }
function flatten_short_options {
local -n result=$1
shift
result=()
for word in "$@"; do
[[ $word == -- ]] && break
if [[ $word =~ ^-[a-z][a-z] ]]; then
word=${word:1}
for ((i = 0; i < ${#word}; i++)); do result+=("-${word:i:1}"); done
else
result+=("$word")
fi
done
}
function parse_options { function parse_options {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
@ -48,8 +33,7 @@ function parse_options {
CONTAINERS+=("$1") CONTAINERS+=("$1")
;; ;;
esac esac
shift 1 # Move to the next argument
shift
done done
[[ ${#CONTAINERS[@]} == 0 ]] && usage && exit 1 || true [[ ${#CONTAINERS[@]} == 0 ]] && usage && exit 1 || true
@ -72,11 +56,12 @@ function destroy {
# MAIN # MAIN
set -Eue -o pipefail
set -Eueo pipefail
# shellcheck source=/opt/miaou-bash/lib/functions.bash
source "$MIAOU_BASH_DIR/lib/functions.bash"
# shellcheck source=/opt/miaou-incus/lib/functions.bash
source "$MIAOU_INCUS_DIR/lib/functions.bash"
_flatten_short_options ARGS
parse_options "${ARGS[@]}"
flatten_short_options flat "$@"
parse_options "${flat[@]}"
destroy destroy

2
tools/miaou-recipe

@ -69,7 +69,7 @@ function show_success {
# MAIN # MAIN
set -Eue
set -Eueo pipefail
parse_options "$@" parse_options "$@"
recipe recipe
$QUIET || show_success $QUIET || show_success
Loading…
Cancel
Save