# -* encoding: utf-8 *-

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from typing import Type

import configargparse

from gopythongo.assemblers import BaseAssembler
from gopythongo.utils import create_script_path, run_process, cmdargs_unquote_split, print_info
from gopythongo.utils.buildcontext import the_context


class CertifyBuildAssembler(BaseAssembler):
    @property
    def assembler_name(self) -> str:
        return "certifybuild"

    @property
    def assembler_type(self) -> str:
        return BaseAssembler.TYPE_PREISOLATION

    def add_args(self, parser: configargparse.ArgumentParser) -> None:
        gp_cert = parser.add_argument_group("Certify build options")
        gp_cert.add_argument("--vaultgetcert-opts", dest="vaultgetcert_opts", default="",
                             help="Specify arguments for the execution of vaultgetcert.")

    def validate_args(self, args: configargparse.Namespace) -> None:
        pass

    def assemble(self, args: configargparse.Namespace) -> None:
        print_info("Certifying build with vaultgetcert")
        cmdargs = [create_script_path(the_context.gopythongo_path, "vaultgetcert")]
        cmdargs += cmdargs_unquote_split(args.vaultgetcert_opts)
        run_process(*cmdargs)

    def print_help(self) -> None:
        print("Certify build Assembler\n"
              "=======================\n"
              "\n"
              "This Assembler will run before the build process is isolated by a GoPythonGo\n"
              "Builder and execute gopythongo.vaultgetcert. This allows you to create\n"
              "build-specific X.509 certificates for authentication with services in your\n"
              "deployment environment.\n")


assembler_class = CertifyBuildAssembler  # type: Type[CertifyBuildAssembler]

@@ -216,16 +216,26 @@
 PL/2.0/.
+%0Aimport os
 %0A%0Afrom t
@@ -419,16 +419,41 @@
 int_info
+, highlight, ErrorMessage
 %0Afrom go
@@ -866,16 +866,314 @@
 tions%22)%0A
+        gp_cert = parser.add_argument(%22--vaultgetcert-config%22, dest=%22vaultgetcert_config%22,%0A                                      default=None,%0A                                      env_var=%22CERTIFYBUILD_CONFIG%22,%0A                                      help=%22Specify a config file for vaultgetcert.%22)%0A
         
@@ -1254,16 +1254,74 @@
 ult=%22%22,%0A
+                             env_var=%22CERTIFYBUILD_OPTS%22,%0A
         
@@ -1484,12 +1484,427 @@
     
-pass
+if args.vaultgetcert_config:%0A            if not os.path.exists(args.vaultgetcert_config) or%0A               not os.path.isfile(args.vaultgetcert_config) or%0A               not os.access(args.vaultgetcert_config, os.R_OK):%0A               raise ErrorMessage(%22%25s is not a file or not readable for gopythongo (%25s)%22 %25%0A                                  (highlight(args.vaultgetcert_config), highlight(%22--vaultgetcert-config%22)))
 %0A%0A  
@@ -2106,16 +2106,109 @@
 cert%22)%5D%0A
+        if args.vaultgetcert_config:%0A            cmdargs += %5B%22-c%22, args.vaultgetcert_config%5D%0A
         
@@ -2705,24 +2705,24 @@
  in your%5Cn%22%0A
-
             
@@ -2750,16 +2750,298 @@
 ment.%5Cn%22
+%0A              %22%5Cn%22%0A              %22It is good practice to configure this so your build server provides%5Cn%22%0A              %22configuration files for your individual Vault-backed CAs and passes them into%5Cn%22%0A              %22the builds using the CERTIFYBUILD_CONFIG environment variable.%5Cn%22
 )%0A%0A%0Aasse

add config file override for vaultgetcert/certifybuild assembler