From 20563c57e804aca11d1c53bf482d7bde9774a635 Mon Sep 17 00:00:00 2001 From: Uros Gruber Date: Thu, 8 Feb 2024 23:32:02 +0100 Subject: [PATCH] Add option to format list of pots --- share/pot/common.sh | 25 +++++++++++++++++++++ share/pot/list.sh | 53 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/share/pot/common.sh b/share/pot/common.sh index a3e8fcbd..c82501e8 100644 --- a/share/pot/common.sh +++ b/share/pot/common.sh @@ -1008,6 +1008,17 @@ _print_pot_fscomp() printf "\\t\\t%s => %s\\n" "${_mnt_p##"${POT_FS_ROOT}"/jails/}" "${_dset##"${POT_ZFS_ROOT}"/}" done < "$1" } +# $1 fscomp.conf absolute pathname +_print_pot_fscomp_json() +{ + local _dset _mnt_p _out + while read -r line ; do + _dset=$( echo "$line" | awk '{print $1}' ) + _mnt_p=$( echo "$line" | awk '{print $2}' ) + _out=$(printf %s"{\"d\": \"%s\", \"m\":\"%s\"}," "$_out" "${_mnt_p##"${POT_FS_ROOT}"/jails/}" "${_dset##"${POT_ZFS_ROOT}"/}") + done < "$1" + printf "[%s]" "${_out%?}" +} # $1 pot name _print_pot_snaps() @@ -1021,6 +1032,20 @@ _print_pot_snaps() fi } +# $1 pot name +_print_pot_snaps_json() +{ + local _out + if [ -z "$( zfs list -t snapshot -o name -Hr "${POT_ZFS_ROOT}/jails/$1")" ]; then + printf "[]" + else + for _s in $( zfs list -t snapshot -o name -Hr "${POT_ZFS_ROOT}/jails/$1" | tr '\n' ' ' ) ; do + _out=$(printf "%s\"%s\"," "$_out" "$_s") + done + printf "[%s]" "${_out%?}" + fi +} + #1 pot name _get_pot_snaps() { diff --git a/share/pot/list.sh b/share/pot/list.sh index 0e8e9b9e..6f88dd10 100644 --- a/share/pot/list.sh +++ b/share/pot/list.sh @@ -5,7 +5,7 @@ list-help() { cat <<-"EOH" - pot list [-hpbfFa] [-qv] + pot list [-hpbfFa] [-qvo] -h print this help -v verbose -q quiet @@ -15,6 +15,9 @@ list-help() -F list available flavours -B list available bridges (network type) -a list everything (incompatible with -q) + -o format: output format, one of + text - textual (the default) + json - JSON format EOH } @@ -51,22 +54,44 @@ _ls_info_pot() _ls_pots() { - local _pots _q + local _pots _q _format _i _q=$1 + _format=$2 _pots=$( _get_pot_list ) if [ -z "$_pots" ]; then if [ "$_q" != "quiet" ]; then - echo "No pot created yet..." + if [ "$_format" = "json" ]; then + echo "[]" + else + echo "No pot created yet..." + fi fi return fi - for _p in $_pots; do + if [ "$_format" = "json" ]; then + printf "[" + fi + _i=0 + set -- $_pots + for _p do if [ "$_q" = "quiet" ]; then - echo "$_p" + if [ "$_format" = "json" ]; then + printf "{\"name\": \"%s\"}" "$_p" + else + echo "$_p" + fi else - _ls_info_pot "$_p" + _ls_info_pot "$_p" "$_format" + fi + _i=$(( _i + 1 )) + # if not the last item add , + if [ "$_i" != "$#" -a "$_format" = "json" ]; then + printf "," fi done + if [ "$_format" = "json" ]; then + printf "]\\n" + fi } _ls_bases() @@ -142,11 +167,12 @@ _ls_bridges() pot-list() { - local _obj _q + local _obj _q _format _obj="pots" + _format="text" _q= OPTIND=1 - while getopts "hvbfFapqB" _o ; do + while getopts "hvbfFapqBo:" _o ; do case "$_o" in h) list-help @@ -158,6 +184,15 @@ pot-list() q) _q="quiet" ;; + o) + if [ "$OPTARG" = "text" ] || [ "$OPTARG" = "json" ]; then + _format="$OPTARG" + else + _error "Format $OPTARG not supported" + list-help + ${EXIT} 1 + fi + ;; p) if [ "$_obj" != "pots" ]; then _error "Options -b -p -f -F -B -a are mutually exclusive" @@ -220,7 +255,7 @@ pot-list() fi case $_obj in "pots"|"ppots") - _ls_pots "$_q" + _ls_pots "$_q" "$_format" ;; "bases") _ls_bases "$_q"