diff --git a/CHANGES b/CHANGES index 14e32ff7c89..deb4a391a4d 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,7 @@ Here you can find the recent changes to tmuxp tmuxp 1.5-current ----------------- +- :issue:`589` added option for the the confirm command to auto-confirm the prompt. - :issue:`626` Add new session name option to cli. Thank you @joseph-flinn! - :issue:`626` Add test for new session name option - :issue:`626` Update docs for new session name option diff --git a/README.rst b/README.rst index 48995015ec4..f16f12164f4 100644 --- a/README.rst +++ b/README.rst @@ -111,6 +111,28 @@ Snapshot your tmux layout, pane paths, and window/session names. See more about `freezing tmux`_ sessions. + +Convert a session file +---------------------- + +Convert a session file from yaml to json and vice versa. + +.. code-block:: sh + + $ tmuxp convert filename + +This will prompt you for confirmation and shows you the new file that is going +to be written. + + +You can auto confirm the prompt. In this case no preview will be shown. + +.. code-block:: sh + + $ tmuxp convert -y filename + $ tmuxp convert --yes filename + + Docs / Reading material ----------------------- diff --git a/tests/test_cli.py b/tests/test_cli.py index f1c921f3aed..79d64480002 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -406,7 +406,14 @@ def test_load_zsh_autotitle_warning(cli_args, tmpdir, monkeypatch): assert 'Please set' not in result.output -@pytest.mark.parametrize("cli_args", [(['convert', '.']), (['convert', '.tmuxp.yaml'])]) +@pytest.mark.parametrize( + "cli_args", + [ + (['convert', '.']), + (['convert', '.tmuxp.yaml']), + (['convert', '.tmuxp.yaml', '-y']), + ], +) def test_convert(cli_args, tmpdir, monkeypatch): # create dummy tmuxp yaml so we don't get yelled at tmpdir.join('.tmuxp.yaml').write( @@ -420,7 +427,10 @@ def test_convert(cli_args, tmpdir, monkeypatch): with tmpdir.as_cwd(): runner = CliRunner() - runner.invoke(cli.cli, cli_args, input='y\ny\n') + # If autoconfirm (-y) no need to prompt y + input_args = 'y\ny\n' if '-y' not in cli_args else '' + + runner.invoke(cli.cli, cli_args, input=input_args) assert tmpdir.join('.tmuxp.json').check() assert tmpdir.join('.tmuxp.json').open().read() == json.dumps( {'session_name': 'hello'}, indent=2 diff --git a/tmuxp/cli.py b/tmuxp/cli.py index ecbdeae2439..cc3cd6afebb 100644 --- a/tmuxp/cli.py +++ b/tmuxp/cli.py @@ -915,34 +915,36 @@ def command_import_tmuxinator(configfile): @cli.command(name='convert') +@click.option( + '--yes', '-y', 'confirmed', help='Auto confirms with "yes".', is_flag=True +) @click.argument('config', type=ConfigPath(exists=True), nargs=1) -def command_convert(config): +def command_convert(confirmed, config): """Convert a tmuxp config between JSON and YAML.""" _, ext = os.path.splitext(config) if 'json' in ext: - if click.confirm('convert to <%s> to yaml?' % config): - configparser = kaptan.Kaptan() - configparser.import_config(config) - newfile = config.replace(ext, '.yaml') - newconfig = configparser.export('yaml', indent=2, default_flow_style=False) - if click.confirm('Save config to %s?' % newfile): - buf = open(newfile, 'w') - buf.write(newconfig) - buf.close() - print('New config saved to %s' % newfile) + to_filetype = 'yaml' elif 'yaml' in ext: - if click.confirm('convert to <%s> to json?' % config): - configparser = kaptan.Kaptan() - configparser.import_config(config) - newfile = config.replace(ext, '.json') - newconfig = configparser.export('json', indent=2) - print(newconfig) - if click.confirm('Save config to <%s>?' % newfile): - buf = open(newfile, 'w') - buf.write(newconfig) - buf.close() - print('New config saved to <%s>.' % newfile) + to_filetype = 'json' + + configparser = kaptan.Kaptan() + configparser.import_config(config) + newfile = config.replace(ext, '.%s' % to_filetype) + + export_kwargs = {'default_flow_style': False} if to_filetype == 'yaml' else {} + newconfig = configparser.export(to_filetype, indent=2, **export_kwargs) + + if not confirmed: + if click.confirm('convert to <%s> to %s?' % (config, to_filetype)): + if click.confirm('Save config to %s?' % newfile): + confirmed = True + + if confirmed: + buf = open(newfile, 'w') + buf.write(newconfig) + buf.close() + print('New config saved to <%s>.' % newfile) @cli.command(