/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2016-06-27 20:21:50 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 350.
  • Revision ID: teddy@recompile.se-20160627202150-ml201ilxwjf1g29w
mandos-ctl: Implement --dump-json option

* mandos-ctl (main): Implement --dump-json option
* mandos-ctl.xml (NAME, DESCRIPTION): Change description to the more
                                      accurate "control or query".
  (SYNOPSIS): Add required group around action options and add new
              "--dump-json" option.
  (OPTIONS): Document new "--dump-json" option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
import os
40
40
import collections
41
41
import doctest
 
42
import json
42
43
 
43
44
import dbus
44
45
 
280
281
                        help="Select all clients")
281
282
    parser.add_argument("-v", "--verbose", action="store_true",
282
283
                        help="Print all fields")
 
284
    parser.add_argument("-j", "--dump-json", action="store_true",
 
285
                        help="Dump client data in JSON format")
283
286
    parser.add_argument("-e", "--enable", action="store_true",
284
287
                        help="Enable client")
285
288
    parser.add_argument("-d", "--disable", action="store_true",
328
331
    if has_actions(options) and not (options.client or options.all):
329
332
        parser.error("Options require clients names or --all.")
330
333
    if options.verbose and has_actions(options):
331
 
        parser.error("--verbose can only be used alone or with"
332
 
                     " --all.")
 
334
        parser.error("--verbose can only be used alone.")
 
335
    if options.dump_json and (options.verbose or has_actions(options)):
 
336
        parser.error("--dump-json can only be used alone.")
333
337
    if options.all and not has_actions(options):
334
338
        parser.error("--all requires an action.")
335
 
 
 
339
    
336
340
    if options.check:
337
341
        fail_count, test_count = doctest.testmod()
338
342
        sys.exit(os.EX_OK if fail_count == 0 else 1)
389
393
                sys.exit(1)
390
394
    
391
395
    if not has_actions(options) and clients:
392
 
        if options.verbose:
 
396
        if options.verbose or options.dump_json:
393
397
            keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK",
394
398
                        "Created", "Interval", "Host", "Fingerprint",
395
399
                        "CheckerRunning", "LastEnabled",
400
404
        else:
401
405
            keywords = defaultkeywords
402
406
        
403
 
        print_clients(clients.values(), keywords)
 
407
        if options.dump_json:
 
408
            json.dump({client["Name"]: {key: client[key]
 
409
                                        for key in keywords }
 
410
                       for client in clients.values() },
 
411
                      fp = sys.stdout, indent = 4,
 
412
                      separators = (',', ': '))
 
413
            print()
 
414
        else:
 
415
            print_clients(clients.values(), keywords)
404
416
    else:
405
417
        # Process each client in the list by all selected options
406
418
        for client in clients: