Skip to content

Commit 54e098c

Browse files
committed
Refactor build script to auto-discover Docker images
- Implement a function to dynamically discover available images from the images/ directory based on the presence of Dockerfiles. - Update the help message to reflect the new image discovery feature, allowing users to see all available images without hardcoding. - Enhance usage instructions for running images interactively and provide a reference to the README for build commands.
1 parent c52123a commit 54e098c

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

scripts/build.sh

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,35 @@ NC='\033[0m' # No Color
1616
# Image tag for local builds
1717
IMAGE_TAG="${IMAGE_TAG:-local}"
1818

19-
# Available images
20-
declare -A IMAGES=(
21-
["esp-idf"]="images/esp-idf"
22-
["platformio"]="images/platformio"
23-
)
19+
# Discover available images from images/ directory
20+
# Each subdirectory with a Dockerfile is considered an image
21+
declare -A IMAGES=()
22+
23+
discover_images() {
24+
local images_dir="images"
25+
26+
if [ ! -d "$images_dir" ]; then
27+
echo "Error: $images_dir directory not found"
28+
exit 1
29+
fi
30+
31+
for dir in "$images_dir"/*/ ; do
32+
if [ -d "$dir" ]; then
33+
local name=$(basename "$dir")
34+
if [ -f "$dir/Dockerfile" ]; then
35+
IMAGES["$name"]="$images_dir/$name"
36+
fi
37+
fi
38+
done
39+
40+
if [ ${#IMAGES[@]} -eq 0 ]; then
41+
echo "Error: No images found in $images_dir directory"
42+
exit 1
43+
fi
44+
}
45+
46+
# Discover images on script start
47+
discover_images
2448

2549
# Print colored message
2650
print_color() {
@@ -41,9 +65,16 @@ Options:
4165
-h, --help Show this help message
4266
4367
Arguments:
44-
IMAGE_NAME Name of the image to build (esp-idf, platformio)
68+
IMAGE_NAME Name of the image to build (auto-discovered from images/ directory)
4569
all Build all images
4670
71+
Available images:
72+
EOF
73+
for name in $(printf '%s\n' "${!IMAGES[@]}" | sort); do
74+
echo " - $name"
75+
done
76+
cat << EOF
77+
4778
Environment Variables:
4879
IMAGE_TAG Docker image tag to use (default: local)
4980
@@ -108,21 +139,13 @@ build_image() {
108139
return 0
109140
fi
110141

111-
print_color "$YELLOW" "To enter the image in interactive mode:"
142+
print_color "$YELLOW" "To run the image interactively:"
112143
echo
113144
print_color "$BLUE" " docker run -it --rm -v \$(pwd):/workspace jethome-dev-${name}:${IMAGE_TAG}"
114145
echo
115-
print_color "$YELLOW" "To build your project:"
116-
echo
117-
118-
if [ "$name" == "esp-idf" ]; then
119-
print_color "$BLUE" " docker run --rm -v \$(pwd):/workspace jethome-dev-${name}:${IMAGE_TAG} idf.py build"
120-
elif [ "$name" == "platformio" ]; then
121-
print_color "$BLUE" " docker run --rm -v \$(pwd):/workspace jethome-dev-${name}:${IMAGE_TAG} pio run"
122-
fi
123-
146+
print_color "$YELLOW" "For usage examples and build commands, see:"
124147
echo
125-
print_color "$YELLOW" "See ${context}/README.md for more usage examples."
148+
print_color "$BLUE" " ${context}/README.md"
126149
echo
127150
}
128151

0 commit comments

Comments
 (0)