-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
199 lines (165 loc) · 5.78 KB
/
cli.py
File metadata and controls
199 lines (165 loc) · 5.78 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import click
import os
from sys import stdin
from utils import get_content_from_editor
from cabinet_wrapper import CabinetWrapper
@click.group()
@click.option('account', '-a', '--account',
help='Specify an account to use')
@click.option('vault', '-v', '--vault',
help='Specify a vault to use')
@click.option('password_env', '-p', '--password-env', is_flag=True,
help='Get password from CABPASS environment variable.')
@click.pass_context
def cli(ctx, account, vault, password_env):
"""Cabinet's command line interface."""
if ctx.obj is None:
ctx.obj = {}
if account or vault:
click.echo(">> Credentials")
click.echo("Account:", account)
click.echo("Vault:", vault)
ctx.obj['account'] = account
ctx.obj['vault'] = vault
if password_env:
password = os.environ.get("CABPASS")
ctx.obj['password'] = password
@cli.command()
@click.argument('name')
@click.option('tags', '-t', '--tag', multiple=True,
help='Specify tags for the item')
@click.option('content', '-c', '--content',
help='The item content')
@click.option('from_stdin', '-i', '--from-stdin', is_flag=True,
help="Get the content from stdin")
@click.option('editor', '-e', '--use-editor', is_flag=True,
help="Use the default editor for entering the content")
@click.pass_context
def add(ctx, name, tags, content, from_stdin, editor):
"""Add an item to cabinet"""
click.echo('>> Add item')
click.echo('Name: {0}'.format(name))
click.echo('Content: {0}'.format(content))
if not content:
if from_stdin:
content = ""
for line in stdin:
content = content + line
elif editor:
content = get_content_from_editor()
print("Content from editor:")
print(content)
else:
click.echo("Error: you need to specify the content")
return
if tags:
click.echo('Tags: %s' % ', '.join(tags))
account = ctx.obj.get('account')
vault = ctx.obj.get('vault')
cab = CabinetWrapper(account, vault)
cab.add_item(name, tags, content)
@cli.command()
@click.argument('name')
@click.option('tags', '-t', '--tag', multiple=True,
help='Specify tags for the item')
@click.option('content', '-c', '--content',
help='The item content')
@click.option('from_stdin', '-i', '--from-stdin', is_flag=True,
help="Get the content from stdin")
@click.option('editor', '-e', '--use-editor', is_flag=True,
help="Use the default editor for entering the content")
@click.pass_context
def update(ctx, name, tags, content, from_stdin, editor):
"""Update an existing item"""
click.echo('>> Edit item')
click.echo('Name: {0}'.format(name))
account = ctx.obj.get('account')
vault = ctx.obj.get('vault')
password = ctx.obj.get('password')
cab = CabinetWrapper(account, vault, password)
if not content:
if from_stdin:
content = ""
for line in stdin:
content = content + line
elif editor:
current_content = cab.get_item_content(name)
if current_content:
content = get_content_from_editor(current_content)
print("Content from editor:")
print(content)
else:
click.echo("Error: you need to specify the content")
return
if content:
if not tags:
tags = cab.get_item_tags(name)
click.echo('Tags: %s' % ', '.join(tags))
cab.update(name, tags, content)
@cli.command()
@click.argument('name')
@click.argument('new_name')
@click.pass_context
def rename(ctx, name, new_name):
"""Rename an existing item"""
click.echo('>> Rename item')
click.echo('Name: {0}'.format(name))
click.echo('New name: {0}'.format(new_name))
account = ctx.obj.get('account')
vault = ctx.obj.get('vault')
cab = CabinetWrapper(account, vault)
cab.rename(name, new_name)
@cli.command()
@click.argument('name')
@click.option('print_all', '-a', '--all', is_flag=True,
help='Print the item with all its information (tags and name)')
@click.pass_context
def get(ctx, name, print_all):
"""Get an item from cabinet"""
account = ctx.obj.get('account')
vault = ctx.obj.get('vault')
password = ctx.obj.get('password')
cab = CabinetWrapper(account, vault, password)
cab.get_item(name, print_all)
@cli.command()
@click.argument('name')
@click.pass_context
def rm(ctx, name):
"""Remove an item from cabinet"""
click.echo('>> Removing item')
click.echo('Name: {0}'.format(name))
if not click.confirm('Do you want to continue?'):
click.echo('Action canceled')
return
account = ctx.obj.get('account')
vault = ctx.obj.get('vault')
password = ctx.obj.get('password')
cab = CabinetWrapper(account, vault, password)
cab.remove(name)
@cli.command()
@click.pass_context
def tags(ctx):
"""Get tags from cabinet"""
account = ctx.obj.get('account')
vault = ctx.obj.get('vault')
cab = CabinetWrapper(account, vault)
cab.get_tags()
@cli.command()
@click.option('tags', '-t', '--tag', multiple=True,
help='Specify tags for the item')
@click.option('show_tags', '-s', '--show-tags', is_flag=True,
help='Show items with tags.')
@click.pass_context
def search(ctx, tags, show_tags):
"""Item searching on cabinet"""
if tags:
click.echo('Tags: %s' % ', '.join(tags))
account = ctx.obj.get('account')
vault = ctx.obj.get('vault')
password = ctx.obj.get('password')
cab = CabinetWrapper(account, vault, password)
cab.search(tags, show_tags)
if __name__ == '__main__':
cli()