Skip to content

Commit

Permalink
Merge pull request #58 from MWers/mwers-cli-get-recursive
Browse files Browse the repository at this point in the history
Added --recurse and --trim to cli kv_get
  • Loading branch information
gmr committed Jul 21, 2015
2 parents 0e0fef2 + 2e6ed04 commit 7f4827b
Showing 1 changed file with 20 additions and 3 deletions.
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

0 comments on commit 7f4827b

Please sign in to comment.