From 003754e21a60ff94b42bd8e222048b92fbc92504 Mon Sep 17 00:00:00 2001 From: Jon Oster Date: Tue, 27 Jun 2023 14:02:08 +0200 Subject: [PATCH 1/4] If $JSON_OUTPUT env var is set and non-empty, output JSON Signed-off-by: Jon Oster --- tdx-info | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/tdx-info b/tdx-info index 2e9b96c..d088dfb 100755 --- a/tdx-info +++ b/tdx-info @@ -9,21 +9,35 @@ TABULATION_WIDTH=25 #### Functions #### +slugify () +{ + echo "$1" | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr A-Z a-z +} + print_header () { + if [ -z "$1" ] ; then + echo 'Error: "print_info" is missing parameter(s)!' + exit 1 + fi + if [ ! -z "$JSON_OUTPUT" ] ; then + echo " \"$(slugify $1)\": {" + return + fi if [ -n "$1" ]; then echo "" echo "$1" printf "%0.s-" $(seq 1 $HORIZONTAL_LINE_WIDTH) printf "\n" - else - echo 'Error: "print_header" called without a parameter!' - exit 1 fi } print_info () { + if [ ! -z "$JSON_OUTPUT" ] ; then + print_info_json "$1" "$2" + return + fi if [ -z "$1" ] ; then echo 'Error: "print_info" is missing parameter(s)!' exit 1 @@ -39,8 +53,23 @@ print_info () fi } +print_info_json () +{ + if [ -z "$1" ] ; then + echo 'Error: "print_info" is missing parameter(s)!' + exit 1 + fi + # escape \ (\ -> \\) escape " (" -> \") newline -> \n + echo " \"$(slugify "$1")\": \"$(echo "$2" | sed "s/['\\]/\\\&/g" | sed "s/['\"]/\\\&/g" | awk '{printf "%s\\n", $0}')\"," +} + print_footer () { + if [ ! -z "$JSON_OUTPUT" ] ; then + echo " }," + return + fi + printf "%0.s-" $(seq 1 $HORIZONTAL_LINE_WIDTH) printf "\n" } @@ -253,6 +282,11 @@ check_root_user distro_detect devicetree_detect bootloader_detect +if [ ! -z "$JSON_OUTPUT" ] ; then + echo "{" +fi + + if [ $# -ne 0 ]; then @@ -302,3 +336,7 @@ else software_summary hardware_info fi +if [ ! -z "$JSON_OUTPUT" ] ; then + echo "}" +fi + From 3842e326237041d903226bcab854b485f52a724e Mon Sep 17 00:00:00 2001 From: Jon Oster Date: Tue, 27 Jun 2023 14:19:25 +0200 Subject: [PATCH 2/4] Fix the damn trailing commas problem to make valid JSON Signed-off-by: Jon Oster --- tdx-info | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tdx-info b/tdx-info index d088dfb..4da5074 100755 --- a/tdx-info +++ b/tdx-info @@ -21,7 +21,11 @@ print_header () exit 1 fi if [ ! -z "$JSON_OUTPUT" ] ; then + if [ -n "$INSIDE_OBJECT" ] ; then + printf ",\n" + fi echo " \"$(slugify $1)\": {" + INSIDE_OBJECT=1 return fi if [ -n "$1" ]; then @@ -59,14 +63,20 @@ print_info_json () echo 'Error: "print_info" is missing parameter(s)!' exit 1 fi - # escape \ (\ -> \\) escape " (" -> \") newline -> \n - echo " \"$(slugify "$1")\": \"$(echo "$2" | sed "s/['\\]/\\\&/g" | sed "s/['\"]/\\\&/g" | awk '{printf "%s\\n", $0}')\"," + if [ -n "$INSIDE_OBJECT_2" ] ; then + printf ",\n" + fi + INSIDE_OBJECT_2=1 + + # escape \ (\ -> \\) escape " (" -> \") newline -> \n + echo -n " \"$(slugify "$1")\": \"$(echo "$2" | sed "s/['\\]/\\\&/g" | sed "s/['\"]/\\\&/g" | awk '{printf "%s\\n", $0}')\"" } print_footer () { if [ ! -z "$JSON_OUTPUT" ] ; then - echo " }," + printf " }" + unset INSIDE_OBJECT_2 return fi From 0a060ca7d260438e18ec35724776650931d9e829 Mon Sep 17 00:00:00 2001 From: Jon Oster Date: Tue, 27 Jun 2023 14:22:01 +0200 Subject: [PATCH 3/4] Add note about JSON output to docs Signed-off-by: Jon Oster --- README.md | 2 ++ tdx-info | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8a11729..4adc5b8 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,5 @@ Usage: ./tdx-info [OPTION] For older versions of BSP or TorizonCore (versions 5 and older), run the following command to print the information: wget https://raw.githubusercontent.com/toradex/tdx-info/master/tdx-info --output-document=tdx-info && sudo sh ./tdx-info + +If the `JSON_OUTPUT` environment variable is set, the script will output in JSON format. diff --git a/tdx-info b/tdx-info index 4da5074..34d7862 100755 --- a/tdx-info +++ b/tdx-info @@ -347,6 +347,5 @@ else hardware_info fi if [ ! -z "$JSON_OUTPUT" ] ; then - echo "}" + printf "\n}\n" fi - From f639a226caf8fcdd27f275347eb5b30b07fd3c2d Mon Sep 17 00:00:00 2001 From: Jon Oster Date: Tue, 27 Jun 2023 14:53:14 +0200 Subject: [PATCH 4/4] Fix newline substitution Signed-off-by: Jon Oster --- tdx-info | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tdx-info b/tdx-info index 34d7862..bffb772 100755 --- a/tdx-info +++ b/tdx-info @@ -24,7 +24,7 @@ print_header () if [ -n "$INSIDE_OBJECT" ] ; then printf ",\n" fi - echo " \"$(slugify $1)\": {" + echo " \"$(slugify "$1")\": {" INSIDE_OBJECT=1 return fi @@ -68,8 +68,8 @@ print_info_json () fi INSIDE_OBJECT_2=1 - # escape \ (\ -> \\) escape " (" -> \") newline -> \n - echo -n " \"$(slugify "$1")\": \"$(echo "$2" | sed "s/['\\]/\\\&/g" | sed "s/['\"]/\\\&/g" | awk '{printf "%s\\n", $0}')\"" + # \ -> \\) " -> \" newline -> \n + echo -n " \"$(slugify "$1")\": \"$(echo -n "$2" | sed "s/['\\]/\\\&/g" | sed "s/['\"]/\\\&/g" | sed ':a;N;$!ba;s/\n/\\n/g')\"" } print_footer ()