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 2e9b96c..bffb772 100755 --- a/tdx-info +++ b/tdx-info @@ -9,21 +9,39 @@ 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 + if [ -n "$INSIDE_OBJECT" ] ; then + printf ",\n" + fi + echo " \"$(slugify "$1")\": {" + INSIDE_OBJECT=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 +57,29 @@ print_info () fi } +print_info_json () +{ + if [ -z "$1" ] ; then + echo 'Error: "print_info" is missing parameter(s)!' + exit 1 + fi + if [ -n "$INSIDE_OBJECT_2" ] ; then + printf ",\n" + fi + INSIDE_OBJECT_2=1 + + # \ -> \\) " -> \" 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 () { + if [ ! -z "$JSON_OUTPUT" ] ; then + printf " }" + unset INSIDE_OBJECT_2 + return + fi + printf "%0.s-" $(seq 1 $HORIZONTAL_LINE_WIDTH) printf "\n" } @@ -253,6 +292,11 @@ check_root_user distro_detect devicetree_detect bootloader_detect +if [ ! -z "$JSON_OUTPUT" ] ; then + echo "{" +fi + + if [ $# -ne 0 ]; then @@ -302,3 +346,6 @@ else software_summary hardware_info fi +if [ ! -z "$JSON_OUTPUT" ] ; then + printf "\n}\n" +fi