Skip to content

Added "-y" Option to convert command to directly convert the config #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------------

Expand Down
14 changes: 12 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
46 changes: 24 additions & 22 deletions tmuxp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down