summarylogtreecommitdiffstats
path: root/0003-scripts-template_dir-re-use-common-prompting-code.patch
blob: 7b9cdef2b55ae65bfc5ee2aa2e284ffa18fb36cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
From 03933375277558f939a12990cae3ee372511d87e Mon Sep 17 00:00:00 2001
From: Alexei Colin <ac@alexeicolin.com>
Date: Sat, 18 Jul 2020 12:14:07 -0400
Subject: [PATCH 3/4] scripts/template_dir: re-use common prompting code

---
 scripts/template_dir | 56 ++++++++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 25 deletions(-)

diff --git a/scripts/template_dir b/scripts/template_dir
index b03d4b5..0a0d69e 100644
--- a/scripts/template_dir
+++ b/scripts/template_dir
@@ -131,15 +131,10 @@ do_zephyrrc()
 	echo "     export ZEPHYR_SDK_INSTALL_DIR=$target_sdk_dir"
 	echo
 	if [ -z "$rc_confirm" ]; then
-		echo "Update/Create $HOME/.zephyrrc with environment variables setup for you (y/n)? "
-		while read rc_confirm; do
-			[ "$rc_confirm" = "Y" -o "$rc_confirm" = "y" \
-			-o "$rc_confirm" = "n" \
-			-o "$rc_confirm" = "N" ] && break
-			echo "Invalid input \"$rc_confirm\", please input 'y' or 'n': "
-		done
+		prompt "creating/updating $HOME/.zephyrrc with environment variables setup for you "
+		rc_confirm=$answer
 	fi
-	if [ "$rc_confirm" = "y" -o "$rc_confirm" = "Y" ]; then
+	if [ "$rc_confirm" = "y" ]; then
 		if [ -f $HOME/.zephyrrc ]; then
 			sed -i "s#ZEPHYR_SDK_INSTALL_DIR=.*#ZEPHYR_SDK_INSTALL_DIR=${target_sdk_dir}#" $HOME/.zephyrrc
 			sed -i "s#ZEPHYR_TOOLCHAIN_VARIANT=.*#ZEPHYR_TOOLCHAIN_VARIANT=zephyr#" $HOME/.zephyrrc
@@ -163,8 +158,8 @@ do_cmake_package()
 			return
 		fi
 
-		read_confirm "registering Zephyr-sdk CMake module with path $target_sdk_dir "
-		if [ "$confirm" = "y" -o "$confirm" = "Y" ]; then
+		query "registering Zephyr-sdk CMake module with path $target_sdk_dir "
+		if [ "$answer" = "y" ]; then
 			if [ ! -d $ZEPHYR_SDK_REGISTRY_DIR ]; then
 				mkdir -p $ZEPHYR_SDK_REGISTRY_DIR
 			fi
@@ -176,17 +171,28 @@ do_cmake_package()
 
 # Read the input "y"
 # $1 is optional, but allows the caller to provide additional text.
-read_confirm () {
-  if [ "$confirm" != "y" ]; then
-      echo "Do you want to continue $1(y/n)? "
-      while read confirm; do
-          [ "$confirm" = "Y" -o "$confirm" = "y" -o "$confirm" = "n" \
-            -o "$confirm" = "N" ] && break
-          echo "Invalid input \"$confirm\", please input 'y' or 'n': "
-      done
-  else
-      echo
-  fi
+prompt () {
+	echo "Do you want to continue $1(y/n)? "
+	while read answer; do
+		if [ "$answer" = "Y" -o "$answer" = "y" ]; then
+			answer="y"
+			break
+		elif [ "$answer" = "N" -o "$answer" = "n" ]; then
+			answer="n"
+			break
+		else
+			echo "Invalid input \"$answer\", please input 'y' or 'n': "
+		fi
+	done
+}
+# Like prompt, but only if prompts were not pre-answered by -y argument
+query () {
+	if [ "$confirm" != "y" ]; then
+		prompt "$1"
+	else
+		answer="y"
+		echo
+	fi
 }
 
 verify_os
@@ -261,8 +267,8 @@ if [    "$sdk_dirname" != "/opt" \
 	echo Note: The version number \'-$SDK_VERSION\' can be omitted.
 	echo
 
-	read_confirm "installing to ${target_sdk_dir} "
-	if [ "$confirm" = "n" -o "$confirm" = "N" ]; then
+	query "installing to ${target_sdk_dir} "
+	if [ "$answer" = "n" ]; then
 		# Abort the installation
 		echo "SDK installation aborted!"
 		exit 1
@@ -282,8 +288,8 @@ if [ -d $target_sdk_dir ]; then
 		# wipe the directory first
 		if [ -d $target_sdk_dir/sysroots ]; then
 			echo "The directory $target_sdk_dir/sysroots will be removed! "
-			read_confirm
-			if [ "$confirm" = "y" -o "$confirm" = "Y" ]; then
+			query
+			if [ "$answer" = "y" ]; then
 		        	rm -rf $target_sdk_dir/sysroots/
 				rm -rf $target_sdk_dir/info-zephyr-sdk*/
 				rm -fr $target_sdk_dir/sdk_version
-- 
2.27.0