Skip to content

Commit f2328fc

Browse files
authored
Add check for abstract contexts & force option to ignore it (#74)
* Refactor compose files checks * Check for abstract context and fail if trying to run it * Add force option * Document force option in help texts * Ignore abstract context error if forced * Add abtract_standard tests * Add abtract_standard_two tests * Fix whitespaces in help text test * Fix help args text test * Fix help args text * Fix whitespaces in help args text test * Fix help args text test * Fix whitespaces in help args text
1 parent 260f0d9 commit f2328fc

File tree

12 files changed

+124
-20
lines changed

12 files changed

+124
-20
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#:dockerc.abstract
12
# Services definition for building & deploying
23
services:
34

dockerc

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ VERSION="1.10.0"
1212
PRINT_HELP=false
1313
PRINT_VERSION=false
1414
UPDATE=false
15+
FORCE=false
1516
DRY_RUN=false
1617
QUIET=false
1718

@@ -43,6 +44,11 @@ while true; do
4344
UPDATE=true
4445
shift
4546
;;
47+
'-f')
48+
# Force, for example ignores abstract contexts
49+
FORCE=true
50+
shift
51+
;;
4652
'-n')
4753
# Dry run, print docker compose command without running it
4854
DRY_RUN=true
@@ -76,6 +82,7 @@ if [ "$PRINT_HELP" = true ]; then
7682
echo ' -h, --help Print this help and exit'
7783
echo ' -v, --version Print version and exit'
7884
echo ' --update Update DockerC and exit'
85+
echo ' -f Force, for example ignores abstract contexts'
7986
echo ' -n Dry run, print docker compose command without running it'
8087
echo ' -q Quiet, do not print docker compose command'
8188
exit 0
@@ -216,6 +223,7 @@ if [ "$(echo "$CONTEXT" | cut -c1)" = '@' ]; then
216223
echo ' @rfa Remove all unused containers, networks and images'
217224
echo ' @rfav Remove all unused containers, networks, images and volumes'
218225
echo ' options:'
226+
echo ' -f Force, for example ignores abstract contexts'
219227
echo ' -n Dry run, print docker command without running it'
220228
echo ' -q Quiet, do not print docker command'
221229
exit 0
@@ -240,6 +248,7 @@ fi
240248

241249
COMPOSE_FILE_ARGS=''
242250
NO_COMPOSE_FILE_ARGS=false
251+
ABSTRACT_COMPOSE_FILE_ARGS=false
243252
ENV_FILE_ARGS=''
244253

245254
# If context is -, use default docker compose files (override if it exists)
@@ -274,8 +283,15 @@ if [ "$NO_COMPOSE_FILE_ARGS" = false ]; then
274283
ENV_FILE_ARGS="$ENV_FILE_ARGS --env-file $dir/.env.local"
275284
fi
276285

277-
if [ -f "$dir/docker-compose.yml" ]; then
278-
COMPOSE_FILE_ARGS_1_DEFAULT=" -f $dir/docker-compose.yml"
286+
COMPOSE_FILE_PATH="$dir/docker-compose.yml"
287+
if [ -f "$COMPOSE_FILE_PATH" ]; then
288+
COMPOSE_FILE_ARGS_1_DEFAULT=" -f $COMPOSE_FILE_PATH"
289+
290+
if cat "$COMPOSE_FILE_PATH" | grep -q "^#:dockerc.abstract$"; then
291+
ABSTRACT_COMPOSE_FILE_ARGS=true
292+
else
293+
ABSTRACT_COMPOSE_FILE_ARGS=false
294+
fi
279295
fi
280296

281297
if [ ! -z "$CONTEXT" ]; then
@@ -307,11 +323,18 @@ if [ "$NO_COMPOSE_FILE_ARGS" = false ]; then
307323
if [ -z "$COMPOSE_FILE_ARGS_2" ]; then
308324

