Skip to content

Commit e821052

Browse files
alexeicolingalak
authored andcommitted
scripts/template_dir: re-use common prompting code
1 parent bbb5d8c commit e821052

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

scripts/template_dir

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,10 @@ do_zephyrrc()
131131
echo " export ZEPHYR_SDK_INSTALL_DIR=$target_sdk_dir"
132132
echo
133133
if [ -z "$rc_confirm" ]; then
134-
echo "Update/Create $HOME/.zephyrrc with environment variables setup for you (y/n)? "
135-
while read rc_confirm; do
136-
[ "$rc_confirm" = "Y" -o "$rc_confirm" = "y" \
137-
-o "$rc_confirm" = "n" \
138-
-o "$rc_confirm" = "N" ] && break
139-
echo "Invalid input \"$rc_confirm\", please input 'y' or 'n': "
140-
done
134+
prompt "creating/updating $HOME/.zephyrrc with environment variables setup for you "
135+
rc_confirm=$answer
141136
fi
142-
if [ "$rc_confirm" = "y" -o "$rc_confirm" = "Y" ]; then
137+
if [ "$rc_confirm" = "y" ]; then
143138
if [ -f $HOME/.zephyrrc ]; then
144139
sed -i "s#ZEPHYR_SDK_INSTALL_DIR=.*#ZEPHYR_SDK_INSTALL_DIR=${target_sdk_dir}#" $HOME/.zephyrrc
145140
sed -i "s#ZEPHYR_TOOLCHAIN_VARIANT=.*#ZEPHYR_TOOLCHAIN_VARIANT=zephyr#" $HOME/.zephyrrc
@@ -163,8 +158,8 @@ do_cmake_package()
163158
return
164159
fi
165160

166-
read_confirm "registering Zephyr-sdk CMake module with path $target_sdk_dir "
167-
if [ "$confirm" = "y" -o "$confirm" = "Y" ]; then
161+
query "registering Zephyr-sdk CMake module with path $target_sdk_dir "
162+
if [ "$answer" = "y" ]; then
168163
if [ ! -d $ZEPHYR_SDK_REGISTRY_DIR ]; then
169164
mkdir -p $ZEPHYR_SDK_REGISTRY_DIR
170165
fi
@@ -176,17 +171,28 @@ do_cmake_package()
176171

177172
# Read the input "y"
178173
# $1 is optional, but allows the caller to provide additional text.
179-
read_confirm () {
180-
if [ "$confirm" != "y" ]; then
181-
echo "Do you want to continue $1(y/n)? "
182-
while read confirm; do
183-
[ "$confirm" = "Y" -o "$confirm" = "y" -o "$confirm" = "n" \
184-
-o "$confirm" = "N" ] && break
185-
echo "Invalid input \"$confirm\", please input 'y' or 'n': "
186-
done
187-
else
188-
echo
189-
fi
174+
prompt () {
175+
echo "Do you want to continue $1(y/n)? "
176+
while read answer; do
177+
if [ "$answer" = "Y" -o "$answer" = "y" ]; then
178+
answer="y"
179+
break
180+
elif [ "$answer" = "N" -o "$answer" = "n" ]; then
181+
answer="n"
182+
break
183+
else
184+
echo "Invalid input \"$answer\", please input 'y' or 'n': "
185+
fi
186+
done
187+
}
188+
# Like prompt, but only if prompts were not pre-answered by -y argument
189+
query () {
190+
if [ "$confirm" != "y" ]; then
191+
prompt "$1"
192+
else
193+
answer="y"
194+
echo
195+
fi
190196
}
191197

192198
verify_os
@@ -261,8 +267,8 @@ if [ "$sdk_dirname" != "/opt" \
261267
echo Note: The version number \'-$SDK_VERSION\' can be omitted.
262268
echo
263269

264-
read_confirm "installing to ${target_sdk_dir} "
265-
if [ "$confirm" = "n" -o "$confirm" = "N" ]; then
270+
query "installing to ${target_sdk_dir} "
271+
if [ "$answer" = "n" ]; then
266272
# Abort the installation
267273
echo "SDK installation aborted!"
268274
exit 1
@@ -282,8 +288,8 @@ if [ -d $target_sdk_dir ]; then
282288
# wipe the directory first
283289
if [ -d $target_sdk_dir/sysroots ]; then
284290
echo "The directory $target_sdk_dir/sysroots will be removed! "
285-
read_confirm
286-
if [ "$confirm" = "y" -o "$confirm" = "Y" ]; then
291+
query
292+
if [ "$answer" = "y" ]; then
287293
rm -rf $target_sdk_dir/sysroots/
288294
rm -rf $target_sdk_dir/info-zephyr-sdk*/
289295
rm -fr $target_sdk_dir/sdk_version

0 commit comments

Comments
 (0)