Skip to content

Commit 59bf783

Browse files
committed
Workflow for importing config files from tmuxinator and teamocil
1 parent 66a655b commit 59bf783

File tree

1 file changed

+102
-16
lines changed

1 file changed

+102
-16
lines changed

tmuxp/cli.py

Lines changed: 102 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def prompt_bool(name, default=False, yes_choices=None, no_choices=None):
9292
def prompt_yes_no(name, default=True):
9393
return prompt_bool(name, default=default)
9494

95+
9596
def prompt_choices(name, choices, default=None, resolve=ascii_lowercase,
9697
no_choice=('none',)):
9798
"""
@@ -254,12 +255,13 @@ def load_workspace(config_file, args):
254255

255256
if 'TMUX' in os.environ:
256257
del os.environ['TMUX']
257-
os.execl(tmux_bin, 'tmux', 'switch-client', '-t', sconfig[
258-
'session_name'])
258+
os.execl(tmux_bin, 'tmux', 'switch-client', '-t',
259+
sconfig['session_name'])
259260

260261
if attach_session:
261-
os.execl(tmux_bin, 'tmux', 'attach-session', '-t', sconfig[
262-
'session_name'])
262+
print(sconfig['sesson_name'])
263+
os.execl(tmux_bin, 'tmux', 'attach-session', '-t',
264+
sconfig['session_name'])
263265
return
264266

265267

@@ -334,18 +336,60 @@ def command_import_teamocil(args):
334336

335337
print(output)
336338
elif args.config:
337-
configfile = os.path.relpath(args.config)
339+
configfile = os.path.abspath(os.path.relpath(args.config))
338340
configparser = kaptan.Kaptan(handler='yaml')
339-
configparser.import_config(configfile)
341+
342+
if os.path.exists(configfile):
343+
print(configfile)
344+
configparser.import_config(configfile)
345+
else:
346+
sys.exit('File not found: %s' % configfile)
340347

341348
newconfig = config.import_teamocil(configparser.get())
342349

343-
newconfig = configparser.import_config(newconfig)
344-
newconfig = configparser.export(
345-
'yaml', indent=2, default_flow_style=False
346-
)
350+
config_format = prompt_choices('Convert to', choices=[
351+
'yaml', 'json'], default='yaml')
352+
353+
if config_format == 'yaml':
354+
newconfig = configparser.export(
355+
'yaml', indent=2, default_flow_style=False
356+
)
357+
elif config_format == 'json':
358+
newconfig = configparser.export('json', indent=2)
359+
else:
360+
sys.exit('Unknown config format.')
347361

348362
print(newconfig)
363+
print(
364+
'---------------------------------------------------------------')
365+
print(
366+
'Configuration import does its best to convert teamocil files.\n')
367+
if prompt_yes_no(
368+
'The new config *WILL* require adjusting afterwards. Save config?'
369+
):
370+
dest = None
371+
while not dest:
372+
dest_prompt = prompt('Save to: ', os.path.abspath(
373+
os.path.join(config_dir, 'myimport.%s' % config_format)))
374+
if os.path.exists(dest_prompt):
375+
print('%s exists. Pick a new filename.' % dest_prompt)
376+
continue
377+
378+
dest = dest_prompt
379+
380+
dest = os.path.abspath(os.path.relpath(dest))
381+
if prompt_yes_no('Write to %s?' % dest):
382+
buf = open(dest, 'w')
383+
buf.write(newconfig)
384+
buf.close()
385+
386+
print('Saved to %s.' % dest)
387+
else:
388+
print(
389+
'tmuxp has examples in JSON and YAML format at <http://tmuxp.readthedocs.org/en/latest/examples.html>\n'
390+
'View tmuxp docs at <http://tmuxp.readthedocs.org/>'
391+
)
392+
sys.exit()
349393

350394

351395
def command_import_tmuxinator(args):
@@ -378,18 +422,60 @@ def command_import_tmuxinator(args):
378422
print(output)
379423

380424
if args.config:
381-
configfile = os.path.relpath(args.config)
425+
configfile = os.path.abspath(os.path.relpath(args.config))
382426
configparser = kaptan.Kaptan(handler='yaml')
383-
configparser.import_config(configfile)
427+
428+
if os.path.exists(configfile):
429+
print(configfile)
430+
configparser.import_config(configfile)
431+
else:
432+
sys.exit('File not found: %s' % configfile)
384433

385434
newconfig = config.import_tmuxinator(configparser.get())
386435

387-
newconfig = configparser.import_config(newconfig)
388-
newconfig = configparser.export(
389-
'yaml', indent=2, default_flow_style=False
390-
)
436+
config_format = prompt_choices('Convert to', choices=[
437+
'yaml', 'json'], default='yaml')
438+
439+
if config_format == 'yaml':
440+
newconfig = configparser.export(
441+
'yaml', indent=2, default_flow_style=False
442+
)
443+
elif config_format == 'json':
444+
newconfig = configparser.export('json', indent=2)
445+
else:
446+
sys.exit('Unknown config format.')
391447

392448
print(newconfig)
449+
print(
450+
'---------------------------------------------------------------')
451+
print(
452+
'Configuration import does its best to convert teamocil files.\n')
453+
if prompt_yes_no(
454+
'The new config *WILL* require adjusting afterwards. Save config?'
455+
):
456+
dest = None
457+
while not dest:
458+
dest_prompt = prompt('Save to: ', os.path.abspath(
459+
os.path.join(config_dir, 'myimport.%s' % config_format)))
460+
if os.path.exists(dest_prompt):
461+
print('%s exists. Pick a new filename.' % dest_prompt)
462+
continue
463+
464+
dest = dest_prompt
465+
466+
dest = os.path.abspath(os.path.relpath(dest))
467+
if prompt_yes_no('Write to %s?' % dest):
468+
buf = open(dest, 'w')
469+
buf.write(newconfig)
470+
buf.close()
471+
472+
print('Saved to %s.' % dest)
473+
else:
474+
print(
475+
'tmuxp has examples in JSON and YAML format at <http://tmuxp.readthedocs.org/en/latest/examples.html>\n'
476+
'View tmuxp docs at <http://tmuxp.readthedocs.org/>'
477+
)
478+
sys.exit()
393479

394480

395481
def command_convert(args):

0 commit comments

Comments
 (0)