Skip to content

Commit b841dea

Browse files
committed
reconcile CLI/config priority
- avoid CLI defaults clobbering values from config. Mostly by using `default=None`, and adding `if args.name is not None` checks before assigning attributes
1 parent b6098c0 commit b841dea

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

repo2docker/__main__.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def get_argparser():
9393

9494
argparser.add_argument(
9595
"--json-logs",
96-
default=False,
96+
default=None,
9797
action="store_true",
9898
help="Emit JSON logs instead of human readable logs",
9999
)
@@ -108,7 +108,7 @@ def get_argparser():
108108

109109
argparser.add_argument(
110110
"--image-name",
111-
help=("Name of image to be built. If unspecified will be " "autogenerated"),
111+
help="Name of image to be built. If unspecified will be autogenerated",
112112
type=validate_image_name,
113113
)
114114

@@ -126,9 +126,7 @@ def get_argparser():
126126
"--no-build",
127127
dest="build",
128128
action="store_false",
129-
help=(
130-
"Do not actually build the image. Useful in conjunction " "with --debug."
131-
),
129+
help="Do not actually build the image. Useful in conjunction with --debug.",
132130
)
133131

134132
argparser.add_argument(
@@ -164,13 +162,15 @@ def get_argparser():
164162
"--publish-all",
165163
"-P",
166164
dest="all_ports",
165+
default=None,
167166
action="store_true",
168167
help="Publish all exposed ports to random host ports.",
169168
)
170169

171170
argparser.add_argument(
172171
"--no-clean",
173172
dest="clean",
173+
default=None,
174174
action="store_false",
175175
help="Don't clean up remote checkouts after we are done",
176176
)
@@ -250,6 +250,7 @@ def get_argparser():
250250
return argparser
251251

252252

253+
# Note: only used by sphinx-autoprogram
253254
argparser = get_argparser()
254255

255256

@@ -274,12 +275,18 @@ def make_r2d(argv=None):
274275
args, traitlet_args = argparser.parse_known_args(argv)
275276

276277
r2d = Repo2Docker()
277-
r2d.parse_command_line(traitlet_args)
278278

279279
if args.debug:
280280
r2d.log_level = logging.DEBUG
281281

282+
# load CLI after config file, for correct priority
282283
r2d.load_config_file(args.config)
284+
r2d.parse_command_line(traitlet_args)
285+
286+
if args.debug:
287+
# re-apply debug in case log_level was also set via config
288+
r2d.log_level = logging.DEBUG
289+
283290
if args.appendix:
284291
r2d.appendix = args.appendix
285292

@@ -317,7 +324,8 @@ def make_r2d(argv=None):
317324
# we will pick a name after fetching the repository
318325
r2d.output_image_spec = ""
319326

320-
r2d.json_logs = args.json_logs
327+
if args.json_logs is not None:
328+
r2d.json_logs = args.json_logs
321329

322330
r2d.dry_run = not args.build
323331

@@ -341,29 +349,33 @@ def make_r2d(argv=None):
341349
src, dest = v.split(":")
342350
r2d.volumes[src] = dest
343351

344-
r2d.run_cmd = args.cmd
352+
if args.cmd:
353+
r2d.run_cmd = args.cmd
345354

346355
if args.all_ports and not r2d.run:
347-
print(
348-
"To publish user defined port mappings, the container must " "also be run"
349-
)
356+
print("To publish user-defined port mappings, the container must also be run")
350357
sys.exit(1)
351358

352359
if args.ports and not r2d.run:
353-
print(
354-
"To publish user defined port mappings, the container must " "also be run"
355-
)
360+
print("To publish user-defined port mappings, the container must also be run")
356361
sys.exit(1)
357362

358363
if args.ports and len(args.ports) > 1 and not r2d.run_cmd:
359364
print(
360-
"To publish user defined port mapping, user must specify "
361-
"the command to run in the container"
365+
"To publish user-defined port mapping, "
366+
"you must specify the command to run in the container"
362367
)
363368
sys.exit(1)
364369

365-
r2d.ports = validate_and_generate_port_mapping(args.ports)
366-
r2d.all_ports = args.all_ports
370+
if args.ports:
371+
# override or update, if also defined in config file?
372+
if r2d.ports:
373+
r2d.log.warning(
374+
f"Ignoring port configuration from config (ports={r2d.ports}), overridden by CLI"
375+
)
376+
r2d.ports = validate_and_generate_port_mapping(args.ports)
377+
if args.all_ports is not None:
378+
r2d.all_ports = args.all_ports
367379

368380
if args.user_id:
369381
r2d.user_id = args.user_id
@@ -401,13 +413,15 @@ def make_r2d(argv=None):
401413
if args.engine:
402414
r2d.engine = args.engine
403415

404-
r2d.environment = args.environment
416+
if args.environment:
417+
# extend any environment config from a config file
418+
r2d.environment.extend(args.environment)
405419

406420
# if the source exists locally we don't want to delete it at the end
407421
# FIXME: Find a better way to figure out if repo is 'local'. Push this into ContentProvider?
408422
if os.path.exists(args.repo):
409423
r2d.cleanup_checkout = False
410-
else:
424+
elif args.clean is not None:
411425
r2d.cleanup_checkout = args.clean
412426

413427
if args.target_repo_dir:

repo2docker/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def _user_name_default(self):
332332
)
333333

334334
run = Bool(
335-
False,
335+
True,
336336
help="""
337337
Run docker image after building
338338
""",

0 commit comments

Comments
 (0)