Changeset - bf7668f56c5d
[Not reviewed]
version_5
0 1 0
Dennis Fink - 9 years ago 2015-10-14 22:46:13
dennis.fink@c3l.lu
Added bridgeprogram switch to add server option
1 file changed with 13 insertions and 1 deletions:
0 comments (0 inline, 0 general)
ennstatus/cli/commands/config.py
Show inline comments
 
@@ -10,73 +10,85 @@ from ...utils import check_ip
 

	
 
@click.group(short_help='Configure ennstatus')
 
def config():
 
    pass
 

	
 

	
 
@config.command('show', short_help='Show current configuration')
 
@click.pass_obj
 
def show(obj):
 

	
 
    with obj['config_file'].open(encoding='utf-8') as f:
 
        config = json.load(f)
 
    pprint(config)
 

	
 

	
 
@config.group(short_help='Configure servers')
 
def server():
 
    pass
 

	
 

	
 
@server.command('add', short_help='Add server')
 
@click.argument('name')
 
@click.option('-i', '--ips', prompt='IPs (comma separated)')
 
@click.password_option()
 
@click.option('--bridgeprogram/--no-bridgeprogram', default=False)
 
@click.pass_obj
 
def add(obj, name, ips, password):
 
def add(obj, name, ips, password, bridgeprogram):
 

	
 
    with obj['config_file'].open() as f:
 
        config = json.load(f)
 

	
 
    name = name.lower()
 
    ips = [ip.strip() for ip in ips.split(',')]
 

	
 
    try:
 
        if name in config['ENNSTATUS_SERVERS']:
 
            try:
 
                click.confirm('Server already exits! Overwrite?', abort=True)
 
            except click.Abort as e:
 
                raise SystemExit from e
 
    except KeyError:
 
        config['ENNSTATUS_SERVERS'] = {}
 

	
 
    converted_ips = {ipaddress.ip_address(ip) for ip in ips}
 

	
 
    for ip in converted_ips:
 
        if not check_ip(ip):
 
            raise SystemExit('ip {} is not accepted!'.format(str(ip)))
 

	
 
    config['ENNSTATUS_SERVERS'][name] = {
 
        'IPS': ips,
 
        'PASSWORD': password
 
    }
 

	
 
    if bridgeprogram:
 
        try:
 
            config['ENNSTATUS_BRIDGE_PROGRAM'].append(name)
 
        except KeyError:
 
            config['ENNSTATUS_BRIDGE_PROGRAM'] = [name]
 

	
 
    with obj['config_file'].open(mode='w', encoding='utf-8') as f:
 
        json.dump(config, f, indent=4, sort_keys=True)
 

	
 

	
 
@server.command('delete', short_help='Remove server')
 
@click.argument('name')
 
@click.pass_obj
 
def delete(obj, name):
 

	
 
    with obj['config_file'].open() as f:
 
        config = json.load(f)
 
    name = name.lower()
 

	
 
    try:
 
        del config['ENNSTATUS_SERVERS'][name]
 
    except KeyError:
 
        raise SystemExit('{} does not exists!'.format(name))
 

	
 
    try:
 
        config['ENNSTATUS_BRIDGE_PROGRAM'].remove(name)
 
    except KeyError:
 
        pass
 

	
 
    with obj['config_file'].open(mode='w', encoding='utf-8') as f:
 
        json.dump(config, f, indent=4, sort_keys=True)
0 comments (0 inline, 0 general)