Skip to content

Commit 1b0ef5a

Browse files
committed
Implementing conflict feedback and resolution for default operating mode for detach and no-tty present.
Signed-off-by: Tully Foote <[email protected]>
1 parent 8a5832f commit 1b0ef5a

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/rocker/cli.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
from .core import RockerExtensionManager
2222
from .core import DependencyMissing
2323
from .core import ExtensionError
24+
from .core import OPERATIONS_DRY_RUN
25+
from .core import OPERATIONS_INTERACTIVE
26+
from .core import OPERATIONS_NON_INTERACTIVE
27+
from .core import OPERATION_MODES
2428

2529
from .os_detector import detect_os
2630

@@ -32,7 +36,7 @@ def main():
3236
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
3337
parser.add_argument('image')
3438
parser.add_argument('command', nargs='*', default='')
35-
parser.add_argument('--noexecute', action='store_true', help='Deprecated')
39+
parser.add_argument('--noexecute', action='store_true', help='Deprecated') # TODO(tfoote) add when 3.13 is minimum supported, deprecated=True
3640
parser.add_argument('--nocache', action='store_true')
3741
parser.add_argument('--nocleanup', action='store_true', help='do not remove the docker container when stopped')
3842
parser.add_argument('--persist-image', action='store_true', help='do not remove the docker image when stopped', default=False) #TODO(tfoote) Add a name to it if persisting
@@ -56,6 +60,34 @@ def main():
5660
args_dict['mode'] = OPERATIONS_DRY_RUN
5761
print('DEPRECATION Warning: --noexecute is deprecated for --mode dry-run please switch your usage by December 2020')
5862

63+
# validate_operating_mode
64+
operating_mode = args_dict.get('mode')
65+
# Don't try to be interactive if there's no tty
66+
if not os.isatty(sys.__stdin__.fileno()):
67+
if operating_mode == OPERATIONS_INTERACTIVE:
68+
parser.error("No tty detected cannot operate in interactive mode")
69+
elif not operating_mode:
70+
print("No tty detected for stdin defaulting mode to non-interactive")
71+
args_dict['mode'] = OPERATIONS_NON_INTERACTIVE
72+
73+
# Check if detach extension is active and deconflict with interactive
74+
detach_active = args_dict.get('detach')
75+
operating_mode = args_dict.get('mode')
76+
if detach_active:
77+
if operating_mode == OPERATIONS_INTERACTIVE:
78+
parser.error("Command line option --mode=interactive and --detach are mutually exclusive")
79+
elif not operating_mode:
80+
print(f"Detach extension active, defaulting mode to {OPERATIONS_NON_INTERACTIVE}")
81+
args_dict['mode'] = OPERATIONS_NON_INTERACTIVE
82+
# TODO(tfoote) Deal with the case of dry-run + detach
83+
# Right now the printed results will include '-it'
84+
# But based on testing the --detach overrides -it in docker so it's ok.
85+
86+
# Default to non-interactive if unset
87+
if args_dict.get('mode') not in OPERATION_MODES:
88+
print("Mode unset, defaulting to interactive")
89+
args_dict['mode'] = OPERATIONS_NON_INTERACTIVE
90+
5991
try:
6092
active_extensions = extension_manager.get_active_extensions(args_dict)
6193
except ExtensionError as e:

src/rocker/core.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ def extend_cli_parser(self, parser, default_args={}):
134134
print("Extension %s doesn't support default arguments. Please extend it." % p.get_name())
135135
p.register_arguments(parser)
136136
parser.add_argument('--mode', choices=OPERATION_MODES,
137-
default=OPERATIONS_INTERACTIVE,
138-
help="Choose mode of operation for rocker")
137+
help="Choose mode of operation for rocker, default interactive unless detached.")
139138
parser.add_argument('--image-name', default=None,
140139
help='Tag the final image, useful with dry-run')
141140
parser.add_argument('--extension-blacklist', nargs='*',
@@ -361,9 +360,6 @@ def get_operating_mode(self, args):
361360
# Default to non-interactive if unset
362361
if operating_mode not in OPERATION_MODES:
363362
operating_mode = OPERATIONS_NON_INTERACTIVE
364-
if operating_mode == OPERATIONS_INTERACTIVE and not os.isatty(sys.__stdin__.fileno()):
365-
operating_mode = OPERATIONS_NON_INTERACTIVE
366-
print("No tty detected for stdin forcing non-interactive")
367363
return operating_mode
368364

369365
def generate_docker_cmd(self, command='', **kwargs):

0 commit comments

Comments
 (0)