Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer a recurseParent() config option #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DotenvBuilder {
private boolean systemProperties = false;
private boolean throwIfMissing = true;
private boolean throwIfMalformed = true;
private boolean recurse = false;

/**
* Sets the directory containing the .env file.
Expand Down Expand Up @@ -55,6 +56,16 @@ public DotenvBuilder ignoreIfMalformed() {
return this;
}


/**
* Recursively search parent directories for a .env file
* @return this {@link DotenvBuilder}
*/
public DotenvBuilder recurseParents() {
recurse = true;
return this;
}

/**
* Sets each environment variable as system properties.
* @return this {@link DotenvBuilder}
Expand All @@ -71,7 +82,7 @@ public DotenvBuilder systemProperties() {
*/
public Dotenv load() throws DotenvException {
DotenvParser reader = new DotenvParser(
new DotenvReader(directoryPath, filename),
new DotenvReader(directoryPath, filename, recurse),
throwIfMissing,
throwIfMalformed);
List<DotenvEntry> env = reader.parse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
public class DotenvReader {
private final String directory;
private final String filename;
private final boolean recurse;

/**
* Creates a dotenv reader
* @param directory the directory containing the .env file
* @param filename the file name of the .env file e.g. .env
* @param recurse
*/
public DotenvReader(String directory, String filename) {
public DotenvReader(String directory, String filename, boolean recurse) {
this.directory = directory;
this.filename = filename;
this.recurse = recurse;
}

/**
Expand All @@ -50,6 +53,18 @@ public List<String> read() throws DotenvException, IOException {
return Files
.lines(path)
.collect(Collectors.toList());
} else if (recurse) {
Path parent = path.getParent().getParent(); // get the parent of the parent of the current .env file
while (parent != null) {
Path fileInParent = parent.resolve(filename); // resolve a .env file in it
if (Files.exists(fileInParent)) {
return Files
.lines(fileInParent)
.collect(Collectors.toList());
} else {
parent = parent.getParent();
}
}
}

try {
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/tests/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,16 @@ private void assertHostEnvVar(Dotenv env) {
assertEquals(expectedHome, actualHome);
}
}

@Test
public void parentDirectory() {
var dotenv = Dotenv.configure()
.directory("./src/test/resources/parent_demo")
.recurseParents()
.ignoreIfMalformed()
.load();
assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));

assertHostEnvVar(dotenv);
}
}
Empty file.