#!/usr/bin/env python3

from os import environ
from os.path import abspath, dirname
from sys import argv

from bundlewrap.exceptions import FaultUnavailable
from bundlewrap.metagen import NodeMetadataProxy
from bundlewrap.repo import Repository
from bundlewrap.utils import Fault

repo = Repository(
    dirname(dirname(abspath(__file__)))
)

def print_faults(dictionary, keypath=[]):
    for key, value in sorted(dictionary.items()):
        key = str(key)
        if isinstance(value, Fault):
            try:
                resolved_fault = value.value
            except FaultUnavailable:
                print('{}/{}: [permission denied]'.format('/'.join(keypath), key))
            else:
                if '\n' not in resolved_fault:
                    print('{}/{}: {}'.format('/'.join(keypath), key, value))
        elif isinstance(value, (list, set, tuple)):
            print_faults(dict(enumerate(value)), keypath=keypath+[key])
        elif isinstance(value, (dict, NodeMetadataProxy)):
            print_faults(value, keypath=keypath+[key])


if len(argv) == 1:
    print('node name missing')
    exit(1)

node = repo.get_node(argv[1])
if node.username or node.password:
    print_faults({
        'username': node.username,
        'password': node.password,
    })
#if node.ipmi_username or node.ipmi_password:
#    print_faults({
#        'ipmi_username': node.ipmi_username,
#        'ipmi_password': node.ipmi_password,
#    })
print_faults({
    'metadata': node.metadata,
})