#!/usr/bin/env python3

# ------------------------------------------------------------------------
# Copyright 2021 IBM Corp. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------

""" Credentials management """


# Global modules

import sys
import yaml


# Local modules

from modules.args     import getCommonArgsParser
from modules.context  import getContext
from modules.creds    import Creds
from modules.nestedns import nestedNsToObj


# Functions

def _getArgs():
    """ Get command line arguments """
    parser = getCommonArgsParser(
        'Credentials management'
    )

    parser.add_argument(
        '-n',
        '--new',
        required = False,
        action   = 'store_true',
        help     = "Create a new credentials file"
    )

    parser.add_argument(
        '-e',
        '--edit',
        required = False,
        action   = 'store_true',
        help     = "Change credentials in an existing credentials file"
    )

    parser.add_argument(
        '-d',
        '--dump',
        required = False,
        action   = 'store_true',
        help     = "Dump credentials to stdout (DISPLAYS SECRETS IN CLEAR TEXT)"
    )

    parser.add_argument(
        '--non-interactive',
        required = False,
        action   = 'store_true',
        help     = "Perform '-n' and '-e' non-interactively"
                   ' (reading values from environment)'
    )

    parser.add_argument(
        '-s',
        '--suppress-descriptions',
        required = False,
        action   = 'store_true',
        help     = "Don't show detailed descriptions during edit"
    )

    parser.add_argument(
        '-u',
        '--unencrypted',
        required = False,
        default  = False,
        action   = 'store_true',
        help     = "If '-n' is specified: Don't encrypt a newly"
                   ' created credentials file'
    )

    parser.add_argument(
        '-r',
        '--recipient',
        metavar  = '<recipient>',
        required = False,
        default  = None,
        help     = "If '-n' is specified and '-u' is not specified:"
                   ' Owner e-mail address or key fingerprint of GPG key'
                   ' which will be used for encrypting a newly created'
                   ' credentials file. If not specified, symmetric AES256'
                   ' encryption is used.'
    )

    return parser.parse_args()


# ----------------------------------------------------------------------

def _main():

    ctx = getContext(_getArgs(), withCreds=False, withConfig=False)

    if ctx.ar.new:
        creds = Creds(ctx, create=True)
    else:
        creds = Creds(ctx, create=False)

    if ctx.ar.new:
        creds.create(ctx.ar.suppress_descriptions)

    elif ctx.ar.edit:
        creds.edit(ctx.ar.suppress_descriptions)

    elif ctx.ar.dump:
        print(yaml.dump(nestedNsToObj(creds.get(), hideSecrets=False)))

    else:
        print('\nUse option\n\n'
              "  '-n' to create a new credentials file\n"
              "  '-e' to edit an existing credentials file\n"
              "  '-d' to dump an existing credentials file (DISPLAYS SECRETS IN CLEAR TEXT)\n"
              "  '-h' to get more help\n\n"
              "NEVER CHANGE FILE 'creds.yaml.template'"
              " - OTHERWISE THIS TOOL WON'T WORK AS EXPECTED\n")


# ----------------------------------------------------------------------

if __name__ == '__main__':
    try:
        _main()
    except KeyboardInterrupt:
        print()
    sys.exit(0)