309325
# Look for docker-compose$PREFIX_1.$part.yml
310-
if [ -f "$dir/docker-compose$PREFIX_1.$MISSING_PARTS$part.yml" ]; then
311-
COMPOSE_FILE_ARGS_1="$COMPOSE_FILE_ARGS_1 -f $dir/docker-compose$PREFIX_1.$MISSING_PARTS$part.yml"
326+
COMPOSE_FILE_PATH="$dir/docker-compose$PREFIX_1.$MISSING_PARTS$part.yml"
327+
if [ -f "$COMPOSE_FILE_PATH" ]; then
328+
COMPOSE_FILE_ARGS_1="$COMPOSE_FILE_ARGS_1 -f $COMPOSE_FILE_PATH"
312329
PREFIX_1="$PREFIX_1.$MISSING_PARTS$part"
313330
MISSING_PARTS=''
314331

332+
if cat "$COMPOSE_FILE_PATH" | grep -q "^#:dockerc.abstract$"; then
333+
ABSTRACT_COMPOSE_FILE_ARGS=true
334+
else
335+
ABSTRACT_COMPOSE_FILE_ARGS=false
336+
fi
337+
315338
else
316339
# Part was not found, invalidate PREFIX_1
317340
PREFIX_1=''
@@ -326,10 +349,17 @@ if [ "$NO_COMPOSE_FILE_ARGS" = false ]; then
326349
if [ -z "$PREFIX_2" ]; then
327350

328351
# Look for docker-compose-$part.yml
329-
if [ -f "$dir/docker-compose-$MISSING_PARTS$part.yml" ]; then
330-
COMPOSE_FILE_ARGS_2="$COMPOSE_FILE_ARGS_2 -f $dir/docker-compose-$MISSING_PARTS$part.yml"
352+
COMPOSE_FILE_PATH="$dir/docker-compose-$MISSING_PARTS$part.yml"
353+
if [ -f "$COMPOSE_FILE_PATH" ]; then
354+
COMPOSE_FILE_ARGS_2="$COMPOSE_FILE_ARGS_2 -f $COMPOSE_FILE_PATH"
331355
PREFIX_2="-$MISSING_PARTS$part"
332356
MISSING_PARTS=''
357+
358+
if cat "$COMPOSE_FILE_PATH" | grep -q "^#:dockerc.abstract$"; then
359+
ABSTRACT_COMPOSE_FILE_ARGS=true
360+
else
361+
ABSTRACT_COMPOSE_FILE_ARGS=false
362+
fi
333363
fi
334364

335365
fi
@@ -339,10 +369,17 @@ if [ "$NO_COMPOSE_FILE_ARGS" = false ]; then
339369
if [ ! -z "$PREFIX_2" ]; then
340370

341371
# Look for docker-compose$PREFIX_2.$part.yml
342-
if [ -f "$dir/docker-compose$PREFIX_2.$part.yml" ]; then
343-
COMPOSE_FILE_ARGS_2="$COMPOSE_FILE_ARGS_2 -f $dir/docker-compose$PREFIX_2.$part.yml"
372+
COMPOSE_FILE_PATH="$dir/docker-compose$PREFIX_2.$part.yml"
373+
if [ -f "$COMPOSE_FILE_PATH" ]; then
374+
COMPOSE_FILE_ARGS_2="$COMPOSE_FILE_ARGS_2 -f $COMPOSE_FILE_PATH"
344375
PREFIX_2="$PREFIX_2.$part"
345376

377+
if cat "$COMPOSE_FILE_PATH" | grep -q "^#:dockerc.abstract$"; then
378+
ABSTRACT_COMPOSE_FILE_ARGS=true
379+
else
380+
ABSTRACT_COMPOSE_FILE_ARGS=false
381+
fi
382+
346383
else
347384
# Part was not found, invalidate PREFIX_2
348385
PREFIX_2=''
@@ -384,8 +421,16 @@ EOF
384421

385422
elif [ -z "$CONTEXT" ] || [ "$CONTEXT" = 'dev' ]; then
386423

387-
if [ -f "$dir/docker-compose.override.yml" ]; then
388-
COMPOSE_FILE_ARGS="$COMPOSE_FILE_ARGS_1_DEFAULT -f $dir/docker-compose.override.yml"
424+
COMPOSE_FILE_PATH="$dir/docker-compose.override.yml"
425+
if [ -f "$COMPOSE_FILE_PATH" ]; then
426+
COMPOSE_FILE_ARGS="$COMPOSE_FILE_ARGS_1_DEFAULT -f $COMPOSE_FILE_PATH"
427+
428+
if cat "$COMPOSE_FILE_PATH" | grep -q "^#:dockerc.abstract$"; then
429+
ABSTRACT_COMPOSE_FILE_ARGS=true
430+
else
431+
ABSTRACT_COMPOSE_FILE_ARGS=false
432+
fi
433+
389434
break
390435

