Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3abc062

Browse files
committedMar 20, 2022
!squash split them out
1 parent d5dc208 commit 3abc062

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed
 

‎tmuxp/cli/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
from .. import exc
1818
from ..__about__ import __version__
1919
from ..log import setup_logger
20+
from .convert import command_convert
2021
from .debug_info import command_debug_info
2122
from .edit import command_edit_config
2223
from .freeze import command_freeze
23-
from .import_config import command_convert, import_config_cmd
24+
from .import_config import import_config_cmd
2425
from .load import command_load
2526
from .shell import command_shell
2627
from .utils import tmuxp_echo

‎tmuxp/cli/convert.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
3+
import click
4+
import kaptan
5+
6+
from .utils import ConfigPath
7+
8+
9+
@click.command(name="convert")
10+
@click.option(
11+
"--yes", "-y", "confirmed", help='Auto confirms with "yes".', is_flag=True
12+
)
13+
@click.argument("config", type=ConfigPath(exists=True), nargs=1)
14+
def command_convert(confirmed, config):
15+
"""Convert a tmuxp config between JSON and YAML."""
16+
17+
_, ext = os.path.splitext(config)
18+
ext = ext.lower()
19+
if ext == ".json":
20+
to_filetype = "yaml"
21+
elif ext in [".yaml", ".yml"]:
22+
to_filetype = "json"
23+
else:
24+
raise click.BadParameter(
25+
f"Unknown filetype: {ext} (valid: [.json, .yaml, .yml])"
26+
)
27+
28+
configparser = kaptan.Kaptan()
29+
configparser.import_config(config)
30+
newfile = config.replace(ext, ".%s" % to_filetype)
31+
32+
export_kwargs = {"default_flow_style": False} if to_filetype == "yaml" else {}
33+
newconfig = configparser.export(to_filetype, indent=2, **export_kwargs)
34+
35+
if not confirmed:
36+
if click.confirm(f"convert to <{config}> to {to_filetype}?"):
37+
if click.confirm("Save config to %s?" % newfile):
38+
confirmed = True
39+
40+
if confirmed:
41+
buf = open(newfile, "w")
42+
buf.write(newconfig)
43+
buf.close()
44+
print("New config saved to <%s>." % newfile)

0 commit comments

Comments
 (0)
Please sign in to comment.