-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
108 lines (95 loc) · 3.24 KB
/
cli.py
File metadata and controls
108 lines (95 loc) · 3.24 KB
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
import os
import stat
import sys
import click
from click import secho
import snapconfig
import gitops
OK = click.style(u'[\u2713]', bold=True, fg='green')
BAD = click.style(u'[\u203C]', bold=True, fg='red')
hook_path = '.git/hooks/post-commit'
def _is_enabled(post_commit_file):
with open(hook_path, 'r') as f:
contents = f.read()
return 'snap-commit' in contents
def write_hook_file(hook_path):
with open(hook_path, 'a') as f:
f.write('\nsnap-commit-hook&\n')
os.chmod(hook_path, os.stat(hook_path).st_mode | stat.S_IEXEC)
secho(u'{} snap-commit enabled'.format(OK))
def disable_hook(hook_path):
with open(hook_path, 'r+') as f:
contents = [line for line in f.read().split('\n') if line is not '']
f.seek(0)
snap_line = [idx for idx, _ in enumerate(contents) if 'snap-commit' in _]
contents.pop(snap_line[0])
f.write('\n'.join(contents))
f.truncate()
secho(u'{} snap-commit disabled'.format(OK))
@click.group()
@click.version_option()
@click.pass_context
def cli(ctx):
ctx.obj = snapconfig.load_config()
@cli.command(help='List images in IMAGE_DIR')
@click.pass_context
def list(ctx):
image_dir = ctx.obj['image_dir']
try:
secho('Listing images in {}'.format(image_dir))
images = sorted(os.listdir(image_dir))
for idx, image in enumerate(images):
secho('{:03d}. {}'.format(idx + 1, image))
except OSError, e:
secho('{}: {}'.format(e.strerror, image_dir))
sys.exit(1)
@cli.command(help='Open an image in the default viewer')
@click.argument('image_id', required=False, type=int)
@click.pass_context
def show(ctx, image_id=None):
image_dir = ctx.obj['image_dir']
if not image_id:
click.launch(image_dir, locate=True)
return
try:
images = sorted(os.listdir(image_dir))
image = images[image_id -1]
secho('Showing image {}'.format(image))
click.launch(os.path.join(image_dir, image))
except IndexError:
secho(u'{} Image id not found'.format(BAD))
sys.exit(1)
@cli.command(help='Enable snap-commit in the current repo')
def enable():
if gitops.is_repo:
if os.path.isfile(hook_path):
if not _is_enabled(hook_path):
write_hook_file(hook_path)
sys.exit(0)
else:
secho(u'{} snap-commit is already enabled for this repo'.format(OK))
sys.exit(0)
else:
write_hook_file(hook_path)
sys.exit(0)
else:
secho(u'{} Error: Directory is not a git repository'.format(BAD))
sys.exit(1)
@cli.command(help='Disable snap-commit in the current repo')
def disable():
if gitops.is_repo:
if os.path.isfile(hook_path):
if _is_enabled(hook_path):
disable_hook(hook_path)
sys.exit(0)
else:
secho(u'{} snap-commit is not enabled for this repo'.format(OK))
sys.exit(0)
else:
secho(u'{} no post-commit file found, snap-commit is not enabled'.format(OK))
sys.exit(0)
else:
secho(u'{} Error: Directory is not a git repository'.format(BAD))
sys.exit(1)
if __name__ == '__main__':
cli()