Skip to content

Commit d829d66

Browse files
authored
Merge pull request #10 from colindean/colindean/drop-env-dump_gh4
Drops env dump from JSON output, introduces System.env wrapper
2 parents 5f8642d + 797c62e commit d829d66

File tree

5 files changed

+109
-8
lines changed

5 files changed

+109
-8
lines changed

src/main/scala/com/target/data_validator/ConfigVar.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.target.data_validator
22

33
import cats.syntax.functor._
4+
import com.target.data_validator.EnvironmentVariables.{Error, Inaccessible, Present, Unset}
45
import com.typesafe.scalalogging.LazyLogging
56
import io.circe.{Decoder, Json}
67
import io.circe.generic.auto._
@@ -21,18 +22,23 @@ case class NameValue(name: String, value: Json) extends ConfigVar {
2122
}
2223

2324
case class NameEnv(name: String, env: String) extends ConfigVar {
25+
2426
override def addEntry(spark: SparkSession, varSub: VarSubstitution): Boolean = {
2527
val newEnv = getVarSub(env, name, varSub)
26-
Option(System.getenv(newEnv)) match {
27-
case None =>
28+
EnvironmentVariables.get(newEnv) match {
29+
case Inaccessible(message) => logger.error(message); true
30+
case Error(message) => logger.error(message); true
31+
case Unset => {
2832
val msg = s"Variable '$name' cannot be processed env variable '$newEnv' not found!"
2933
logger.error(msg)
3034
addEvent(ValidatorError(msg))
3135
true
32-
case Some(v) =>
33-
val resolvedEnvVar = getVarSubJson(JsonUtils.string2Json(v), name, varSub)
34-
logger.debug(s"name: $name env: $env getEnv: $v resolvedEnvVar: $resolvedEnvVar")
36+
}
37+
case Present(value) => {
38+
val resolvedEnvVar = getVarSubJson(JsonUtils.string2Json(value), name, varSub)
39+
logger.info(s"name: $name env: $env getEnv: $value resolvedEnvVar: $resolvedEnvVar")
3540
varSub.add(name, resolvedEnvVar)
41+
}
3642
}
3743
}
3844
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.target.data_validator
2+
3+
import scala.collection.mutable
4+
import scala.util.Try
5+
6+
object EnvironmentVariables {
7+
type MaybeEnvVar = Try[Option[String]]
8+
9+
val accessedEnvVars: mutable.Map[String, MaybeEnvVar] = mutable.Map.empty
10+
11+
def get(key: String): EnvVarResult = {
12+
getWithHandlers(key)(
13+
whenError = { case throwable: Throwable => Inaccessible(throwable) },
14+
whenUnset = { Unset },
15+
whenPresent = { Present }
16+
)
17+
.recover { case throwable: Throwable => Error(throwable) }
18+
.get
19+
}
20+
21+
def getWithHandlers[T](key: String)(
22+
whenError: PartialFunction[Throwable, T],
23+
whenUnset: => T,
24+
whenPresent: String => T
25+
): Try[T] = {
26+
tryGet(key)
27+
.map(_.map(whenPresent).getOrElse(whenUnset))
28+
.recover(whenError)
29+
}
30+
31+
def tryGet(key: String): MaybeEnvVar = {
32+
val result = Try(System.getenv(key)).map(Option(_))
33+
accessedEnvVars += key -> result
34+
result
35+
}
36+
37+
sealed trait EnvVarResult {
38+
def toString: String
39+
}
40+
case class Present(value: String) extends EnvVarResult {
41+
override def toString: String = value
42+
}
43+
case class Inaccessible(message: String) extends EnvVarResult {
44+
override val toString: String = s"<inaccessible: $message>"
45+
}
46+
object Inaccessible {
47+
def apply(throwable: Throwable): Inaccessible = Inaccessible(throwable.getMessage)
48+
}
49+
case object Unset extends EnvVarResult {
50+
override val toString: String = "<unset>"
51+
}
52+
case class Error(message: String) extends EnvVarResult {
53+
override val toString: String = s"<error: $message>"
54+
}
55+
object Error {
56+
def apply(throwable: Throwable): Error = Error(throwable.getMessage)
57+
}
58+
}

src/main/scala/com/target/data_validator/Main.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.target.data_validator
33
import java.util.Properties
44

55
import com.typesafe.scalalogging.LazyLogging
6-
import org.apache.log4j.PropertyConfigurator
6+
import org.apache.log4j.{Level, Logger, PropertyConfigurator}
77
import org.apache.spark.sql.SparkSession
88
import scopt.OptionParser
99

@@ -93,6 +93,7 @@ object Main extends LazyLogging with EventLog {
9393
if (mainConfig.verbose) {
9494
logger.info("Verbose Flag detected")
9595
logger.info(s"Original config: $origConfig")
96+
Logger.getRootLogger.setLevel(Level.DEBUG)
9697
}
9798

9899
// Resolve config

src/main/scala/com/target/data_validator/ValidatorConfig.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.target.data_validator
22

33
import java.net.InetAddress
44

5+
import com.target.data_validator.EnvironmentVariables.MaybeEnvVar
56
import com.typesafe.scalalogging.LazyLogging
67
import io.circe.Json
78
import io.circe.generic.auto._
@@ -101,8 +102,12 @@ object ValidatorConfig {
101102
}
102103

103104
private def envToJson: Json = {
104-
val env = System.getenv.asScala.toList.map(x => (x._1, Json.fromString(x._2)))
105-
Json.obj(env: _*)
105+
def extractFromAccessionList(pair: (String, MaybeEnvVar)) = {
106+
pair._1 -> Json.fromString(pair._2.map(_.getOrElse("<unset>")).getOrElse("<inaccessible>"))
107+
}
108+
109+
val env = EnvironmentVariables.accessedEnvVars.map(extractFromAccessionList)
110+
Json.obj(env.toSeq: _*)
106111
}
107112

108113
private def runtimeInfoJson(spark: SparkSession): Json = {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.target.data_validator
2+
3+
import com.target.data_validator.EnvironmentVariables.{Inaccessible, Present, Unset}
4+
import org.scalatest.{Matchers, WordSpec}
5+
6+
class EnvironmentVariablesSpec extends WordSpec with Matchers {
7+
8+
"EnvironmentVariables" should {
9+
"get envvars" when {
10+
"an envvar exists" in {
11+
EnvironmentVariables.get("HOME") should be(Present(System.getenv("HOME")))
12+
}
13+
"an envvar doesn't exist" in {
14+
EnvironmentVariables.get("NOPE") should be(Unset)
15+
}
16+
"an envvar isn't an envvar" in {
17+
EnvironmentVariables.get(null) shouldBe a[Inaccessible] // scalastyle:ignore
18+
}
19+
}
20+
"log envvars" when {
21+
"using get" in {
22+
EnvironmentVariables.get("HOME")
23+
EnvironmentVariables.accessedEnvVars.keySet should contain ("HOME")
24+
}
25+
"using tryGet" in {
26+
EnvironmentVariables.tryGet("HOME")
27+
EnvironmentVariables.accessedEnvVars.keySet should contain ("HOME")
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)