Skip to content

Commit cb1ee4d

Browse files
authored
Allow passing -y to convert (#621)
Added "-y" Option to convert command to directly convert the config Thank you @aRkedos!
2 parents 7db5972 + d210873 commit cb1ee4d

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Here you can find the recent changes to tmuxp
66

77
tmuxp 1.5-current
88
-----------------
9+
- :issue:`589` added option for the the confirm command to auto-confirm the prompt.
910
- :issue:`626` Add new session name option to cli. Thank you @joseph-flinn!
1011
- :issue:`626` Add test for new session name option
1112
- :issue:`626` Update docs for new session name option

README.rst

+22
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ Snapshot your tmux layout, pane paths, and window/session names.
111111
112112
See more about `freezing tmux`_ sessions.
113113

114+
115+
Convert a session file
116+
----------------------
117+
118+
Convert a session file from yaml to json and vice versa.
119+
120+
.. code-block:: sh
121+
122+
$ tmuxp convert filename
123+
124+
This will prompt you for confirmation and shows you the new file that is going
125+
to be written.
126+
127+
128+
You can auto confirm the prompt. In this case no preview will be shown.
129+
130+
.. code-block:: sh
131+
132+
$ tmuxp convert -y filename
133+
$ tmuxp convert --yes filename
134+
135+
114136
Docs / Reading material
115137
-----------------------
116138

tests/test_cli.py

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

408408

409-
@pytest.mark.parametrize("cli_args", [(['convert', '.']), (['convert', '.tmuxp.yaml'])])
409+
@pytest.mark.parametrize(
410+
"cli_args",
411+
[
412+
(['convert', '.']),
413+
(['convert', '.tmuxp.yaml']),
414+
(['convert', '.tmuxp.yaml', '-y']),
415+
],
416+
)
410417
def test_convert(cli_args, tmpdir, monkeypatch):
411418
# create dummy tmuxp yaml so we don't get yelled at
412419
tmpdir.join('.tmuxp.yaml').write(
@@ -420,7 +427,10 @@ def test_convert(cli_args, tmpdir, monkeypatch):
420427
with tmpdir.as_cwd():
421428
runner = CliRunner()
422429

423-
runner.invoke(cli.cli, cli_args, input='y\ny\n')
430+
# If autoconfirm (-y) no need to prompt y
431+
input_args = 'y\ny\n' if '-y' not in cli_args else ''
432+
433+
runner.invoke(cli.cli, cli_args, input=input_args)
424434
assert tmpdir.join('.tmuxp.json').check()
425435
assert tmpdir.join('.tmuxp.json').open().read() == json.dumps(
426436
{'session_name': 'hello'}, indent=2

tmuxp/cli.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -915,34 +915,36 @@ def command_import_tmuxinator(configfile):
915915

916916

917917
@cli.command(name='convert')
918+
@click.option(
919+
'--yes', '-y', 'confirmed', help='Auto confirms with "yes".', is_flag=True
920+
)
918921
@click.argument('config', type=ConfigPath(exists=True), nargs=1)
919-
def command_convert(config):
922+
def command_convert(confirmed, config):
920923
"""Convert a tmuxp config between JSON and YAML."""
921924

922925
_, ext = os.path.splitext(config)
923926
if 'json' in ext:
924-
if click.confirm('convert to <%s> to yaml?' % config):
925-
configparser = kaptan.Kaptan()
926-
configparser.import_config(config)
927-
newfile = config.replace(ext, '.yaml')
928-
newconfig = configparser.export('yaml', indent=2, default_flow_style=False)
929-
if click.confirm('Save config to %s?' % newfile):
930-
buf = open(newfile, 'w')
931-
buf.write(newconfig)
932-
buf.close()
933-
print('New config saved to %s' % newfile)
927+
to_filetype = 'yaml'
934928
elif 'yaml' in ext:
935-
if click.confirm('convert to <%s> to json?' % config):
936-
configparser = kaptan.Kaptan()
937-
configparser.import_config(config)
938-
newfile = config.replace(ext, '.json')
939-
newconfig = configparser.export('json', indent=2)
940-
print(newconfig)
941-
if click.confirm('Save config to <%s>?' % newfile):
942-
buf = open(newfile, 'w')
943-
buf.write(newconfig)
944-
buf.close()
945-
print('New config saved to <%s>.' % newfile)
929+
to_filetype = 'json'
930+
931+
configparser = kaptan.Kaptan()
932+
configparser.import_config(config)
933+
newfile = config.replace(ext, '.%s' % to_filetype)
934+
935+
export_kwargs = {'default_flow_style': False} if to_filetype == 'yaml' else {}
936+
newconfig = configparser.export(to_filetype, indent=2, **export_kwargs)
937+
938+
if not confirmed:
939+
if click.confirm('convert to <%s> to %s?' % (config, to_filetype)):
940+
if click.confirm('Save config to %s?' % newfile):
941+
confirmed = True
942+
943+
if confirmed:
944+
buf = open(newfile, 'w')
945+
buf.write(newconfig)
946+
buf.close()
947+
print('New config saved to <%s>.' % newfile)
946948

947949

948950
@cli.command(

0 commit comments

Comments
 (0)