Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --recurse and --trim to cli kv_get #58

Merged
merged 2 commits into from
Jul 21, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions consulate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ def connection_error():
[['path'],
{'help': 'The path to create'}]]),
('get', 'Get a key from the database', [
[['key'],
{'help': 'The key to get'}]]),
[['key'], {'help': 'The key to get'}],
[['-r', '--recurse'],
{'help': 'Get all keys prefixed with the specified key',
'action': 'store_true'}],
[['-t', '--trim'],
{'help': 'Number of levels of prefix to trim from returned key',
'type': int,
'default': 0}]]),
('set', 'Set a key in the database', [
[['key'], {'help': 'The key to set'}],
[['value'], {'help': 'The value of the key'}]]),
Expand Down Expand Up @@ -209,7 +215,18 @@ def kv_get(consul, args):

"""
try:
sys.stdout.write("%s\n" % consul.kv.get(args.key))
if args.recurse:
for key in sorted(consul.kv.find(args.key)):
displaykey = key
if args.trim:
keyparts = displaykey.split('/')
if (args.trim >= len(keyparts)):
displaykey = keyparts[-1]
else:
displaykey = '/'.join(keyparts[args.trim:])
sys.stdout.write('%s\t%s\n' % (displaykey, consul.kv.get(key)))
else:
sys.stdout.write('%s\n' % consul.kv.get(args.key))
except exceptions.ConnectionError:
connection_error()

Expand Down