#!/usr/bin/env python3

# ------------------------------------------------------------------------
# Copyright 2020, 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.
# ------------------------------------------------------------------------

""" Generate YAML file for service account creation """


# Global modules

import sys


# Local modules

from modules.args    import (
    getCommonArgsParser,
    addArgOutputFile
)
from modules.context import getContext


# Functions

def _getArgs():
    """ Get command line arguments """
    parser = getCommonArgsParser(
        'Generate YAML file for service account creation'
    )

    addArgOutputFile(parser, None)

    return parser.parse_args()


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

def _main():

    ctx = getContext(_getArgs())

    outputFile = ctx.ar.output_file
    if not outputFile:
        outputFile = ctx.cf.ocp.sa.file

    with open(outputFile, 'w') as fh:  # pylint: disable=invalid-name
        print(f'apiVersion: v1\n'
              f'kind: ServiceAccount\n'
              f'metadata:\n'
              f'  name: {ctx.cf.ocp.sa.name}\n'
              f'  namespace: {ctx.cf.ocp.project}',
              file=fh
              )

    print(outputFile)


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

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