Skip to content

Commit

Permalink
Add option to format list of pots
Browse files Browse the repository at this point in the history
  • Loading branch information
urosgruber committed Feb 8, 2024
1 parent 9ac5a5f commit 20563c5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
25 changes: 25 additions & 0 deletions share/pot/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
{
Expand Down
53 changes: 44 additions & 9 deletions share/pot/list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
list-help()
{
cat <<-"EOH"
pot list [-hpbfFa] [-qv]
pot list [-hpbfFa] [-qvo]
-h print this help
-v verbose
-q quiet
Expand All @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -220,7 +255,7 @@ pot-list()
fi
case $_obj in
"pots"|"ppots")
_ls_pots "$_q"
_ls_pots "$_q" "$_format"
;;
"bases")
_ls_bases "$_q"
Expand Down

0 comments on commit 20563c5

Please sign in to comment.