-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carmine DiMascio
committed
Nov 25, 2017
0 parents
commit 429a517
Showing
7 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
### JetBrains template | ||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm | ||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
||
# User-specific stuff: | ||
.idea/**/workspace.xml | ||
.idea/**/tasks.xml | ||
.idea/dictionaries | ||
|
||
# Sensitive or high-churn files: | ||
.idea/**/dataSources/ | ||
.idea/**/dataSources.ids | ||
.idea/**/dataSources.xml | ||
.idea/**/dataSources.local.xml | ||
.idea/**/sqlDataSources.xml | ||
.idea/**/dynamic.xml | ||
.idea/**/uiDesigner.xml | ||
|
||
# Gradle: | ||
.idea/**/gradle.xml | ||
.idea/**/libraries | ||
|
||
# CMake | ||
cmake-build-debug/ | ||
|
||
# Mongo Explorer plugin: | ||
.idea/**/mongoSettings.xml | ||
|
||
## File-based project format: | ||
*.iws | ||
|
||
## Plugin-specific files: | ||
|
||
# IntelliJ | ||
out/ | ||
|
||
# mpeltonen/sbt-idea plugin | ||
.idea_modules/ | ||
|
||
# JIRA plugin | ||
atlassian-ide-plugin.xml | ||
|
||
# Cursive Clojure plugin | ||
.idea/replstate.xml | ||
|
||
# Crashlytics plugin (for Android Studio and IntelliJ) | ||
com_crashlytics_export_strings.xml | ||
crashlytics.properties | ||
crashlytics-build.properties | ||
fabric.properties | ||
### Kotlin template | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
### Maven template | ||
target/ | ||
pom.xml.tag | ||
pom.xml.releaseBackup | ||
pom.xml.versionsBackup | ||
pom.xml.next | ||
release.properties | ||
dependency-reduced-pom.xml | ||
buildNumber.properties | ||
.mvn/timing.properties | ||
|
||
# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) | ||
!/.mvn/wrapper/maven-wrapper.jar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="FacetManager"> | ||
<facet type="kotlin-language" name="Kotlin"> | ||
<configuration version="3" platform="JVM 1.6" useProjectSettings="false"> | ||
<compilerSettings /> | ||
<compilerArguments> | ||
<option name="languageVersion" value="1.1" /> | ||
<option name="apiVersion" value="1.1" /> | ||
<option name="pluginOptions"> | ||
<array /> | ||
</option> | ||
<option name="pluginClasspaths"> | ||
<array /> | ||
</option> | ||
</compilerArguments> | ||
</configuration> | ||
</facet> | ||
</component> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="KotlinJavaRuntime" level="project" /> | ||
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib:1.1.61" level="project" /> | ||
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.jetbrains.kotlin:kotlin-test-junit:1.1.61" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.jetbrains.kotlin:kotlin-test:1.1.61" level="project" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.cdimascio</groupId> | ||
<artifactId>java-dotenv</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<kotlin.version>1.1.61</kotlin.version> | ||
<main.class>io.cdimascio.DotenvKt</main.class> | ||
<junit.version>4.12</junit.version> | ||
|
||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-stdlib</artifactId> | ||
<version>${kotlin.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-test-junit</artifactId> | ||
<version>${kotlin.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> | ||
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> | ||
<plugins> | ||
<plugin> | ||
<artifactId>kotlin-maven-plugin</artifactId> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<version>${kotlin.version}</version> | ||
|
||
<executions> | ||
<execution> | ||
<id>compile</id> | ||
<goals> <goal>compile</goal> </goals> | ||
</execution> | ||
|
||
<execution> | ||
<id>test-compile</id> | ||
<goals> <goal>test-compile</goal> </goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>2.6</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<addClasspath>true</addClasspath> | ||
<mainClass>${main.class}</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.20.1</version> | ||
<configuration> | ||
<workingDirectory>${project.basedir}</workingDirectory> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package io.cdimascio | ||
|
||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import java.util.stream.Collectors | ||
import java.util.stream.Stream | ||
|
||
interface Dotenv { | ||
companion object Factory { | ||
fun configure(): DotenvBuilder = DotenvBuilder() | ||
} | ||
fun get(envVar: String): String? | ||
} | ||
|
||
class DotEnvException : Exception { | ||
constructor(message: String): super(message) | ||
constructor(throwable: Throwable): super(throwable) | ||
} | ||
|
||
class DotenvBuilder internal constructor() { | ||
private var filename = ".env" | ||
private var directoryPath = System.getProperty("user.home") | ||
private var throwIfMissing = true | ||
private var throwIfMalformed = true | ||
|
||
fun withDirectory(path: String = directoryPath): DotenvBuilder { | ||
directoryPath = path | ||
return this | ||
} | ||
|
||
fun ingoreIfMissing(): DotenvBuilder { | ||
throwIfMissing = false | ||
return this | ||
} | ||
|
||
fun ignoreIfMalformed(): DotenvBuilder { | ||
throwIfMalformed = false | ||
return this | ||
} | ||
|
||
fun build(): Dotenv { | ||
val reader = DotEnvReader(directoryPath, filename, throwIfMalformed, throwIfMissing) | ||
val env = reader.read() | ||
// applyEnv(env) | ||
return DotenvImpl(env) | ||
} | ||
|
||
// private fun applyEnv(pairs: List<Pair<String, String>>) { | ||
// val processBuilder = ProcessBuilder() | ||
// val env = processBuilder.environment() | ||
// pairs.forEach { | ||
// println("applying to env ${it.first} ${it.second}") | ||
// env[it.first] = it.second | ||
// } | ||
// | ||
// } | ||
} | ||
|
||
private class DotenvImpl(envVars: List<Pair<String,String>>): Dotenv { | ||
val map = envVars.associateBy({ it.first }, { it.second }) | ||
|
||
override fun get(envVar: String): String? = map[envVar] ?: System.getenv(envVar) | ||
} | ||
|
||
private class DotEnvReader( | ||
val directory: String, | ||
val filename: String = ".env", | ||
val throwIfMalformed: Boolean, | ||
val throwIfMissing: Boolean | ||
) { | ||
private val commentHash = "#" | ||
private val commentSlashes = """//""" | ||
|
||
fun read() = parse() | ||
|
||
private fun parse(): List<Pair<String, String>> { | ||
val isWhiteSpace = { s: String -> """^\s*${'$'}""".toRegex().matches(s) } | ||
val isComment = { s: String -> s.startsWith(commentHash) || s.startsWith(commentSlashes) } | ||
val parseLine = { s: String -> """^\s*([\w.\-]+)\s*(=)\s*(.*)?\s*$""".toRegex().matchEntire(s) } | ||
|
||
val userSpecifiedPath = Paths.get(directory, filename) | ||
val cwd = Paths.get(System.getProperty("user.dir")) | ||
val path = cwd.resolve(userSpecifiedPath) | ||
val lines = | ||
try { Files.lines(path) } | ||
catch (e: Exception) { | ||
if (throwIfMissing) throw DotEnvException(e) | ||
else Stream.empty<String>() | ||
}.collect(Collectors.toList()) | ||
|
||
return lines | ||
.map { it.trim() } | ||
.filter { !isWhiteSpace(it) } | ||
.filter { !isComment(it) } | ||
.mapNotNull { | ||
val match = parseLine(it) | ||
if (match != null) { | ||
val (key, _, value) = match.destructured | ||
Pair(key, value) | ||
} else { | ||
if (throwIfMalformed) throw DotEnvException("Malformed entry: $it") | ||
else null | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.