#!/usr/bin/python # juju-get-set # Copyright 2015 Canonical Ltd. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3, as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranties of # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import pipes def parse_args(): import argparse import sys parser = argparse.ArgumentParser( description='Format `juju get` output as `juju set` commands', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin) parser.add_argument( '--all', '-a', action='store_true', help='Display all options, even unmodified default options') parser.add_argument( '--comments', '-c', action='store_true', help='Add option descriptions to the end of each line as comments') return parser.parse_args() def parse_juju_get(get_struct, args): service = get_struct['service'] for name in get_struct['settings']: b = get_struct['settings'][name] if (not args.all) and ('default' in b) and (b['default']): continue novalue = False if 'value' not in b: novalue = True value = '' elif b['type'] in ('int', 'boolean'): value = str(b['value']).lower() else: value = pipes.quote(b['value']) if novalue: commented_str = '#' else: commented_str = '' print '%sjuju set %s %s=%s' % (commented_str, service, name, value) if novalue: print '# Option has no default or value set, type is: %s' % b['type'] if args.comments and 'description' in b: print '# Description: %s' % b['description'].replace('\n', '\n# ').replace('\r', '') if __name__ == '__main__': import sys import yaml args = parse_args() parse_juju_get(yaml.safe_load(args.infile), args)