Skip to content

Commit 2c3850e

Browse files
dnaauntony
authored andcommitted
Allow specifying file name and format as cmdline arguments for tmuxp freeze, deduce format from filename
1 parent d2c1b56 commit 2c3850e

File tree

1 file changed

+79
-43
lines changed

1 file changed

+79
-43
lines changed

tmuxp/cli.py

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,25 @@ def command_shell(
911911
@click.argument('session_name', nargs=1, required=False)
912912
@click.option('-S', 'socket_path', help='pass-through for tmux -S')
913913
@click.option('-L', 'socket_name', help='pass-through for tmux -L')
914+
@click.option(
915+
'-f',
916+
'--config-format',
917+
type=click.Choice(['yaml', 'json']),
918+
help='format to save in',
919+
)
920+
@click.option('-o', '--save-to', type=click.Path(exists=False), help='file to save to')
921+
@click.option(
922+
'-y',
923+
'--yes',
924+
type=bool,
925+
is_flag=True,
926+
default=False,
927+
help="Don't prompt for confirmation",
928+
)
914929
@click.option('--force', 'force', help='overwrite the config file', is_flag=True)
915-
def command_freeze(session_name, socket_name, socket_path, force):
930+
def command_freeze(
931+
session_name, socket_name, config_format, save_to, socket_path, yes, force
932+
):
916933
"""Snapshot a session into a config.
917934
918935
If SESSION_NAME is provided, snapshot that session. Otherwise, use the
@@ -936,62 +953,81 @@ def command_freeze(session_name, socket_name, socket_path, force):
936953
configparser = kaptan.Kaptan()
937954
newconfig = config.inline(sconf)
938955
configparser.import_config(newconfig)
939-
config_format = click.prompt(
940-
'Convert to', value_proc=_validate_choices(['yaml', 'json']), default='yaml'
941-
)
942-
943-
if config_format == 'yaml':
944-
newconfig = configparser.export(
945-
'yaml', indent=2, default_flow_style=False, safe=True
946-
)
947-
elif config_format == 'json':
948-
newconfig = configparser.export('json', indent=2)
949-
else:
950-
sys.exit('Unknown config format.')
951956

952957
print(
953958
'---------------------------------------------------------------'
954959
'\n'
955960
'Freeze does its best to snapshot live tmux sessions.\n'
956961
)
957-
if click.confirm(
958-
'The new config *WILL* require adjusting afterwards. Save config?'
962+
if not (
963+
yes
964+
or click.confirm(
965+
'The new config *WILL* require adjusting afterwards. Save config?'
966+
)
959967
):
960-
dest = None
961-
while not dest:
962-
save_to = os.path.abspath(
963-
os.path.join(
964-
get_config_dir(),
965-
'%s.%s' % (sconf.get('session_name'), config_format),
966-
)
967-
)
968-
dest_prompt = click.prompt(
969-
'Path to new config:', value_proc=get_abs_path, default=save_to
970-
)
971-
if not force and os.path.exists(dest_prompt):
972-
print('%s exists. Pick a new filename.' % dest_prompt)
973-
continue
974-
975-
dest = dest_prompt
976-
977-
dest = os.path.abspath(os.path.relpath(os.path.expanduser(dest)))
978-
if click.confirm('Save to %s?' % dest):
979-
destdir = os.path.dirname(dest)
980-
if not os.path.isdir(destdir):
981-
os.makedirs(destdir)
982-
buf = open(dest, 'w')
983-
buf.write(newconfig)
984-
buf.close()
985-
986-
print('Saved to %s.' % dest)
987-
else:
988968
print(
989969
'tmuxp has examples in JSON and YAML format at '
990970
'<http://tmuxp.git-pull.com/examples.html>\n'
991971
'View tmuxp docs at <http://tmuxp.git-pull.com/>.'
992972
)
993973
sys.exit()
994974

975+
dest = save_to
976+
while not dest:
977+
save_to = os.path.abspath(
978+
os.path.join(
979+
get_config_dir(),
980+
'%s.%s' % (sconf.get('session_name'), config_format or 'yaml'),
981+
)
982+
)
983+
dest_prompt = click.prompt(
984+
'Save to: %s' % save_to, value_proc=get_abs_path, default=save_to
985+
)
986+
if not force and os.path.exists(dest_prompt):
987+
print('%s exists. Pick a new filename.' % dest_prompt)
988+
continue
989+
990+
dest = dest_prompt
991+
dest = os.path.abspath(os.path.relpath(os.path.expanduser(dest)))
992+
993+
if config_format is None:
994+
valid_config_formats = ['json', 'yaml']
995+
_, config_format = os.path.splitext(dest)
996+
config_format = config_format[1:].lower()
997+
if config_format not in valid_config_formats:
998+
config_format = click.prompt(
999+
"Couldn't ascertain one of [%s] from file name. Convert to"
1000+
% ", ".join(valid_config_formats),
1001+
value_proc=_validate_choices(['yaml', 'json']),
1002+
default='yaml',
1003+
)
1004+
1005+
if config_format == 'yaml':
1006+
newconfig = configparser.export(
1007+
'yaml', indent=2, default_flow_style=False, safe=True
1008+
)
1009+
elif config_format == 'json':
1010+
newconfig = configparser.export('json', indent=2)
1011+
1012+
if yes or click.confirm('Save to %s?' % dest):
1013+
destdir = os.path.dirname(dest)
1014+
if not os.path.isdir(destdir):
1015+
os.makedirs(destdir)
1016+
buf = open(dest, 'w')
1017+
buf.write(newconfig)
1018+
buf.close()
1019+
1020+
print('Saved to %s.' % dest)
1021+
1022+
if config_format == 'yaml':
1023+
newconfig = configparser.export(
1024+
'yaml', indent=2, default_flow_style=False, safe=True
1025+
)
1026+
elif config_format == 'json':
1027+
newconfig = configparser.export('json', indent=2)
1028+
else:
1029+
sys.exit('Unknown config format.')
1030+
9951031

9961032
@cli.command(name='load', short_help='Load tmuxp workspaces.')
9971033
@click.pass_context

0 commit comments

Comments
 (0)