list: Support json format when listing all jails

This commit is contained in:
tschettervictor
2025-02-27 17:52:20 -07:00
committed by GitHub
parent 8e4f09cf8b
commit bbc89e36a5

View File

@@ -242,25 +242,35 @@ fi
TARGET=""
# Handle options.
OPT_JSON=0
OPT_ALL=0
while [ "$#" -gt 0 ]; do
case "${1}" in
-h|--help|help)
usage
;;
-a|--all|all)
list_all
exit 0
OPT_ALL=1
shift
;;
-j|--json)
/usr/sbin/jls -N --libxo json
exit 0
OPT_JSON=1
shift
;;
-x|--debug)
enable_debug
shift
shift
;;
-*)
error_exit "Unknown option: \"${1}\""
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
case ${_opt} in
a) OPT_ALL=1 ;;
j) OPT_JSON=1 ;;
x) enable_debug ;;
*) error_exit "Unknown Option: \"${1}\""
esac
done
shift
;;
*)
break
@@ -268,6 +278,15 @@ while [ "$#" -gt 0 ]; do
esac
done
# List json format, otherwise list all jails
if [ "${OPT_ALL}" -eq 1 ] && [ "${OPT_JSON}" -eq 1 ]; then
list_all | awk 'BEGIN {print "["} NR>1 {print "{"JID": "" $1 "", "State": "" $2 "", "IP_Address": "" $3 "", "Hostname": "" $5 "", "Release": "" $6 "", "Path": "" $7 ""},"} END {print "]"}' | sed '$s/,//'
elif [ "${OPT_ALL}" -eq 0 ] && [ "${OPT_JSON}" -eq 1 ]; then
/usr/sbin/jls -N --libxo json
elif [ "${OPT_ALL}" -eq 1 ] && [ "${OPT_JSON}" -eq 0 ]; then
list_all
fi
if [ "$#" -gt 0 ]; then
case "${1}" in
port|ports)