forked from cdimascio/dotenv-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotenvBuilder.java
More file actions
132 lines (113 loc) · 4.07 KB
/
DotenvBuilder.java
File metadata and controls
132 lines (113 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package io.github.cdimascio.dotenv;
import io.github.cdimascio.dotenv.internal.DotenvParser;
import io.github.cdimascio.dotenv.internal.DotenvReader;
import java.util.*;
import static java.util.stream.Collectors.*;
/**
* Builds and loads and {@link Dotenv} instance.
* @see Dotenv#configure()
*/
public class DotenvBuilder {
private String filename = ".env";
private String directoryPath = "./";
private boolean systemProperties = false;
private boolean throwIfMissing = true;
private boolean throwIfMalformed = true;
/**
* Sets the directory containing the .env file.
* @param path the directory containing the .env file
* @return this {@link DotenvBuilder}
*/
public DotenvBuilder directory(final String path) {
this.directoryPath = path;
return this;
}
/**
* Sets the name of the .env file. The default is .env.
* @param name the filename
* @return this {@link DotenvBuilder}
*/
public DotenvBuilder filename(final String name) {
filename = name;
return this;
}
/**
* Does not throw an exception when .env is missing.
* @return this {@link DotenvBuilder}
*/
public DotenvBuilder ignoreIfMissing() {
throwIfMissing = false;
return this;
}
/**
* Does not throw an exception when .env is malformed.
* @return this {@link DotenvBuilder}
*/
public DotenvBuilder ignoreIfMalformed() {
throwIfMalformed = false;
return this;
}
/**
* Sets each environment variable as system properties.
* @return this {@link DotenvBuilder}
*/
public DotenvBuilder systemProperties() {
systemProperties = true;
return this;
}
/**
* Load the contents of .env into the virtual environment.
* @return a new {@link Dotenv} instance
* @throws DotenvException when an error occurs
*/
public Dotenv load() throws DotenvException {
final var reader = new DotenvParser(
new DotenvReader(directoryPath, filename),
throwIfMissing, throwIfMalformed);
final List<DotenvEntry> env = reader.parse();
if (systemProperties) {
env.forEach(it -> System.setProperty(it.getKey(), it.getValue()));
}
return new DotenvImpl(env);
}
static class DotenvImpl implements Dotenv {
private final Map<String, String> envVars;
private final Set<DotenvEntry> set;
private final Set<DotenvEntry> setInFile;
public DotenvImpl(final List<DotenvEntry> envVars) {
final Map<String, String> envVarsInFile =
envVars.stream()
.collect(toMap(DotenvEntry::getKey, DotenvEntry::getValue, (a, b) -> b));
this.envVars = new HashMap<>(envVarsInFile);
this.envVars.putAll(System.getenv());
this.set =
this.envVars.entrySet()
.stream()
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
this.setInFile =
envVarsInFile.entrySet()
.stream()
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
}
@Override
public Set<DotenvEntry> entries() {
return set;
}
@Override
public Set<DotenvEntry> entries(final Dotenv.Filter filter) {
return filter == null ? entries() : setInFile;
}
@Override
public String get(final String key) {
final String value = System.getenv(key);
return value == null ? envVars.get(key) : value;
}
@Override
public String get(String key, String defaultValue) {
final String value = this.get(key);
return value == null ? defaultValue : value;
}
}
}