Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to output JSON #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
53 changes: 50 additions & 3 deletions tdx-info
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
Expand Down Expand Up @@ -253,6 +292,11 @@ check_root_user
distro_detect
devicetree_detect
bootloader_detect
if [ ! -z "$JSON_OUTPUT" ] ; then
echo "{"
fi



if [ $# -ne 0 ]; then

Expand Down Expand Up @@ -302,3 +346,6 @@ else
software_summary
hardware_info
fi
if [ ! -z "$JSON_OUTPUT" ] ; then
printf "\n}\n"
fi