Files
lighthouse/scripts/cli.sh
2023-10-19 07:46:55 +08:00

99 lines
2.6 KiB
Bash
Executable File

#! /bin/bash
write_to_file() {
local cmd="$1"
local file="$2"
local program="$3"
# Remove first line of cmd to get rid of commit specific numbers.
cmd=${cmd#*$'\n'}
# We need to add the header and the backticks to create the code block.
printf "#%s\n\n\`\`\`\n%s\n\`\`\`" "$program" "$cmd" > "$file"
}
# Check if a lighthouse binary exists in the current branch.
# -f means check if the file exists, to see all options, type "bash test" in a terminal
maxperf=./target/maxperf/lighthouse
release=./target/release/lighthouse
debug=./target/debug/lighthouse
if [[ -f "$maxperf" ]]; then
CMD="$maxperf"
elif [[ -f "$release" ]]; then
CMD="$release"
elif [[ -f "$debug" ]]; then
CMD="$debug"
else
# No binary exists, build it.
cargo build --locked
CMD="$debug"
fi
# Store all help strings in variables.
general_cli=$($CMD --help)
bn_cli=$($CMD bn --help)
vc_cli=$($CMD vc --help)
am_cli=$($CMD am --help)
general=./general_help.md
bn=./bn_help.md
vc=./vc_help.md
am=./am_help.md
# create .md files
write_to_file "$general_cli" "$general" "Lighthouse General Commands"
write_to_file "$bn_cli" "$bn" "Beacon Node"
write_to_file "$vc_cli" "$vc" "Validator Client"
write_to_file "$am_cli" "$am" "Account Manager"
# create empty array to store variables for exit condition later
exist=()
update=()
for i in general_help bn_help vc_help am_help
do
if [[ -f ./book/src/cli/$i.md ]]; then # first check if .md exists
echo "$i.md exists, continue to check for any changes"
difference=$(diff ./book/src/cli/$i.md $i.md)
case1=false
exist+=($case1)
if [[ -z $difference ]]; then # then check if any changes required
case2=false
update+=($case2)
echo "$i.md is up to date"
else
cp $i.md ./book/src/cli/$i.md
echo "$i has been updated"
case2=true
update+=($case2)
fi
else
echo "$i.md is not found, it will be created now"
cp $i.md ./book/src/cli/$i.md
case1=true
exist+=($case1)
# echo $case1
#exit 1
fi
# use during testing to show exit conditions
#echo "${exist[@]}"
#echo "${update[@]}"
# exit condition, exit when .md does not exist or changes requried
if [[ ${exist[@]} == *"true"* && ${update[@]} == *"true"* ]]; then
echo "exit 1 due to one or more .md file does not exist and changes updated"
exit 1
elif [[ ${exist[@]} == *"true"* ]]; then
echo "exit 1 due to one or more .md file does not exist"
exit 1
elif [[ ${update[@]} == *"true"* ]]; then
echo "exit 1 due to changes updated"
exit 1
else
echo "Task completed, no changes in CLI parameters"
fi
# remove .md files in current directory
rm -f general_help.md bn_help.md vc_help.md am_help.md