From c9c29109bffc3872f29d59c803d777961c709a1b Mon Sep 17 00:00:00 2001 From: aRkedos Date: Mon, 17 Aug 2020 18:36:58 +0200 Subject: [PATCH 1/4] rebase to sync up with current upstream master --- tmuxp/cli.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/tmuxp/cli.py b/tmuxp/cli.py index 35374493568..7a68361e6c2 100644 --- a/tmuxp/cli.py +++ b/tmuxp/cli.py @@ -898,12 +898,14 @@ 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 'json' in ext and not confirmed: if click.confirm('convert to <%s> to yaml?' % config): configparser = kaptan.Kaptan() configparser.import_config(config) @@ -914,7 +916,7 @@ def command_convert(config): buf.write(newconfig) buf.close() print('New config saved to %s' % newfile) - elif 'yaml' in ext: + elif 'yaml' in ext and not confirmed: if click.confirm('convert to <%s> to json?' % config): configparser = kaptan.Kaptan() configparser.import_config(config) @@ -926,6 +928,24 @@ def command_convert(config): buf.write(newconfig) buf.close() print('New config saved to <%s>.' % newfile) + elif 'json' in ext and confirmed: + configparser = kaptan.Kaptan() + configparser.import_config(config) + newfile = config.replace(ext, '.yaml') + newconfig = configparser.export('yaml', indent=2, default_flow_style=False) + buf = open(newfile, 'w') + buf.write(newconfig) + buf.close() + print('New config saved to <%s>.' % newfile) + elif 'yaml' in ext and confirmed: + configparser = kaptan.Kaptan() + configparser.import_config(config) + newfile = config.replace(ext, '.json') + newconfig = configparser.export('json', indent=2) + buf = open(newfile, 'w') + buf.write(newconfig) + buf.close() + print('New config saved to <%s>.' % newfile) @cli.command( From 672bacfbcc1976debbf0c4cb73d3bc6a595f4abe Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 19 Sep 2020 07:47:52 -0500 Subject: [PATCH 2/4] Test convert -y --- tests/test_cli.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 1d5882fe5ce..a2060406358 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -387,7 +387,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( @@ -401,7 +408,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 From 28bc3ffdd0f9cfc7805da990cd6009034c373e78 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 19 Sep 2020 07:56:44 -0500 Subject: [PATCH 3/4] Simplify convert --- tmuxp/cli.py | 58 ++++++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/tmuxp/cli.py b/tmuxp/cli.py index 7a68361e6c2..31a8f3075f7 100644 --- a/tmuxp/cli.py +++ b/tmuxp/cli.py @@ -898,50 +898,32 @@ def command_import_tmuxinator(configfile): @cli.command(name='convert') -@click.option('--yes', '-y', 'confirmed', help='Auto confirms with "yes".', - is_flag=True) +@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(confirmed, config): """Convert a tmuxp config between JSON and YAML.""" _, ext = os.path.splitext(config) - if 'json' in ext and not confirmed: - 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 'json' in ext: + to_filetype = 'yaml' + elif 'yaml' in ext: + 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): - buf = open(newfile, 'w') - buf.write(newconfig) - buf.close() - print('New config saved to %s' % newfile) - elif 'yaml' in ext and not confirmed: - 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) - elif 'json' in ext and confirmed: - configparser = kaptan.Kaptan() - configparser.import_config(config) - newfile = config.replace(ext, '.yaml') - newconfig = configparser.export('yaml', indent=2, default_flow_style=False) - buf = open(newfile, 'w') - buf.write(newconfig) - buf.close() - print('New config saved to <%s>.' % newfile) - elif 'yaml' in ext and confirmed: - configparser = kaptan.Kaptan() - configparser.import_config(config) - newfile = config.replace(ext, '.json') - newconfig = configparser.export('json', indent=2) + confirmed = True + + if confirmed: buf = open(newfile, 'w') buf.write(newconfig) buf.close() From aef8b8e79a593e7f8ed143e110f734e068de2631 Mon Sep 17 00:00:00 2001 From: aRkedos Date: Mon, 21 Sep 2020 18:13:32 +0200 Subject: [PATCH 4/4] documented changes in CHANGES and README file --- CHANGES | 1 + README.rst | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/CHANGES b/CHANGES index d91236054ba..985fa2464d5 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,7 @@ Here you can find the recent changes to tmuxp current ------- +- :issue:`589` added option for the the confirm command to auto-confirm the prompt. - :issue:`623` Move docs from RTD to self-serve site - :issue:`623` Modernize Makefiles - :issue:`623` New development docs diff --git a/README.rst b/README.rst index 55391e4da3f..3829fe2b3ac 100644 --- a/README.rst +++ b/README.rst @@ -105,6 +105,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 -----------------------