Commit 6d4c8a65 authored by Claude's avatar Claude 🌴

Improve parameters handling

parent ecadb0a7
......@@ -12,3 +12,15 @@ function print_variable {
echo '<<< not define >>>'
fi
}
# param variable is mandatory
function test_P {
local value=
if ! value="$( P "$@" )" ; then
# shellcheck disable=SC2154
echofatal "Value required for '${param}'"
usage >&2
return 100
fi
echo "${value}"
}
......@@ -9,9 +9,9 @@ PROJECT_HOME="$( dirname "$( dirname "$( realpath "${BASH_SOURCE[0]}" )" )" )"
declare -gr PROJECT_HOME
# shellcheck disable=SC1090
source "${PROJECT_HOME}/apis/bash-common-api/bash-common-api.sh" || exit 1
source "${PROJECT_HOME}/apis/bash-common-api/bash-common-api.sh" || exit $?
# shellcheck disable=SC1090
source "${PROJECT_HOME}/bin/api/prepare-raspbian-api.sh" || exit 1
source "${PROJECT_HOME}/bin/api/prepare-raspbian-api.sh" || exit $?
function usage {
cat <<EOF
......@@ -50,9 +50,9 @@ function getLastLibreElecUrl {
function prepare_libreelec_sd {
local -a params=()
local url=
url="$( getLastLibreElecUrl )" || exit 1
local url=
url="$( getLastLibreElecUrl )" || return $?
params+=(--unzip)
params+=(--url)
......@@ -60,7 +60,7 @@ function prepare_libreelec_sd {
echoinfo "URL: '${url}'"
if [ -z "${param_expected_img}" ] ; then
param_expected_img="$( basename "${url%.gz}" )" || exit 1
param_expected_img="$( basename "${url%.gz}" )" || return $?
fi
params+=(--expected-img)
......@@ -80,7 +80,7 @@ function prepare_libreelec_sd {
cat <<EOF >&2
"${PROJECT_HOME}/bin/prepare-raspbian-sd.sh" "${params[@]}"
EOF
"${PROJECT_HOME}/bin/prepare-raspbian-sd.sh" "${params[@]}" || exit 1
"${PROJECT_HOME}/bin/prepare-raspbian-sd.sh" "${params[@]}" || return $?
}
function main {
......@@ -100,15 +100,18 @@ function main {
;;
'--download-to')
param_download_to="$( P "$@" )" && shift
param_download_to="$( test_P "$@" )" || return $?
shift
;;
'--dev-name')
param_dev_name="$( P "$@" )" && shift
;;
param_dev_name="$( test_P "$@" )" || return $?
shift
;;
'--expected-img')
param_expected_img="$( P "$@" )" && shift
param_expected_img="$( test_P "$@" )" || return $?
shift
;;
*)
......@@ -119,8 +122,8 @@ function main {
esac
done
prepare_libreelec_sd
prepare_libreelec_sd || return $?
}
main "$@"
main "$@" || exit $?
......@@ -9,13 +9,15 @@ PROJECT_HOME="$( dirname "$( dirname "$( realpath "${BASH_SOURCE[0]}" )" )" )"
declare -gr PROJECT_HOME
# shellcheck disable=SC1090
source "${PROJECT_HOME}/apis/bash-common-api/bash-common-api.sh" || exit 1
source "${PROJECT_HOME}/apis/bash-common-api/bash-common-api.sh" || exit $?
# shellcheck disable=SC1090
source "${PROJECT_HOME}/bin/api/prepare-raspbian-config-json-api.sh" || exit 1
source "${PROJECT_HOME}/bin/api/prepare-raspbian-api.sh" || exit $?
# shellcheck disable=SC1090
source "${PROJECT_HOME}/bin/api/prepare-raspbian-config-json-api.sh" || exit $?
function usage {
cat <<EOF
Configure prepare-raspbian-sd.
Configure prepare-raspbian-sd, allow to modify '$( prepare_raspbian_get_config_file )'
Usage:
......@@ -63,7 +65,7 @@ function main {
if [ "$#" -eq 0 ] ; then
echofatal 'Parameters missing.'
usage >&2
exit 1
exit 100
fi
while [ "$#" -gt 0 ] ; do
......@@ -86,31 +88,39 @@ function main {
'--get')
test_action && action='--get'
param_name="$( P "$@" )" && shift
param_name="$( test_P "$@" )" || return $?
shift
;;
'--set')
test_action && action='--set'
param_name="$( P "$@" )" && shift
param_value="$( P "$@" )" && shift
param_name="$( test_P "$@" )" || return $?
shift
param_value="$( test_P "$@" )" || return $?
shift
;;
'--set-boolean'|'--set-integer')
test_action && action="${param}"
param_name="$( P "$@" )" && shift
param_value="$( P "$@" )" && shift
param_name="$( test_P "$@" )" || return $?
shift
param_value="$( test_P "$@" )" || return $?
shift
;;
'--set-link')
test_action && action='--set-link'
param_name="$( P "$@" )" && shift
param_value="$( P "$@" )" && shift
param_name="$( test_P "$@" )" || return $?
shift
param_value="$( test_P "$@" )" || return $?
shift
;;
*)
test_action && action='--set'
param_name="${param}"
param_value="$( P "$@" )" && shift
param_value="$( test_P "$@" )" || return $?
shift
;;
esac
done
......@@ -146,9 +156,9 @@ function main {
*)
echofatal "Internal error: action='${action}'"
exit 1
exit 100
;;
esac
}
main "$@"
main "$@" || exit $?
......@@ -9,11 +9,11 @@ PROJECT_HOME="$( dirname "$( dirname "$( realpath "${BASH_SOURCE[0]}" )" )" )"
declare -gr PROJECT_HOME
# shellcheck disable=SC1090
source "${PROJECT_HOME}/apis/bash-common-api/bash-common-api.sh" || exit 1
source "${PROJECT_HOME}/apis/bash-common-api/bash-common-api.sh" || exit $?
# shellcheck disable=SC1090
#source "${PROJECT_HOME}/bin/api/prepare-raspbian-api.sh" || exit 1
source "${PROJECT_HOME}/bin/api/prepare-raspbian-api.sh" || exit $?
# shellcheck disable=SC1090
source "${PROJECT_HOME}/bin/api/prepare-raspbian-config-json-api.sh" || exit 1
source "${PROJECT_HOME}/bin/api/prepare-raspbian-config-json-api.sh" || exit $?
declare -g ZIP_CHECKED='false'
......@@ -36,11 +36,13 @@ Actions
--url IMGURL Allow to set image file URL
Parameters
--arch ARCH Define machine hardware name (optional, but recommended).
If not set, or empty, use "Raspbian" as source,
if set use "Raspberry Pi OS" as source).
ARCH could be an empty string (for Raspbian), 'armhf', 'arm32'
(alias for 'armhf') or 'arm64'
--arch ARCH Define machine hardware name (optional). If not set, or empty, use "Raspbian"
as source, if set use "Raspberry Pi OS" as source). ARCH could be an empty string,
armhf or arm64.
--arch '' : Download last available version of raspbian
--arch armhf: Download last 32 bits version of "Raspberry Pi OS"
--arch arm32: Same as 'armhf'
--arch arm64: Download last 64 bits version of "Raspberry Pi OS"
(Parameter is ignored if --url is used)
--desktop, Same as --full (deprecated)
--expected-img IMG Expected image filename (without path) - if specified when using --url allow
......@@ -85,7 +87,8 @@ EOF
function build_last_version_url_for_raspbian {
local -r os_type="$1"
local lite=
local lite=
if [ "${os_type}" = 'lite' ] ; then
lite='lite_'
fi
......@@ -104,8 +107,8 @@ function build_last_version_url_for_raspbian {
function build_last_version_url_for_raspberry_pi_os {
local -r arch_type="$1"
local -r os_type="$2"
local identifier=
local identifier=
case "${os_type}" in
'lite')
identifier="lite_${arch_type}_latest";
......@@ -128,7 +131,7 @@ function build_last_version_url_for_raspberry_pi_os {
function download_if_needed {
if is_download_needed ; then
download
download || return $?
fi
}
......@@ -147,16 +150,16 @@ function is_download_needed {
}
function download {
wget "${LAST_VERSION_URL}" -O "${EXPECTED_ZIP_FILE}" || exit 1
wget "${LAST_VERSION_URL}" -O "${EXPECTED_ZIP_FILE}" || return $?
if [ -z "${param_url}" ] ; then
wget "${LAST_VERSION_URL}.sha256" -O "${EXPECTED_ZIP_FILE}.sha256" || exit 1
wget "${LAST_VERSION_URL}.sha256" -O "${EXPECTED_ZIP_FILE}.sha256" || return $?
fi
}
function unzip_if_needed {
if is_unzip_needed ; then
pushd "${param_download_image_directory}" >/dev/null || exit 1
pushd "${param_download_image_directory}" >/dev/null || return $?
if [ "${EXPECTED_ZIP_FILE%*.zip}.zip" = "${EXPECTED_ZIP_FILE}" ] ; then
cat <<EOF >&2
......@@ -167,11 +170,11 @@ EOF
cat <<EOF >&2
gzip --decompress --keep "${EXPECTED_ZIP_FILE}"
EOF
gzip --decompress --keep "${EXPECTED_ZIP_FILE}" || exit 1
gzip --decompress --keep "${EXPECTED_ZIP_FILE}" || return $?
else
echowarn "Don't kown how to unzip: '${EXPECTED_ZIP_FILE}'" >&2
fi
popd >/dev/null || exit 1
popd >/dev/null || return $?
fi
}
......@@ -200,18 +203,18 @@ function check_zip_once {
if [ ! -f "${EXPECTED_ZIP_FILE}.sha256" ] ; then
echofatal "Cannot find '${EXPECTED_ZIP_FILE}.sha256' - Cannot trust zip file."
exit 1
return 100
fi
echoinfo "Verifying checksum of '${EXPECTED_ZIP_FILE}'" >&2
local check256=
check256="$(
cd "$( dirname "${EXPECTED_ZIP_FILE}" )" || exit 1
cd "$( dirname "${EXPECTED_ZIP_FILE}" )" || exit $?
sha256sum "$( basename "${EXPECTED_ZIP_FILE}" )"
)"
)" || return $?
local verify256=
verify256="$( cat "${EXPECTED_ZIP_FILE}.sha256" )"
verify256="$( cat "${EXPECTED_ZIP_FILE}.sha256" )" || return $?
if [ "${check256}" != "${verify256}" ] ; then
echofatal "Wrong sha256sum !"
......@@ -219,7 +222,7 @@ function check_zip_once {
COMPUTED: ${check256}
EXPECTED: ${verify256}
EOF
exit 1
return 100
fi
ZIP_CHECKED='true'
......@@ -260,11 +263,13 @@ function main {
'-T'|'--download-to')
action='DOWNLOAD'
param_download_image_directory="$( P "$@" )" && shift
param_download_image_directory="$( test_P "$@" )" || return $?
shift
;;
'--arch')
param_arch="$( P "$@" )" && shift
param_arch="$( test_P "$@" )" || return $?
shift
;;
'--desktop'|'--full')
......@@ -277,12 +282,14 @@ function main {
'--url')
param_url='true'
LAST_VERSION_URL="$( P "$@" )" && shift
LAST_VERSION_URL="$( test_P "$@" )" || return $?
shift
declare -gr LAST_VERSION_URL
;;
'--expected-img')
param_expected_img="$( P "$@" )" && shift
param_expected_img="$( test_P "$@" )" || return $?
shift
;;
*)
......@@ -354,7 +361,7 @@ EOF
download_if_needed
if [ -z "${param_url}" ] ; then
check_zip_once
check_zip_once || return $?
else
echowarn "Cannot trust downloaded file (don't how to get hashcode)."
fi
......@@ -369,12 +376,12 @@ EOF
if [ "${param_unzip}" = 'true' ] ; then
if [ -z "${param_url}" ] ; then
check_zip_once
check_zip_once || return $?
else
echowarn "Cannot trust downloaded file (don't how to get hashcode)."
fi
unzip_if_needed
unzip_if_needed || return $?
fi
cat <<EOF
......@@ -385,4 +392,4 @@ EOF
prepare_raspbian_initialise_if_needed RASPBIAN_DEFAULT_DOWNLOAD_IMAGE_DIRECTORY "$( xdg-user-dir DOWNLOAD )"
main "$@"
main "$@" || exit $?
......@@ -9,13 +9,13 @@ PROJECT_HOME="$( dirname "$( dirname "$( realpath "${BASH_SOURCE[0]}" )" )" )"
declare -gr PROJECT_HOME
# shellcheck disable=SC1090
source "${PROJECT_HOME}/apis/bash-common-api/bash-common-api.sh" || exit 1
source "${PROJECT_HOME}/apis/bash-common-api/bash-common-api.sh" || exit $?
# shellcheck disable=SC1090
source "${PROJECT_HOME}/bin/api/prepare-raspbian-api.sh" || exit 1
source "${PROJECT_HOME}/bin/api/prepare-raspbian-api.sh" || exit $?
# shellcheck disable=SC1090
source "${PROJECT_HOME}/bin/api/prepare-raspbian-config-json-api.sh" || exit 1
source "${PROJECT_HOME}/bin/api/prepare-raspbian-config-json-api.sh" || exit $?
# shellcheck disable=SC1090
source "${PROJECT_HOME}/bin/api/prepare-raspbian-customize-api.sh" || exit 1
source "${PROJECT_HOME}/bin/api/prepare-raspbian-customize-api.sh" || exit $?
function usage {
cat <<EOF
......@@ -118,9 +118,9 @@ function get_last_img_version {
local output=
output="$(
"${PROJECT_HOME}/bin/prepare-raspbian-sd-download-image.sh" --unzip --download-to "${download_to}" "${options[@]}"
)" || exit 1
)" || return $?
grep 'LAST_IMG_FILE:' <<<"${output}" | cut -d ':' -f2
grep 'LAST_IMG_FILE:' <<<"${output}" | cut -d ':' -f2 || return $?
}
function download_img_file {
......@@ -208,7 +208,9 @@ function create_sd_complete_image {
echofatal "SD Card '${sd_full_dev_name}' is mounted, you need to run:"
while read -r dev ; do
echo "sudo umount ${dev}"
if [ -n "${dev}" ] ; then
echo "sudo umount ${dev}"
fi
done < <( mount | grep "${sd_full_dev_name}" | cut -d ' ' -f1 | sort )
exit 1
fi
......@@ -268,8 +270,7 @@ function get_SD_device {
function get_SD_device_safe {
local sd_dev_name=
sd_dev_name="$( get_SD_device )"
sd_dev_name="$( get_SD_device )" || true # Don't fail on error
if [ -z "${sd_dev_name}" ] ; then
echofatal 'No SD devices found. Do you have a connected SD card?'
......@@ -292,7 +293,7 @@ function init_SD_DEV_NAME {
if [ -z "${SD_DEV_NAME}" ] ; then
echofatal 'Cannot compute SD_DEV_NAME'
echofatal 'Use must use --dev-name option'
echofatal 'Use must use --dev-name option or plug SD Card.'
exit 1
else
local -r sd_full_dev_name="/dev/${SD_DEV_NAME}"
......@@ -350,7 +351,8 @@ function main {
'-I'|'--put-image')
action='WRITE_ON_SD'
param_image_file="$( P "$@" )" && shift
param_image_file="$( test_P "$@" )" || return $?
shift
;;
'-L'|'--put-last-image')
......@@ -361,7 +363,8 @@ function main {
'--url')
action='WRITE_ON_SD'
param_image_file=':URL:'
param_url="$( P "$@" )" && shift
param_url="$( test_P "$@" )" || return $?
shift
;;
'--configure')
......@@ -376,7 +379,8 @@ function main {
'-T'|'--full-clone-to')
action='READ_FROM_SD'
param_image_file="$( P "$@" )" && shift
param_image_file="$( test_P "$@" )" || return $?
shift
;;
'--dry-run')
......@@ -384,11 +388,13 @@ function main {
;;
'--arch')
param_arch="$( P "$@" )" && shift
param_arch="$( test_P "$@" )" || return $?
shift
;;
'--download-to')
download_to="$( P "$@" )" && shift
download_to="$( test_P "$@" )" || return $?
shift
;;
'--desktop'|'--full'|'--gui')
......@@ -396,7 +402,8 @@ function main {
;;
'--dev-name')
param_force_sd_dev_name="$( P "$@" )" && shift
param_force_sd_dev_name="$( test_P "$@" )" || return $?
shift
;;
'--compute-sd-dev-name')
......@@ -405,7 +412,8 @@ function main {
;;
'--expected-img')
param_expected_img="$( P "$@" )" && shift
param_expected_img="$( test_P "$@" )" || return $?
shift
;;
'--unzip')
......@@ -455,15 +463,15 @@ function main {
case "${action}" in
'CONFIGURE')
customize_sd
customize_sd || return $?
;;
'WRITE_ON_SD')
erase_sd_card_with_image "${sd_full_dev_name}" "${param_image_file}"
erase_sd_card_with_image "${sd_full_dev_name}" "${param_image_file}" || return $?
;;
'READ_FROM_SD')
create_sd_complete_image "${sd_full_dev_name}" "${param_image_file}"
create_sd_complete_image "${sd_full_dev_name}" "${param_image_file}" || return $?
;;
'DRYRUN')
......@@ -482,5 +490,5 @@ prepare_raspbian_initialise_if_needed RASPBIAN_DEFAULT_BACKUP_DIRECTORY "$( xdg-
prepare_raspbian_initialise_if_needed RASPBIAN_DEFAULT_DOWNLOAD_IMAGE_DIRECTORY "$( xdg-user-dir DOWNLOAD )"
prepare_raspbian_initialise_if_needed RASPBIAN_DEFAULT_IMG ''
main "$@"
main "$@" || exit $?
......@@ -49,7 +49,9 @@ mkdir -vp "${_DOWNLOAD_}" "${_BACKUP_}"
prepare-raspbian-sd-config RASPBIAN_DEFAULT_BACKUP_DIRECTORY "${_BACKUP_}"
prepare-raspbian-sd-config RASPBIAN_DEFAULT_DOWNLOAD_IMAGE_DIRECTORY "${_DOWNLOAD_}"
prepare-raspbian-sd-config SSH_AUTHORIZED_KEYS "$( cat ~/.ssh/id_rsa.pub )"
prepare-raspbian-sd-config BCM_WORKSPACE true
prepare-raspbian-sd-config --set-boolean BCM_WORKSPACE true
prepare-raspbian-sd-config --set-boolean SSH_ENABLE true
prepare-raspbian-sd-config --set-boolean SSH_CONFIGURE true
# Configuration (optional - if used ensure value is what you want)
prepare-raspbian-sd-config DEFAULT_SD_DEV_NAME 'mmcblk0'
......
......@@ -25,9 +25,12 @@ Manage download for `prepare-raspbian-sd`.
```
--arch ARCH Define machine hardware name (optional). If not set, or empty, use "Raspbian"
as source, if set use "Raspberry Pi OS" as source). ARCH could be an empty string, or
armhf. In the future it will be mandatory to choose beetwin 32 bit or 64 bits
version of "Raspberry Pi OS"
as source, if set use "Raspberry Pi OS" as source). ARCH could be an empty string,
armhf or arm64.
--arch '' : Download last available version of raspbian
--arch armhf: Download last 32 bits version of "Raspberry Pi OS"
--arch arm32: Same as 'armhf'
--arch arm64: Download last 64 bits version of "Raspberry Pi OS"
(Parameter is ignored if --url is used)
--desktop, Same as --full (deprecated)
--expected-img IMG Expected image filename (without path) - if specified when using --url allow
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment