APP_NAME=monujo SOURCE_URL="https://docker.0k.io/downloads" LOCATION="$SERVICE_CONFIGSTORE/opt/apps/$APP_NAME" CONFIGFILE="$LOCATION/config.json" version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } monujo:get_source_url() { local app_name="$1" version="$2" if version_gt "$version" 1.0.0-rc.7; then SOURCE_URL="https://github.com/Lokavaluto/monujo/releases/download" echo "$SOURCE_URL/${version}/${app_name}_${version}.tar.bz2" return 0 fi SOURCE_URL="https://docker.0k.io/downloads" echo "$SOURCE_URL/${app_name}-${version}.tar.bz2" return 0 } monujo:code_init() { local version="$1" enforce_version="$2" source_url if [ -e "$LOCATION/.version" ] && \ [ "$(cat "$LOCATION/.version")" == "$version" ]; then return 0 fi if [[ "${enforce_version,,}" =~ ^(false|0|no)$ ]]; then if [ -d "$LOCATION" ]; then return 0 fi fi if [ -d "$LOCATION" ]; then find "$LOCATION" -mindepth 1 -delete else mkdir -p "$LOCATION" fi cd "$LOCATION" source_url="$(monujo:get_source_url "$APP_NAME" "$version")" info "Downloading '$source_url'." wget -q "$source_url" -O file.tar.bz2 || { err "Couldn't download '$source_url'." rm file.tar.bz2 return 1 } tar xjf file.tar.bz2 && rm file.tar.bz2 && chown root:root "$LOCATION" -R && echo "$version" > "$LOCATION/.version" } export MONUJO_OPTIONS=( lokapi-host:string lokapi-db:string map-url:string local-password-retention-time:numeric theme:struct\* locales:struct version:ignore enforce-version:ignore enforce-config:ignore css:string available-languages:struct\* app-strings-language:string default-language:string prefer-navigator-language:bool ) export MONUJO_OPTIONS_CONCAT=" ${MONUJO_OPTIONS[*]} " monujo:code_config_base() { local enforce_config="$1" service_def if [[ "${enforce_config,,}" =~ ^(false|0|no)$ ]]; then if [ -e "$CONFIGFILE" ]; then return 0 fi fi service_def=$(get_compose_service_def "$SERVICE_NAME") || return 1 options=$(e "$service_def" | shyaml get-value -y options) || true e "$options" | monujo:json-make > "$CONFIGFILE" || { err "Failed to make 'config.json'." return 1 } } monujo:json-make() { local conv="$1" key val ## XXXvlab: Should probably offer some lib to do this local sep= while read-0 key val; do key=$(e "$key" | shyaml get-value) ytype=$(e "$val" | shyaml get-type) case "$MONUJO_OPTIONS_CONCAT" in *" ${key}:ignore "*) continue ;; *" ${key}:bool "*) val=$(e "$val" | shyaml get-value) case "${val,,}" in true|ok|yes|y) val=true ;; false|ko|nok|no|n) val=false ;; *) die "Invalid value for ${WHITE}$key$NORMAL, please use a boolean value." ;; esac ;; *" ${key}:numeric "*) val=$(e "$val" | shyaml get-value) if ! is_int "$val"; then err "Invalid value for ${WHITE}$key$NORMAL, please use numeric value." return 1 fi ;; *" ${key}:struct* "*) val=$(e "$val" | monujo:json-make noconv) || return 1 ;; *" ${key}:struct "*) val=$(e "$val" | monujo:json-make) || return 1 ;; *" ${key}:string "*|*) : ;; esac case "$ytype" in struct|sequence) val=$(e "$val" | monujo:json-make noconv) || return 1 ;; bool) val=$(e "$val" | shyaml get-value) ## shyaml outputs actually python's True/False, ## json need the lowercase version. val=${val,,} ;; str) val=$(e "$val" | shyaml get-value | jq -Rr tojson) ;; *) echo "YTYPE: $ytype" >&2 echo "VAL: $val" >&2 val=$(e "$val" | shyaml get-value | jq -r tojson) ;; esac if [ -z "$conv" ]; then key=$(echo "${key//-/_}" | sed 's/_\([a-z0-9]\)/\U\1/g') fi printf "$sep%s\0%s" "$key" "$val" sep="\0\0" done < <(shyaml key-values-0 -y) | jq -sR 'split("\u0000\u0000") | map(split("\u0000") | {key: .[0], value: .[1] | fromjson}) | from_entries' } monujo:config_merge() { local old_config new_config="$1" old_config=$(cat "$CONFIGFILE") e "$old_config" "$new_config" | jq -s 'reduce .[] as $x ({}; . * $x)' > "$CONFIGFILE" }