-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TLS - Prevent Duplicate Entries in .env File
Previously, when generating local certificates, running the command multiple times could result in duplicate lines being written to the `.env` file. This commit resolves the issue by implementing the same mechanism used in the Let's Encrypt commands, ensuring that duplicate entries are avoided.
- Loading branch information
1 parent
2105d46
commit 1aa9508
Showing
3 changed files
with
64 additions
and
42 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/DotEnvHelper.java
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,48 @@ | ||
package io.quarkus.tls.cli; | ||
|
||
import static io.quarkus.tls.cli.letsencrypt.LetsEncryptConstants.DOT_ENV_FILE; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DotEnvHelper { | ||
|
||
private DotEnvHelper() { | ||
// Avoid direct instantiation | ||
} | ||
|
||
public static List<String> readDotEnvFile() throws IOException { | ||
if (!DOT_ENV_FILE.exists()) { | ||
return new ArrayList<>(); | ||
} | ||
return new ArrayList<>(Files.readAllLines(DOT_ENV_FILE.toPath())); | ||
} | ||
|
||
public static void addOrReplaceProperty(List<String> content, String key, String value) { | ||
var line = hasLine(content, key); | ||
if (line != -1) { | ||
content.set(line, key + "=" + value); | ||
} else { | ||
content.add(key + "=" + value); | ||
} | ||
} | ||
|
||
private static int hasLine(List<String> content, String key) { | ||
for (int i = 0; i < content.size(); i++) { | ||
if (content.get(i).startsWith(key + "=") || content.get(i).startsWith(key + " =")) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
public static void deleteQuietly(File file) { | ||
if (file.isFile()) { | ||
file.delete(); | ||
} | ||
} | ||
} |
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
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