Skip to content

Commit 296a65d

Browse files
author
Derek Smart
committed
Merge branch 'feature/env-as-variable'
2 parents 6c70d7c + ee848f4 commit 296a65d

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@
218218
<artifactId>json</artifactId>
219219
<version>20180130</version>
220220
</dependency>
221+
<dependency>
222+
<groupId>com.github.ajalt</groupId>
223+
<artifactId>clikt</artifactId>
224+
<version>1.6.0</version>
225+
</dependency>
226+
<dependency>
227+
<groupId>io.github.cdimascio</groupId>
228+
<artifactId>java-dotenv</artifactId>
229+
<version>3.1.7</version>
230+
</dependency>
221231
</dependencies>
222232

223233
</project>

src/main/kotlin/com/delphix/yamlparser/Parser.kt

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package 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+
37
import com.delphix.yamlparser.sdk.Delphix as Delphix
48
import 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

Comments
 (0)