Skip to content

Commit 3f4f3ae

Browse files
committed
Merge branch 'confirm-convert' of github.com:aRkedos/tmuxp into confirm-convert
2 parents aef8b8e + 28bc3ff commit 3f4f3ae

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

tests/test_cli.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,14 @@ def test_load_zsh_autotitle_warning(cli_args, tmpdir, monkeypatch):
387387
assert 'Please set' not in result.output
388388

389389

390-
@pytest.mark.parametrize("cli_args", [(['convert', '.']), (['convert', '.tmuxp.yaml'])])
390+
@pytest.mark.parametrize(
391+
"cli_args",
392+
[
393+
(['convert', '.']),
394+
(['convert', '.tmuxp.yaml']),
395+
(['convert', '.tmuxp.yaml', '-y']),
396+
],
397+
)
391398
def test_convert(cli_args, tmpdir, monkeypatch):
392399
# create dummy tmuxp yaml so we don't get yelled at
393400
tmpdir.join('.tmuxp.yaml').write(
@@ -401,7 +408,10 @@ def test_convert(cli_args, tmpdir, monkeypatch):
401408
with tmpdir.as_cwd():
402409
runner = CliRunner()
403410

404-
runner.invoke(cli.cli, cli_args, input='y\ny\n')
411+
# If autoconfirm (-y) no need to prompt y
412+
input_args = 'y\ny\n' if '-y' not in cli_args else ''
413+
414+
runner.invoke(cli.cli, cli_args, input=input_args)
405415
assert tmpdir.join('.tmuxp.json').check()
406416
assert tmpdir.join('.tmuxp.json').open().read() == json.dumps(
407417
{'session_name': 'hello'}, indent=2

tmuxp/cli.py

+20-38
Original file line numberDiff line numberDiff line change
@@ -898,50 +898,32 @@ def command_import_tmuxinator(configfile):
898898

899899

900900
@cli.command(name='convert')
901-
@click.option('--yes', '-y', 'confirmed', help='Auto confirms with "yes".',
902-
is_flag=True)
901+
@click.option(
902+
'--yes', '-y', 'confirmed', help='Auto confirms with "yes".', is_flag=True
903+
)
903904
@click.argument('config', type=ConfigPath(exists=True), nargs=1)
904905
def command_convert(confirmed, config):
905906
"""Convert a tmuxp config between JSON and YAML."""
906907

907908
_, ext = os.path.splitext(config)
908-
if 'json' in ext and not confirmed:
909-
if click.confirm('convert to <%s> to yaml?' % config):
910-
configparser = kaptan.Kaptan()
911-
configparser.import_config(config)
912-
newfile = config.replace(ext, '.yaml')
913-
newconfig = configparser.export('yaml', indent=2, default_flow_style=False)
909+
if 'json' in ext:
910+
to_filetype = 'yaml'
911+
elif 'yaml' in ext:
912+
to_filetype = 'json'
913+
914+
configparser = kaptan.Kaptan()
915+
configparser.import_config(config)
916+
newfile = config.replace(ext, '.%s' % to_filetype)
917+
918+
export_kwargs = {'default_flow_style': False} if to_filetype == 'yaml' else {}
919+
newconfig = configparser.export(to_filetype, indent=2, **export_kwargs)
920+
921+
if not confirmed:
922+
if click.confirm('convert to <%s> to %s?' % (config, to_filetype)):
914923
if click.confirm('Save config to %s?' % newfile):
915-
buf = open(newfile, 'w')
916-
buf.write(newconfig)
917-
buf.close()
918-
print('New config saved to %s' % newfile)
919-
elif 'yaml' in ext and not confirmed:
920-
if click.confirm('convert to <%s> to json?' % config):
921-
configparser = kaptan.Kaptan()
922-
configparser.import_config(config)
923-
newfile = config.replace(ext, '.json')
924-
newconfig = configparser.export('json', indent=2)
925-
print(newconfig)
926-
if click.confirm('Save config to <%s>?' % newfile):
927-
buf = open(newfile, 'w')
928-
buf.write(newconfig)
929-
buf.close()
930-
print('New config saved to <%s>.' % newfile)
931-
elif 'json' in ext and confirmed:
932-
configparser = kaptan.Kaptan()
933-
configparser.import_config(config)
934-
newfile = config.replace(ext, '.yaml')
935-
newconfig = configparser.export('yaml', indent=2, default_flow_style=False)
936-
buf = open(newfile, 'w')
937-
buf.write(newconfig)
938-
buf.close()
939-
print('New config saved to <%s>.' % newfile)
940-
elif 'yaml' in ext and confirmed:
941-
configparser = kaptan.Kaptan()
942-
configparser.import_config(config)
943-
newfile = config.replace(ext, '.json')
944-
newconfig = configparser.export('json', indent=2)
924+
confirmed = True
925+
926+
if confirmed:
945927
buf = open(newfile, 'w')
946928
buf.write(newconfig)
947929
buf.close()

0 commit comments

Comments
 (0)