Skip to content

Commit 7682e70

Browse files
committed
refactor: de-duplicate func for checking if Docker Compose supports using cache_from & cache_to
1 parent d9463c7 commit 7682e70

2 files changed

Lines changed: 9 additions & 17 deletions

File tree

lib/shared.bash

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,15 @@ function build_image_override_file() {
133133
"$(docker_compose_config_version)" "$@"
134134
}
135135

136-
# Checks that a specific version of docker-compose supports cache_from
136+
# Checks that a specific version of docker-compose supports cache_from and cache_to
137137
function docker_compose_supports_cache() {
138138
local version="$1"
139139
if [[ "$version" == 1* || "$version" =~ ^(2|3)(\.[01])?$ ]] ; then
140-
return 1
140+
echo "Unsupported Docker Compose config file version: $version"
141+
echo "The 'cache_from' option can only be used with Compose file versions 2.2 or 3.2 and above."
142+
echo "For more information on Docker Compose configuration file versions, see:"
143+
echo "https://docs.docker.com/compose/compose-file/compose-versioning/#versioning"
144+
exit 1
141145
fi
142146
}
143147

@@ -216,13 +220,7 @@ function build_image_override_file_with_version() {
216220
fi
217221

218222
if [[ "$cache_from_amt" -gt 0 ]] ; then
219-
if ! docker_compose_supports_cache "$version" ; then
220-
echo "Unsupported Docker Compose config file version: $version"
221-
echo "The 'cache_from' option can only be used with Compose file versions 2.2 or 3.2 and above."
222-
echo "For more information on Docker Compose configuration file versions, see:"
223-
echo "https://docs.docker.com/compose/compose-file/compose-versioning/#versioning"
224-
exit 1
225-
fi
223+
docker_compose_supports_cache "$version"
226224

227225
printf " cache_from:\\n"
228226
for cache_from_i in "${cache_from[@]}"; do
@@ -231,13 +229,7 @@ function build_image_override_file_with_version() {
231229
fi
232230

233231
if [[ "$cache_to_amt" -gt 0 ]] ; then
234-
if ! docker_compose_supports_cache "$version" ; then
235-
echo "Unsupported Docker Compose config file version: $version"
236-
echo "The 'cache_to' option can only be used with Compose file versions 2.2 or 3.2 and above."
237-
echo "For more information on Docker Compose configuration file versions, see:"
238-
echo "https://docs.docker.com/compose/compose-file/compose-versioning/#versioning"
239-
exit 1
240-
fi
232+
docker_compose_supports_cache "$version"
241233

242234
printf " cache_to:\\n"
243235
for cache_to_i in "${cache_to[@]}"; do

tests/docker-compose-config.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ load '../lib/shared'
8181
assert_output "2.0"
8282
}
8383

84-
@test "Whether docker-compose supports cache_from directive" {
84+
@test "Whether docker-compose supports cache_from and cache_to directive" {
8585
run docker_compose_supports_cache ""
8686
assert_success
8787

0 commit comments

Comments
 (0)