@@ -55,13 +55,19 @@ def connection_error():
55
55
56
56
KV_PARSERS = [
57
57
('backup' , 'Backup to stdout or a JSON file' , [
58
+ [['key' ], {'help' : 'The key to use as target to backup a specific key or folder.' ,
59
+ 'nargs' :'?' }],
58
60
[['-b' , '--base64' ], {'help' : 'Base64 encode values' ,
59
61
'action' : 'store_true' }],
60
62
[['-f' , '--file' ], {'help' : 'JSON file to write instead of stdout' ,
61
63
'nargs' : '?' }],
62
64
[['-p' , '--pretty' ], {'help' : 'pretty-print JSON output' ,
63
65
'action' : 'store_true' }]]),
64
66
('restore' , 'Restore from stdin or a JSON file' , [
67
+ [['key' ], {'help' : 'The key as target to restore to a specific key or folder.' ,
68
+ 'nargs' :'?' }],
69
+ [['-p' , '--prune' ], {'help' :'Remove entries from consul tree that are not in restore file.' ,
70
+ 'action' : 'store_true' }],
65
71
[['-b' , '--base64' ], {'help' : 'Restore from Base64 encode values' ,
66
72
'action' : 'store_true' }],
67
73
[['-f' , '--file' ],
@@ -71,6 +77,8 @@ def connection_error():
71
77
{'help' : 'Do not replace existing entries' ,
72
78
'action' : 'store_true' }]]),
73
79
('ls' , 'List all of the keys' , [
80
+ [['key' ], {'help' : 'The key to use as target to list contents of specific key or folder' ,
81
+ 'nargs' :'?' }],
74
82
[['-l' , '--long' ],
75
83
{'help' : 'Long format' ,
76
84
'action' : 'store_true' }]]),
@@ -277,7 +285,13 @@ def kv_backup(consul, args):
277
285
278
286
"""
279
287
handle = open (args .file , 'w' ) if args .file else sys .stdout
280
- records = consul .kv .records ()
288
+ if args .key :
289
+ args .key = args .key .strip ('/' )
290
+ prefixlen = len (args .key .split ('/' ))
291
+ records = [('/' .join (k .split ('/' )[prefixlen :]), f , v )
292
+ for k , f , v in consul .kv .records (args .key )]
293
+ else :
294
+ records = consul .kv .records ()
281
295
if args .base64 :
282
296
if utils .PYTHON3 :
283
297
records = [(k , f , str (base64 .b64encode (utils .maybe_encode (v )),
@@ -341,7 +355,12 @@ def kv_ls(consul, args):
341
355
342
356
"""
343
357
try :
344
- for key in consul .kv .keys ():
358
+ if args .key :
359
+ args .key = args .key .lstrip ('/' )
360
+ keylist = sorted (consul .kv .find (args .key ))
361
+ else :
362
+ keylist = consul .kv .keys ()
363
+ for key in keylist :
345
364
if args .long :
346
365
keylen = 0
347
366
if consul .kv [key ]:
@@ -375,6 +394,12 @@ def kv_restore(consul, args):
375
394
:param argparser.namespace args: The cli args
376
395
377
396
"""
397
+ if args .prune :
398
+ if args .key :
399
+ args .key = args .key .strip ('/' )
400
+ keylist = consul .kv .find (args .key )
401
+ else :
402
+ keylist = consul .kv .find ('' )
378
403
handle = open (args .file , 'r' ) if args .file else sys .stdin
379
404
data = json .load (handle )
380
405
for row in data :
@@ -390,10 +415,28 @@ def kv_restore(consul, args):
390
415
# Here's an awesome thing to make things work
391
416
if not utils .PYTHON3 and isinstance (row [2 ], unicode ):
392
417
row [2 ] = row [2 ].encode ('utf-8' )
418
+ if args .key :
419
+ if row [0 ] == "" :
420
+ rowkey = args .key
421
+ else :
422
+ rowkey = args .key + '/' + row [0 ]
423
+ else :
424
+ rowkey = row [0 ]
425
+ if args .prune :
426
+ if rowkey in keylist :
427
+ del keylist [rowkey ]
393
428
try :
394
- consul .kv .set_record (row [ 0 ] , row [1 ], row [2 ], not args .no_replace )
429
+ consul .kv .set_record (rowkey , row [1 ], row [2 ], not args .no_replace )
395
430
except exceptions .ConnectionError :
396
431
connection_error ()
432
+ if args .prune :
433
+ for key in keylist :
434
+ print "Pruning " + key
435
+ try :
436
+ consul .kv .delete (key )
437
+ except exceptions .ConnectionError :
438
+ connection_error ()
439
+
397
440
398
441
399
442
def kv_rm (consul , args ):
0 commit comments