-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathcli.py
962 lines (771 loc) · 28.7 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
# -*- coding: utf8 - *-
"""Command line tool for managing tmux workspaces and tmuxp configurations.
tmuxp.cli
~~~~~~~~~
:copyright: Copyright 2013 Tony Narlock.
:license: BSD, see LICENSE for details
"""
import os
import sys
import argparse
import argcomplete
import logging
import kaptan
from . import log, util, exc, WorkspaceBuilder, Server, config
from .util import ascii_lowercase, input
from .workspacebuilder import freeze
from distutils.util import strtobool
import re
VERSIONFILE = os.path.join(os.path.abspath(os.path.dirname(__file__)), '__init__.py')
verstrline = open(VERSIONFILE, "rt").read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, verstrline, re.M)
if mo:
__version__ = mo.group(1)
#import pkg_resources
#__version__ = pkg_resources.require("tmuxp")[0].version
logger = logging.getLogger(__name__)
config_dir = os.path.expanduser('~/.tmuxp/')
cwd_dir = os.getcwd() + '/'
tmuxinator_config_dir = os.path.expanduser('~/.tmuxinator/')
teamocil_config_dir = os.path.expanduser('~/.teamocil/')
def prompt(name, default=None):
"""Return user input from command line.
:meth:`~prompt`, :meth:`~prompt_bool` and :meth:`prompt_choices` are from
`flask-script`_. See the `flask-script license`_.
.. _flask-script: https://github.com/techniq/flask-script
.. _flask-script license:
https://github.com/techniq/flask-script/blob/master/LICENSE
:param name: prompt text
:param default: default value if no input provided.
:rtype: string
"""
prompt = name + (default and ' [%s]' % default or '')
prompt += name.endswith('?') and ' ' or ': '
while True:
rv = input(prompt)
if rv:
return rv
if default is not None:
return default
def prompt_bool(name, default=False, yes_choices=None, no_choices=None):
"""Return user input from command line and converts to boolean value.
:param name: prompt text
:param default: default value if no input provided.
:param yes_choices: default 'y', 'yes', '1', 'on', 'true', 't'
:param no_choices: default 'n', 'no', '0', 'off', 'false', 'f'
:rtype: bool
"""
yes_choices = yes_choices or ('y', 'yes', '1', 'on', 'true', 't')
no_choices = no_choices or ('n', 'no', '0', 'off', 'false', 'f')
if default is None:
prompt_choice = 'y/n'
elif default is True:
prompt_choice = 'Y/n'
else:
prompt_choice = 'y/N'
prompt = name + ' [%s]' % prompt_choice
prompt += name.endswith('?') and ' ' or ': '
while True:
rv = input(prompt)
if not rv:
return default
if rv.lower() in yes_choices:
return True
elif rv.lower() in no_choices:
return False
def prompt_yes_no(name, default=True):
""":meth:`prompt_bool()` returning yes by default."""
return prompt_bool(name, default=default)
def prompt_choices(name, choices, default=None, resolve=ascii_lowercase,
no_choice=('none',)):
"""Return user input from command line from set of provided choices.
:param name: prompt text
:param choices: list or tuple of available choices. Choices may be
single strings or (key, value) tuples.
:param default: default value if no input provided.
:param no_choice: acceptable list of strings for "null choice"
:rtype: str
"""
_choices = []
options = []
for choice in choices:
if isinstance(choice, basestring):
options.append(choice)
else:
options.append("%s [%s]" % (choice[1], choice[0]))
choice = choice[0]
_choices.append(choice)
while True:
rv = prompt(name + ' - (%s)' % ', '.join(options), default)
if not rv:
return default
rv = resolve(rv)
if rv in no_choice:
return None
if rv in _choices:
return rv
class ConfigFileCompleter(argcomplete.completers.FilesCompleter):
"""argcomplete completer for tmuxp files."""
def __call__(self, prefix, **kwargs):
completion = argcomplete.completers.FilesCompleter.__call__(
self, prefix, **kwargs
)
completion += [os.path.join(config_dir, c)
for c in config.in_dir(config_dir)]
return completion
class TmuxinatorCompleter(argcomplete.completers.FilesCompleter):
"""argcomplete completer for Tmuxinator files."""
def __call__(self, prefix, **kwargs):
completion = argcomplete.completers.FilesCompleter.__call__(
self, prefix, **kwargs
)
tmuxinator_configs = config.in_dir(
tmuxinator_config_dir, extensions='yml'
)
completion += [
os.path.join(tmuxinator_config_dir, f)
for f in tmuxinator_configs
]
return completion
class TeamocilCompleter(argcomplete.completers.FilesCompleter):
"""argcomplete completer for Teamocil files."""
def __call__(self, prefix, **kwargs):
completion = argcomplete.completers.FilesCompleter.__call__(
self, prefix, **kwargs
)
teamocil_configs = config.in_dir(teamocil_config_dir, extensions='yml')
completion += [
os.path.join(teamocil_config_dir, f)
for f in teamocil_configs
]
return completion
def SessionCompleter(prefix, parsed_args, **kwargs):
"""Return list of session names for argcomplete completer."""
t = Server(
socket_name=parsed_args.socket_name,
socket_path=parsed_args.socket_path
)
sessions_available = [
s.get('session_name') for s in t._sessions
if s.get('session_name').startswith(' '.join(prefix))
]
if parsed_args.session_name and sessions_available:
return []
return [
s.get('session_name') for s in t._sessions
if s.get('session_name').startswith(prefix)
]
def setup_logger(logger=None, level='INFO'):
"""Setup logging for CLI use.
:param logger: instance of logger
:type logger: :py:class:`Logger`
"""
if not logger:
logger = logging.getLogger()
if not logger.handlers:
channel = logging.StreamHandler()
channel.setFormatter(log.DebugLogFormatter())
# channel.setFormatter(log.LogFormatter())
logger.setLevel(level)
logger.addHandler(channel)
def startup(config_dir):
"""Initialize CLI.
:param config_dir: Config directory to search
:type config_dir: string
"""
if not os.path.exists(config_dir):
os.makedirs(config_dir)
def load_workspace(config_file, args):
"""Build config workspace.
:param config_file: full path to config file
:param type: string
"""
logger.info('Loading %s.' % config_file)
sconfig = kaptan.Kaptan()
sconfig = sconfig.import_config(config_file).get()
sconfig = config.expand(sconfig, os.path.dirname(config_file))
sconfig = config.trickle(sconfig)
t = Server(
socket_name=args.socket_name,
socket_path=args.socket_path,
colors=args.colors
)
try:
builder = WorkspaceBuilder(sconf=sconfig, server=t)
except exc.EmptyConfigException:
logger.error('%s is empty or parsed no config data' % config_file)
return
tmux_bin = util.which('tmux')
try:
builder.build()
if 'TMUX' in os.environ:
if args.answer_yes or prompt_yes_no('Already inside TMUX, switch to session?'):
tmux_env = os.environ.pop('TMUX')
builder.session.switch_client()
os.environ['TMUX'] = tmux_env
return
else:
sys.exit('Session created in detached state.')
builder.session.attach_session()
except exc.TmuxSessionExists as e:
if args.answer_yes or prompt_yes_no('%s Attach?' % e):
if 'TMUX' in os.environ:
builder.session.switch_client()
else:
builder.session.attach_session()
return
except exc.TmuxpException as e:
import traceback
print(traceback.format_exc())
logger.error(e)
choice = prompt_choices(
'Error loading workspace. (k)ill, (a)ttach, (d)etach?',
choices=['k', 'a', 'd'],
default='k'
)
if choice == 'k':
builder.session.kill_session()
print('Session killed.')
elif choice == 'a':
if 'TMUX' in os.environ:
builder.session.switch_client()
else:
builder.session.attach_session()
else:
sys.exit()
def command_freeze(args):
"""Import teamocil config to tmuxp format."""
ctext = ' '.join(args.session_name)
t = Server(
socket_name=args.socket_name,
socket_path=args.socket_path,
colors=args.colors
)
session = t.findWhere({
'session_name': ctext
})
sconf = freeze(session)
configparser = kaptan.Kaptan()
newconfig = config.inline(sconf)
configparser.import_config(newconfig)
config_format = prompt_choices('Convert to', choices=[
'yaml', 'json'], default='yaml')
if config_format == 'yaml':
newconfig = configparser.export(
'yaml', indent=2, default_flow_style=False, safe=True
)
elif config_format == 'json':
newconfig = configparser.export('json', indent=2)
else:
sys.exit('Unknown config format.')
print(newconfig)
print(
'---------------------------------------------------------------')
print(
'Configuration import does its best to convert teamocil files.\n')
if args.answer_yes or prompt_yes_no(
'The new config *WILL* require adjusting afterwards. Save config?'
):
dest = None
while not dest:
dest_prompt = prompt('Save to: ', os.path.abspath(
os.path.join(config_dir, '%s.%s' % (sconf.get('session_name'), config_format))))
if os.path.exists(dest_prompt):
print('%s exists. Pick a new filename.' % dest_prompt)
continue
dest = dest_prompt
dest = os.path.abspath(os.path.relpath(os.path.expanduser(dest)))
if args.answer_yes or prompt_yes_no('Write to %s?' % dest):
buf = open(dest, 'w')
buf.write(newconfig)
buf.close()
print('Saved to %s.' % dest)
else:
print(
'tmuxp has examples in JSON and YAML format at <http://tmuxp.readthedocs.org/en/latest/examples.html>\n'
'View tmuxp docs at <http://tmuxp.readthedocs.org/>'
)
sys.exit()
def command_load(args):
"""Load a session from a tmuxp session file."""
if args.list:
startup(config_dir)
configs_in_user = config.in_dir(config_dir)
configs_in_cwd = config.in_cwd()
sys.exit()
output = ''
if not configs_in_user:
output += '# %s: \n\tNone found.\n' % config_dir
else:
output += '# %s: \n\t%s\n' % (
config_dir, ', '.join(configs_in_user)
)
if configs_in_cwd:
output += '# current directory:\n\t%s' % (
', '.join(configs_in_cwd)
)
print(output)
return
elif args.config:
if '.' == args.config:
if config.in_cwd():
configfile = config.in_cwd()[0]
print(configfile)
else:
sys.exit('No tmuxp configs found in current directory.')
else:
configfile = args.config
file_user = os.path.join(config_dir, configfile)
file_cwd = os.path.join(cwd_dir, configfile)
if os.path.exists(file_cwd) and os.path.isfile(file_cwd):
print('load %s' % file_cwd)
load_workspace(file_cwd, args)
elif os.path.exists(file_user) and os.path.isfile(file_user):
load_workspace(file_user, args)
else:
logger.error('%s not found.' % configfile)
def command_import_teamocil(args):
"""Import teamocil config to tmuxp format."""
if args.list:
try:
configs_in_user = config.in_dir(
teamocil_config_dir, extensions='yml')
except OSError:
configs_in_user = []
configs_in_cwd = config.in_dir(
config_dir=cwd_dir, extensions='yml')
output = ''
if not os.path.exists(teamocil_config_dir):
output += '# %s: \n\tDirectory doesn\'t exist.\n' % teamocil_config_dir
elif not configs_in_user:
output += '# %s: \n\tNone found.\n' % teamocil_config_dir
else:
output += '# %s: \n\t%s\n' % (
config_dir, ', '.join(configs_in_user)
)
if configs_in_cwd:
output += '# current directory:\n\t%s' % (
', '.join(configs_in_cwd)
)
print(output)
elif args.config:
configfile = os.path.abspath(os.path.relpath(
os.path.expanduser(args.config)))
configparser = kaptan.Kaptan(handler='yaml')
if os.path.exists(configfile):
print(configfile)
configparser.import_config(configfile)
newconfig = config.import_teamocil(configparser.get())
configparser.import_config(newconfig)
else:
sys.exit('File not found: %s' % configfile)
config_format = prompt_choices('Convert to', choices=[
'yaml', 'json'], default='yaml')
if config_format == 'yaml':
newconfig = configparser.export(
'yaml', indent=2, default_flow_style=False
)
elif config_format == 'json':
newconfig = configparser.export('json', indent=2)
else:
sys.exit('Unknown config format.')
print(newconfig)
print(
'---------------------------------------------------------------')
print(
'Configuration import does its best to convert teamocil files.\n')
if args.answer_yes or prompt_yes_no(
'The new config *WILL* require adjusting afterwards. Save config?'
):
dest = None
while not dest:
dest_prompt = prompt('Save to: ', os.path.abspath(
os.path.join(config_dir, 'myimport.%s' % config_format)))
if os.path.exists(dest_prompt):
print('%s exists. Pick a new filename.' % dest_prompt)
continue
dest = dest_prompt
dest = os.path.abspath(os.path.relpath(os.path.expanduser(dest)))
if args.answer_yes or prompt_yes_no('Write to %s?' % dest):
buf = open(dest, 'w')
buf.write(newconfig)
buf.close()
print('Saved to %s.' % dest)
else:
print(
'tmuxp has examples in JSON and YAML format at <http://tmuxp.readthedocs.org/en/latest/examples.html>\n'
'View tmuxp docs at <http://tmuxp.readthedocs.org/>'
)
sys.exit()
def command_import_tmuxinator(args):
"""Import tmuxinator config to tmuxp format."""
if args.list:
try:
configs_in_user = config.in_dir(
tmuxinator_config_dir, extensions='yml')
except OSError:
configs_in_user = []
configs_in_cwd = config.in_dir(
config_dir=cwd_dir, extensions='yml')
output = ''
if not os.path.exists(tmuxinator_config_dir):
output += '# %s: \n\tDirectory doesn\'t exist.\n' % tmuxinator_config_dir
elif not configs_in_user:
output += '# %s: \n\tNone found.\n' % tmuxinator_config_dir
else:
output += '# %s: \n\t%s\n' % (
config_dir, ', '.join(configs_in_user)
)
if configs_in_cwd:
output += '# current directory:\n\t%s' % (
', '.join(configs_in_cwd)
)
print(output)
if args.config:
configfile = os.path.abspath(os.path.relpath(
os.path.expanduser(args.config)))
configparser = kaptan.Kaptan(handler='yaml')
if os.path.exists(configfile):
print(configfile)
configparser.import_config(configfile)
newconfig = config.import_tmuxinator(configparser.get())
configparser.import_config(newconfig)
else:
sys.exit('File not found: %s' % configfile)
config_format = prompt_choices('Convert to', choices=[
'yaml', 'json'], default='yaml')
if config_format == 'yaml':
newconfig = configparser.export(
'yaml', indent=2, default_flow_style=False
)
elif config_format == 'json':
newconfig = configparser.export('json', indent=2)
else:
sys.exit('Unknown config format.')
print(newconfig)
print(
'---------------------------------------------------------------')
print(
'Configuration import does its best to convert tmuxinator files.\n')
if args.answer_yes or prompt_yes_no(
'The new config *WILL* require adjusting afterwards. Save config?'
):
dest = None
while not dest:
dest_prompt = prompt('Save to: ', os.path.abspath(
os.path.join(config_dir, 'myimport.%s' % config_format)))
if os.path.exists(dest_prompt):
print('%s exists. Pick a new filename.' % dest_prompt)
continue
dest = dest_prompt
dest = os.path.abspath(os.path.relpath(os.path.expanduser(dest)))
if args.answer_yes or prompt_yes_no('Write to %s?' % dest):
buf = open(dest, 'w')
buf.write(newconfig)
buf.close()
print('Saved to %s.' % dest)
else:
print(
'tmuxp has examples in JSON and YAML format at <http://tmuxp.readthedocs.org/en/latest/examples.html>\n'
'View tmuxp docs at <http://tmuxp.readthedocs.org/>'
)
sys.exit()
def command_convert(args):
"""Convert tmuxp config to and from JSON and YAML."""
try:
configfile = args.config
except exc.TmuxpException:
print('Please enter a config')
file_user = os.path.join(config_dir, configfile)
file_cwd = os.path.join(cwd_dir, configfile)
if os.path.exists(file_cwd) and os.path.isfile(file_cwd):
fullfile = os.path.normpath(file_cwd)
filename, ext = os.path.splitext(file_cwd)
elif os.path.exists(file_user) and os.path.isfile(file_user):
fullfile = os.path.normpath(file_user)
filename, ext = os.path.splitext(file_user)
else:
logger.error('%s not found.' % configfile)
return
if 'json' in ext:
if args.answer_yes or prompt_yes_no('convert to <%s> to yaml?' % (fullfile)):
configparser = kaptan.Kaptan()
configparser.import_config(configfile)
newfile = fullfile.replace(ext, '.yaml')
newconfig = configparser.export(
'yaml', indent=2, default_flow_style=False
)
if args.answer_yes or prompt_yes_no('write config to %s?' % (newfile)):
buf = open(newfile, 'w')
buf.write(newconfig)
buf.close()
print('written new config to %s' % (newfile))
elif 'yaml' in ext:
if args.answer_yes or prompt_yes_no('convert to <%s> to json?' % (fullfile)):
configparser = kaptan.Kaptan()
configparser.import_config(configfile)
newfile = fullfile.replace(ext, '.json')
newconfig = configparser.export('json', indent=2)
print(newconfig)
if args.answer_yes or prompt_yes_no('write config to <%s>?' % (newfile)):
buf = open(newfile, 'w')
buf.write(newconfig)
buf.close()
print('written new config to <%s>.' % (newfile))
def command_attach_session(args):
"""Command to attach / switch client to a tmux session."""
commands = []
ctext = ' '.join(args.session_name)
t = Server(
socket_name=args.socket_name,
socket_path=args.socket_path,
colors=args.colors
)
try:
session = next((s for s in t.sessions if s.get(
'session_name') == ctext), None)
if not session:
raise exc.TmuxpException('Session not found.')
except exc.TmuxpException as e:
print(e)
return
if 'TMUX' in os.environ:
del os.environ['TMUX']
session.switch_client()
print('Inside tmux client, switching client.')
else:
session.attach_session()
print('Attaching client.')
def command_kill_session(args):
"""Command to kill a tmux session."""
commands = []
ctext = ' '.join(args.session_name)
t = Server(
socket_name=args.socket_name or None,
socket_path=args.socket_path or None
)
try:
session = next((s for s in t.sessions if s.get(
'session_name') == ctext), None)
if not session:
raise exc.TmuxpException('Session not found.')
except exc.TmuxpException as e:
print(e)
return
try:
session.kill_session()
print("Killed session %s." % ctext)
except exc.TmuxpException as e:
logger.error(e)
def get_parser():
"""Return :py:class:`argparse.ArgumentParser` instance for CLI."""
server_parser = argparse.ArgumentParser(add_help=False)
# server_parser.add_argument(
# '--log-level',
# dest='log_level',
# default='INFO',
# metavar='log-level',
# help='Log level e.g. INFO, DEBUG, ERROR'
# )
server_parser.add_argument(
'-L', dest='socket_name',
default=None,
help='socket name of tmux server. Same as tmux.',
metavar='socket-name'
)
server_parser.add_argument(
'-S',
dest='socket_path',
default=None,
help='socket path of tmux server. Same as tmux.',
metavar='socket-path'
)
server_parser.add_argument(
'-y',
dest='answer_yes',
default=None,
help='Always answer yes.',
action='store_true'
)
parser = argparse.ArgumentParser(
description='Launch tmux workspace. '
'Help documentation: <http://tmuxp.rtfd.org>.',
parents=[server_parser]
)
client_parser = argparse.ArgumentParser(add_help=False)
colorsgroup = client_parser.add_mutually_exclusive_group()
colorsgroup.add_argument(
'-2',
dest='colors',
action='store_const',
const=256,
help='Force tmux to assume the terminal supports 256 colours.',
)
colorsgroup.add_argument(
'-8',
dest='colors',
action='store_const',
const=88,
help='Like -2, but indicates that the terminal supports 88 colours.',
)
parser.set_defaults(colors=None)
subparsers = parser.add_subparsers(
title='commands',
description='valid commands',
)
kill_session = subparsers.add_parser(
'kill-session',
parents=[server_parser],
help='Kill tmux session by name.'
)
kill_session.set_defaults(callback=command_kill_session)
kill_session.add_argument(
dest='session_name',
type=str,
nargs='+',
default=None,
help='Name of session',
).completer = SessionCompleter
attach_session = subparsers.add_parser(
'attach-session',
parents=[server_parser, client_parser],
help='If run from outside tmux, create a new client in the current '
'terminal and attach it. If used from inside, switch the current '
'client.'
)
attach_session.set_defaults(callback=command_attach_session)
attach_session.add_argument(
dest='session_name',
nargs='+',
type=str,
help='Name of session',
).completer = SessionCompleter
freeze = subparsers.add_parser(
'freeze',
parents=[server_parser],
help='Create a snapshot of a tmux session and save it to JSON or YAML.'
)
freeze.set_defaults(callback=command_freeze)
freeze.add_argument(
dest='session_name',
type=str,
nargs='+',
help='Name of session',
).completer = SessionCompleter
load = subparsers.add_parser(
'load',
parents=[server_parser, client_parser],
help='Load a configuration from file. Attach the session. If session '
'already exists, offer to attach instead.'
)
loadgroup = load.add_mutually_exclusive_group(required=True)
loadgroup.add_argument(
'--list', dest='list', action='store_true',
help='List config files available',
)
loadgroup.add_argument(
dest='config',
type=str,
nargs='?',
help='List config available in working directory and config folder.'
).completer = ConfigFileCompleter(allowednames=('.yaml', '.json'), directories=False)
load.set_defaults(callback=command_load)
convert = subparsers.add_parser(
'convert',
help='Convert tmuxp config between YAML and JSON format.'
)
convert.add_argument(
dest='config',
type=str,
default=None,
help='Absolute or relative path to config file.'
).completer = ConfigFileCompleter(allowednames=('.yaml', '.json'), directories=False)
convert.set_defaults(callback=command_convert)
importparser = subparsers.add_parser(
'import',
help='Import configurations from teamocil and tmuxinator.'
)
importsubparser = importparser.add_subparsers(
title='commands',
description='valid commands',
help='additional help'
)
import_teamocil = importsubparser.add_parser(
'teamocil',
help="Parse teamocil configurations into tmuxp format"
)
import_teamocilgroup = import_teamocil.add_mutually_exclusive_group(
required=True
)
import_teamocilgroup.add_argument(
'--list', dest='list', action='store_true',
help='List configs in ~/.teamocil and current working directory.'
)
import_teamocilgroup.add_argument(
dest='config',
type=str,
nargs='?',
help='''\
Checks current ~/.teamocil and current directory for yaml files.
'''
).completer = TeamocilCompleter(allowednames=('.yml'), directories=False)
import_teamocil.set_defaults(callback=command_import_teamocil)
import_tmuxinator = importsubparser.add_parser(
'tmuxinator',
help="Parse teamocil configurations into tmuxp format"
)
import_tmuxinatorgroup = import_tmuxinator.add_mutually_exclusive_group(
required=True)
import_tmuxinatorgroup.add_argument(
'--list', dest='list', action='store_true',
help='List yaml configs in ~/.tmuxinator and current working directory.'
)
import_tmuxinatorgroup.add_argument(
dest='config',
type=str,
nargs='?',
help='''\
Checks current ~/.tmuxinator and current directory for yaml files.
'''
).completer = TmuxinatorCompleter(allowednames=('.yml'), directories=False)
import_tmuxinator.set_defaults(callback=command_import_tmuxinator)
# http://stackoverflow.com/questions/8521612/argparse-optional-subparser
parser.add_argument(
'-v', '--version', action='version',
version='tmuxp %s' % __version__,
help='Prints the tmuxp version',
)
return parser
def main():
parser = get_parser()
argcomplete.autocomplete(parser, always_complete_options=False)
args = parser.parse_args()
setup_logger(level=args.log_level.upper() if 'log_level' in args else 'INFO')
try:
util.has_required_tmux_version()
except exc.TmuxpException as e:
logger.error(e)
sys.exit()
util.oh_my_zsh_auto_title()
t = Server(
socket_name=args.socket_name,
socket_path=args.socket_path,
colors=args.colors
)
if args.callback is command_load:
command_load(args)
elif args.callback is command_convert:
command_convert(args)
elif args.callback is command_import_teamocil:
command_import_teamocil(args)
elif args.callback is command_import_tmuxinator:
command_import_tmuxinator(args)
elif args.callback is command_freeze:
command_freeze(args)
elif args.callback is command_attach_session:
command_attach_session(args)
elif args.callback is command_kill_session:
command_kill_session(args)
else:
parser.print_help()