391436
elif [ -z "$CONTEXT" ]; then
@@ -413,6 +458,13 @@ EOF
413458
exit 1
414459
fi
415460

461+
# Unless force option is set,
462+
# If compose file arguments are abstract, exit with error
463+
if [ "$FORCE" = false ] && [ "$ABSTRACT_COMPOSE_FILE_ARGS" = true ]; then
464+
echo 'Error: Abstract context found'
465+
exit 1
466+
fi
467+
416468
fi
417469

418470
# Check for shell arguments preset shortcut
@@ -732,11 +784,11 @@ elif [ "$(echo "$ARGS" | cut -c1)" = '@' ]; then
732784
echo ' @bp build --pull'
733785
echo ' @bf build --pull --no-cache'
734786
echo ' @bn build --dry-run --pull --no-cache'
735-
echo ' @w watch'
736-
echo ' @wq watch --quiet'
737-
echo ' @ww watch --no-up'
738-
echo ' @wqw watch --quiet --no-up'
739-
echo ' @wn watch --dry-run'
787+
echo ' @w watch'
788+
echo ' @wq watch --quiet'
789+
echo ' @ww watch --no-up'
790+
echo ' @wqw watch --quiet --no-up'
791+
echo ' @wn watch --dry-run'
740792
echo ' @d down'
741793
echo ' @da down --remove-orphans'
742794
echo ' @dr down --remove-orphans --rmi local'
@@ -792,6 +844,7 @@ elif [ "$(echo "$ARGS" | cut -c1)" = '@' ]; then
792844
echo ' @rfv rm -f -v'
793845
print_context_help
794846
echo ' options:'
847+
echo ' -f Force, for example ignores abstract contexts'
795848
echo ' -n Dry run, print docker compose command without running it'
796849
echo ' -q Quiet, do not print docker compose command'
797850
exit 0

test/test/abstract_standard/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#:dockerc.abstract
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from test.src.format_dockerc_stdout import format_dockerc_stdout
2+
from test.src.TestDirContext import TestDirContext
3+
4+
def test_abstract_standard(file = __file__):
5+
with TestDirContext(file) as ctx:
6+
dockerc = ctx.run_dockerc()
7+
dockerc.assert_context_error(
8+
stdout = b'Error: Abstract context found\n',
9+
)
10+
11+
def test_abstract_standard_forced(file = __file__):
12+
with TestDirContext(file) as ctx:
13+
dockerc = ctx.run_dockerc(
14+
'-f',
15+
)
16+
dockerc.assert_context_found(
17+
format_dockerc_stdout(
18+
b'docker compose'
19+
b' -f ./docker-compose.yml'
20+
b' up -d'
21+
),
22+
)

test/test/abstract_standard_two/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#:dockerc.abstract
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#:dockerc.abstract
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from test.src.format_dockerc_stdout import format_dockerc_stdout
2+
from test.src.TestDirContext import TestDirContext
3+
4+
def test_simple(file = __file__):
5+
with TestDirContext(file) as ctx:
6+
dockerc = ctx.run_dockerc()
7+
dockerc.assert_context_error(
8+
stdout = b'Error: Abstract context found\n',
9+
)
10+
11+
def test_forced(file = __file__):
12+
with TestDirContext(file) as ctx:
13+
dockerc = ctx.run_dockerc(
14+
'-f',
15+
)
16+
dockerc.assert_context_found(
17+
format_dockerc_stdout(
18+
b'docker compose'
19+
b' -f ./docker-compose.yml -f ./docker-compose.override.yml'
20+
b' up -d'
21+
),
22+
)

test/test/help/test_help.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def get_help_stdout(dockerc_path: str):
1313
b' -h, --help Print this help and exit\n'
1414
b' -v, --version Print version and exit\n'
1515
b' --update Update DockerC and exit\n'
16+
b' -f Force, for example ignores abstract contexts\n'
1617
b' -n Dry run, print docker compose command without running it\n'
1718
b' -q Quiet, do not print docker compose command\n'
1819
)

0 commit comments

Comments
 (0)