diff --git a/share/pot/common.sh b/share/pot/common.sh index a3e8fcb..c82501e 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 0e8e9b9..f886d56 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,58 +15,118 @@ 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 } # $1 pot name _ls_info_pot() { - local _pname _cdir _lvl + local _pname _format _cdir _lvl _active _pname=$1 + _format=$2 _cdir="${POT_FS_ROOT}/jails/$_pname/conf" _lvl=$( _get_conf_var "$_pname" pot.level) - printf "pot name : %s\\n" "$_pname" - printf "\\tnetwork : %s\\n" "$( _get_conf_var "$_pname" network_type)" + if [ "$_format" = "json" ]; then + printf "{" + fi + if [ "$_format" = "json" ]; then + printf "\"name\":\"%s\"," "$_pname" + printf "\"network\":\"%s\"," "$( _get_conf_var "$_pname" network_type)" + else + printf "pot name : %s\\n" "$_pname" + printf "\\tnetwork : %s\\n" "$( _get_conf_var "$_pname" network_type)" + fi + if [ "$( _get_conf_var "$_pname" network_type)" != "inherit" ]; then - printf "\\tip : %s\\n" "$( _get_ip_var "$_pname" )" + if [ "$_format" = "json" ]; then + printf "\"ip\":\"%s\"," "$( _get_ip_var "$_pname" )" + else + printf "\\tip : %s\\n" "$( _get_ip_var "$_pname" )" + fi fi if _is_pot_running "$_pname" ; then - printf "\\tactive : true\\n" + _active=true else - printf "\\tactive : false\\n" + _active=false fi - if _is_verbose ; then - printf "\\tbase : %s\\n" "$( _get_conf_var "$_pname" pot.base)" - printf "\\tlevel : %s\\n" "$_lvl" + if [ "$_format" = "json" ]; then + printf "\"active\":\"%s\"" "$_active" + else + printf "\\tactive : %s\\n" "$_active" + fi + if _is_verbose ; then + if [ "$_format" = "json" ]; then + printf ",\"base\":\"%s\"," "$( _get_conf_var "$_pname" pot.base)" + printf "\"level\":\"%s\"," "$_lvl" + else + printf "\\tbase : %s\\n" "$( _get_conf_var "$_pname" pot.base)" + printf "\\tlevel : %s\\n" "$_lvl" + fi if [ "$_lvl" -eq 2 ]; then printf "\\tbase pot : %s\\n" "$( _get_conf_var "$_pname" pot.potbase)" fi - printf "\\tdatasets:\\n" - _print_pot_fscomp "$_cdir/fscomp.conf" - printf "\\tsnapshot:\\n" - _print_pot_snaps "$_pname" + if [ "$_format" = "json" ]; then + printf "\"datasets\":" + _print_pot_fscomp_json "$_cdir/fscomp.conf" + printf ",\"snapshot\":" + _print_pot_snaps_json "$_pname" + else + printf "\\tdatasets:\\n" + _print_pot_fscomp "$_cdir/fscomp.conf" + printf "\\tsnapshot:\\n" + _print_pot_snaps "$_pname" + fi + fi + if [ "$_format" = "json" ]; then + printf "}" + else + echo fi - echo } _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" != "$#" ] && [ "$_format" = "json" ]; then + printf "," fi done + if [ "$_format" = "json" ]; then + printf "]\\n" + fi } _ls_bases() @@ -142,11 +202,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 +219,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 +290,7 @@ pot-list() fi case $_obj in "pots"|"ppots") - _ls_pots "$_q" + _ls_pots "$_q" "$_format" ;; "bases") _ls_bases "$_q"