11package com.delphix.yamlparser
22
3+ import com.github.ajalt.clikt.core.CliktCommand
4+ import com.github.ajalt.clikt.parameters.options.*
5+ import io.github.cdimascio.dotenv.dotenv as Dotenv
6+
37import com.delphix.yamlparser.sdk.Delphix as Delphix
48import com.delphix.yamlparser.sdk.Http as Http
59
@@ -41,13 +45,20 @@ object Parser {
4145 return event
4246 }
4347
44- fun loadEnvs (): Map <String , String > {
45- val gitBranch: String = System .getenv(" GIT_BRANCH" ) ? : throw IllegalArgumentException (" GIT_BRANCH Environment Variable Required." )
46- val gitCommit: String = System .getenv(" GIT_COMMIT" ) ? : throw IllegalArgumentException (" GIT_COMMIT Environment Variable Required." )
47- val gitEvent = System .getenv(" GIT_EVENT" ) ? : loadEventFromPayload()
48- val delphixEngine: String = System .getenv(" DELPHIX_ENGINE" ) ? : throw IllegalArgumentException (" DELPHIX_ENGINE Environment Variable Required." )
49- val delphixUser: String = System .getenv(" DELPHIX_USER" ) ? : throw IllegalArgumentException (" DELPHIX_USER Environment Variable Required." )
50- val delphixPass: String = System .getenv(" DELPHIX_PASS" ) ? : throw IllegalArgumentException (" DELPHIX_PASS Environment Variable Required." )
48+ fun loadEnvs (env : String ): Map <String , String > {
49+
50+ val dotenv = Dotenv {
51+ filename = " $env "
52+ ignoreIfMalformed = true
53+ ignoreIfMissing = true
54+ }
55+
56+ val gitBranch: String = dotenv[" GIT_BRANCH" ] ? : throw IllegalArgumentException (" GIT_BRANCH Environment Variable Required." )
57+ val gitCommit: String = dotenv[" GIT_COMMIT" ] ? : throw IllegalArgumentException (" GIT_COMMIT Environment Variable Required." )
58+ val gitEvent = dotenv[" GIT_EVENT" ] ? : loadEventFromPayload()
59+ val delphixEngine: String = dotenv[" DELPHIX_ENGINE" ] ? : throw IllegalArgumentException (" DELPHIX_ENGINE Environment Variable Required." )
60+ val delphixUser: String = dotenv[" DELPHIX_USER" ] ? : throw IllegalArgumentException (" DELPHIX_USER Environment Variable Required." )
61+ val delphixPass: String = dotenv[" DELPHIX_PASS" ] ? : throw IllegalArgumentException (" DELPHIX_PASS Environment Variable Required." )
5162 return mapOf (
5263 " gitBranch" to gitBranch,
5364 " gitCommit" to gitCommit,
@@ -59,36 +70,41 @@ object Parser {
5970 )
6071 }
6172
62- @JvmStatic
63- fun main (args : Array <String >) {
64- val file = File (" delphix.yaml" )
65- try {
66- fileExists(file)
67- } catch (e: NoSuchFileException ) {
68- System .err.println (e.message + " is required." )
69- System .exit(0 )
70- }
73+ class Parse : CliktCommand () {
74+ val env: String by option(help= " Path to env file." ).default(" .env" )
7175
72- val contents = loadYamlFromFile(file)
73- try {
74- Validator (contents).validate()
75- } catch (e: IllegalArgumentException ) {
76- System .err.println (e.message)
77- System .exit(0 )
78- }
76+ override fun run (){
77+ val file = File (" delphix.yaml" )
78+ try {
79+ fileExists(file)
80+ } catch (e: NoSuchFileException ) {
81+ System .err.println (e.message + " is required." )
82+ System .exit(0 )
83+ }
7984
80- val env: Map <String , String > = loadEnvs()
81- val delphix: Delphix = Delphix (Http (env[" delphixEngine" ]? : " " ))
82- val yaml: Yaml = Mapper ().mapYaml(contents)
83- val runner: Runner = Runner (yaml, env, delphix)
85+ val contents = loadYamlFromFile(file)
86+ try {
87+ Validator (contents).validate()
88+ } catch (e: IllegalArgumentException ) {
89+ System .err.println (e.message)
90+ System .exit(0 )
91+ }
8492
85- try {
86- runner.run ()
87- }
88- catch (e: Exception ) {
89- System .err.println (e.message)
90- System .exit(0 )
93+ val env: Map <String , String > = loadEnvs(env)
94+ val delphix: Delphix = Delphix (Http (env[" delphixEngine" ]? : " " ))
95+ val yaml: Yaml = Mapper ().mapYaml(contents)
96+ val runner: Runner = Runner (yaml, env, delphix)
97+
98+ try {
99+ runner.run ()
100+ }
101+ catch (e: Exception ) {
102+ System .err.println (e.message)
103+ System .exit(0 )
104+ }
91105 }
92-
93106 }
107+
108+ @JvmStatic
109+ fun main (args : Array <String >) = Parse ().main(args)
94110}
0 commit comments