(mOSAIC) Object store -- Guide -- Snippets

Important

This document is a draft!

To easily use the snippets in the manual, one can use the following functions which extract the commands from the text and execute them in the requested order. Before using them one should paste their source into the shell (only once), and then follow the examples. Alternatively you can have the functions in a script as seen in the last section.

1   Usage

After the execution of each snippet its output is displayed with the help of less, thus to move to the next snippet one has to press q, or Ctrl+C to interrupt the execution.

_snippets_execute create:o-0 select:o-0 destroy:o-0

2   Functions

Choose from the following implementations exactly one for each of the functions, and either paste it into the shell or save it into a script (like suggested at the end).

2.1   _manual_source

Getting the manual source from a file

In case you have stored the manual text inside the file /tmp/mosaic-object-store--manual.rst you have to use the following function:

function _manual_source () {
    cat /tmp/mosaic-object-store--manual.rst \
    | tr '\r' '\n' | tr -s '\n'
}

Getting the manual source from the wiki

In case you want to fetch the raw manual text each time from the wiki:

function _manual_source () {
    curl -s -k -L 'https://wiki.volution.ro/ModaClouds/Notes/ObjectStore/Guide?action=raw' \
    | tr '\r' '\n' | tr -s '\n'
}

Getting the manual source from the clipboard

function _manual_source () {
    xclip -o -selection clipboard \
    | tr '\r' '\n' | tr -s '\n'
}

2.2   _manual_snippets_extract

function _manual_snippets_extract () {
    _manual_source \
    | sed -n -r \
            -e ':b' -e '\!^::$!bs' -e 'n' -e 'bb' \
            -e ':s' -e 'n' -e '\!^$!bs' \
            -e ':l' -e '\!^([^ ].*)?$!be' -e 's/^    //' -e 'p' -e 'n' -e 'bl' \
            -e ':e' -e 'h' -e 's/.*/##/' -e 'p' -e 'g' -e 'bb'
}

2.3   _snippet_select

function _snippet_select () {
    _manual_snippets_extract \
    | sed -n -r \
            -e '\!^## '"${1}"'$!!b' -e 'n' \
            -e ':l' -e '\!^##$!b' -e 'p' -e 'n' -e 'bl'
}

2.4   _snippets_execute

function _snippets_execute () {
    for _snippet in "${@}" ; do
        clear
        bash <(
            printf "set -e -E -u -o pipefail -o noclobber -o noglob -o braceexpand -o errtrace || exit 1\n"
            printf "trap 'printf \"[ee] failed: %%s\\n\" \"\${BASH_COMMAND}\" >&2' ERR || exit 1\n"
            printf "trap 'printf \"\\n[xx] %%s\\n\" \"\${BASH_COMMAND}\" >&2' DEBUG\n"
            printf "function _json_format () { json_reformat 2>/dev/null || true ; }\n"
            _snippet_select "${_snippet}"
        ) 2>&1 </dev/null \
        | less -K -R \
        || true
    done
}

3   Script

#!/bin/bash
set -e -E -u -o pipefail -o noclobber -o noglob +o braceexpand || exit 1
trap 'printf "[ee] failed: %s\n" "${BASH_COMMAND}" >&2' ERR || exit 1

function _manual_source () {
    ...
}
function _manual_snippets_extract () {
    ...
}
function _snippet_select () {
    ...
}
function _snippets_execute () {
    ...
}

_snippets_execute "${@}"

exit 0