-
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.
- Loading branch information
Showing
8 changed files
with
164 additions
and
77 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
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
44 changes: 44 additions & 0 deletions
44
devtools/cli/src/main/java/io/quarkus/cli/config/RemoveConfig.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,44 @@ | ||
package io.quarkus.cli.config; | ||
|
||
import java.io.BufferedWriter; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
|
||
import io.smallrye.config.ConfigValue; | ||
import picocli.CommandLine; | ||
|
||
@CommandLine.Command(name = "remove", header = "Removes a configuration in application.properties") | ||
public class RemoveConfig extends BaseConfigCommand implements Callable<Integer> { | ||
@CommandLine.Parameters(index = "0", arity = "1", paramLabel = "NAME", description = "Configuration name") | ||
String name; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
Path properties = projectRoot().resolve("src/main/resources/application.properties"); | ||
if (!properties.toFile().exists()) { | ||
System.out.println("Could not find an application.properties file"); | ||
return 0; | ||
} | ||
|
||
List<String> lines = Files.readAllLines(properties); | ||
|
||
ConfigValue configValue = findKey(lines, name); | ||
if (configValue.getLineNumber() != -1) { | ||
System.out.println("Removing configuration " + name); | ||
lines.remove(configValue.getLineNumber()); | ||
} else { | ||
System.out.println("Could not find configuration " + name); | ||
} | ||
|
||
try (BufferedWriter writer = Files.newBufferedWriter(properties)) { | ||
for (String i : lines) { | ||
writer.write(i); | ||
writer.newLine(); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
devtools/cli/src/test/java/io/quarkus/cli/config/RemoveConfigTest.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,56 @@ | ||
package io.quarkus.cli.config; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Properties; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import io.quarkus.cli.CliDriver; | ||
import io.smallrye.config.PropertiesConfigSource; | ||
import io.smallrye.config.SmallRyeConfig; | ||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
|
||
public class RemoveConfigTest { | ||
@TempDir | ||
Path tempDir; | ||
|
||
@BeforeEach | ||
void setUp() throws Exception { | ||
Path resources = tempDir.resolve("src/main/resources"); | ||
Files.createDirectories(resources); | ||
Files.createFile(resources.resolve("application.properties")); | ||
} | ||
|
||
@Test | ||
void removeConfiguration() throws Exception { | ||
Path propertiesFile = tempDir.resolve("src/main/resources/application.properties"); | ||
Properties properties = new Properties(); | ||
try (InputStream inputStream = propertiesFile.toUri().toURL().openStream()) { | ||
properties.load(inputStream); | ||
} | ||
properties.put("foo.bar", "1234"); | ||
try (FileOutputStream outputStream = new FileOutputStream(propertiesFile.toFile())) { | ||
properties.store(outputStream, ""); | ||
} | ||
CliDriver.Result result = CliDriver.execute(tempDir, "config", "remove", "foo.bar"); | ||
assertEquals(0, result.getExitCode()); | ||
assertTrue(result.getStdout().contains("Removing configuration foo.bar")); | ||
assertTrue(config().getOptionalValue("foo.bar", String.class).isEmpty()); | ||
} | ||
|
||
private SmallRyeConfig config() throws Exception { | ||
PropertiesConfigSource propertiesConfigSource = new PropertiesConfigSource( | ||
tempDir.resolve("src/main/resources/application.properties").toUri().toURL()); | ||
return new SmallRyeConfigBuilder() | ||
.withSources(propertiesConfigSource) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.