From 451e990746fb355f43fc467e7a85255e6e3666c3 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Fri, 1 Nov 2024 12:05:23 +0100 Subject: [PATCH] new: [compose-core] add ``--raw|-r`` and ``-0`` raw output and NUL-separated fields support --- bin/compose-core | 118 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 81 insertions(+), 37 deletions(-) diff --git a/bin/compose-core b/bin/compose-core index 872a73f..fdc67ff 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -5313,6 +5313,12 @@ Options: -f, --filter Filter services by a key=value pair, separated by commas or can be repeated. (default: --filter root=yes) + + -r, --raw Raw data output (no colors nor alignement) + + -0 Separate field with NUL char. Implies raw + data output. + " while read-0 arg; do case "$arg" in @@ -5320,6 +5326,19 @@ Options: echo "$help" exit 0 ;; + --raw|-r|-0) + state_raw_output="$arg"; + ## check if any state_columns have alignements specs + for col in "${state_columns[@]}"; do + if [[ "$col" == [-+]* ]]; then + err "Cannot use $arg and provide columns with alignment specs." + exit 1 + fi + done + if [[ "$arg" == "-0" ]]; then + state_raw_output_nul=1 + fi + ;; --all|-a) if [ "${#state_services[@]}" -gt 0 ]; then err "Cannot use --all and provide services at the same time." @@ -5334,10 +5353,19 @@ Options: --column|-c) read-0 value if [[ "$value" == *,* ]]; then - state_columns+=(${value//,/ }) + state_columns_candidate=(${value//,/ }) else - state_columns+=("$value") + state_columns_candidate=("$value") + fi + if [[ -n "$state_raw_output" ]]; then + for col in "${state_columns_candidate[@]}"; do + if [[ "$col" == [-+]* ]]; then + err "Cannot use ${state_raw_output} and provide columns with alignment specs." + exit 1 + fi + done fi + state_columns+=("${state_columns_candidate[@]}") ;; --filter|-f) if [ "${#state_services[@]}" -gt 0 ]; then @@ -5705,38 +5733,40 @@ if [ "$action" == "status" ]; then for col in "${state_columns_raw[@]}"; do color= value="${!col}" - read -r -- value_trim <<<"${!col}" - case "${col//_/-}" in - root) - case "$value_trim" in - 0) value=" ";; - 1) value="*";; - esac - ;; - name) color=darkyellow;; - charm) color=darkpink;; - state) - case "$value_trim" in - up) color=green;; - down) color=gray;; - deploying) color=yellow;; - *) color=red;; - esac - ;; - type) - case "$value_trim" in - run-once) color=gray;; - stub) color=gray;; - *) color=darkcyan;; - esac - ;; - *) - if [[ "${value_trim}" == "N/A" ]]; then - color=gray - fi - ;; - esac - color="${color^^}" + if [ -z "$state_raw_output" ]; then + read -r -- value_trim <<<"${!col}" + case "${col//_/-}" in + root) + case "$value_trim" in + 0) value=" ";; + 1) value="*";; + esac + ;; + name) color=darkyellow;; + charm) color=darkpink;; + state) + case "$value_trim" in + up) color=green;; + down) color=gray;; + deploying) color=yellow;; + *) color=red;; + esac + ;; + type) + case "$value_trim" in + run-once) color=gray;; + stub) color=gray;; + *) color=darkcyan;; + esac + ;; + *) + if [[ "${value_trim}" == "N/A" ]]; then + color=gray + fi + ;; + esac + color="${color^^}" + fi if [ -n "$color" ]; then values+=("${!color}$value${NORMAL}") else @@ -5748,11 +5778,19 @@ if [ "$action" == "status" ]; then if [ -n "$first" ]; then first= else - printf " " + if [ -n "$state_raw_output_nul" ]; then + printf "\0" + else + printf " " + fi fi printf "%s" "$value" done - printf "\n" + if [ -n "$state_raw_output_nul" ]; then + printf "\0" + else + printf "\n" + fi done < <( set -o pipefail filter_cols=() @@ -5813,7 +5851,13 @@ if [ "$action" == "status" ]; then for col in "${state_columns_raw[@]}"; do p0 "${values[$col]}" done - done | col-0:normalize:size "${state_columns_align}" + done | { + if [ -z "$state_raw_output" ]; then + col-0:normalize:size "${state_columns_align}" + else + cat + fi + } echo 0 ) if [ "$E" != 0 ]; then