-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kv-store): introduce CLI commands
- Loading branch information
1 parent
38b72a2
commit 1b6e853
Showing
4 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
cli/src/main/java/io/kestra/cli/commands/namespaces/kv/KvCommand.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,29 @@ | ||
package io.kestra.cli.commands.namespaces.kv; | ||
|
||
import io.kestra.cli.AbstractCommand; | ||
import io.kestra.cli.App; | ||
import io.micronaut.configuration.picocli.PicocliRunner; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import picocli.CommandLine; | ||
|
||
@CommandLine.Command( | ||
name = "kv", | ||
description = "handle KV Store", | ||
mixinStandardHelpOptions = true, | ||
subcommands = { | ||
KvUpdateCommand.class, | ||
} | ||
) | ||
@Slf4j | ||
public class KvCommand extends AbstractCommand { | ||
@SneakyThrows | ||
@Override | ||
public Integer call() throws Exception { | ||
super.call(); | ||
|
||
PicocliRunner.call(App.class, "namespace", "kv", "--help"); | ||
|
||
return 0; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
cli/src/main/java/io/kestra/cli/commands/namespaces/kv/KvUpdateCommand.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,85 @@ | ||
package io.kestra.cli.commands.namespaces.kv; | ||
|
||
import io.kestra.cli.AbstractApiCommand; | ||
import io.kestra.cli.AbstractValidateCommand; | ||
import io.kestra.core.services.KVStoreService; | ||
import io.kestra.core.storages.kv.KVMetadata; | ||
import io.kestra.core.storages.kv.KVStoreValueWrapper; | ||
import io.kestra.core.utils.KestraIgnore; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.MediaType; | ||
import io.micronaut.http.MutableHttpRequest; | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException; | ||
import io.micronaut.http.client.multipart.MultipartBody; | ||
import io.micronaut.http.client.netty.DefaultHttpClient; | ||
import jakarta.inject.Inject; | ||
import lombok.extern.slf4j.Slf4j; | ||
import picocli.CommandLine; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
@CommandLine.Command( | ||
name = "update", | ||
description = "update value for a KV Store key", | ||
mixinStandardHelpOptions = true | ||
) | ||
@Slf4j | ||
public class KvUpdateCommand extends AbstractApiCommand { | ||
private static final Pattern STRING_PATTERN = Pattern.compile("^(?![\\d{\\[\"]+)(?!false)(?!true)(?!P(?=[^T]|T.)(?:\\d*D)?(?:T(?=.)(?:\\d*H)?(?:\\d*M)?(?:\\d*S)?)?).*$"); | ||
|
||
@CommandLine.Parameters(index = "0", description = "the namespace to update") | ||
public String namespace; | ||
|
||
@CommandLine.Parameters(index = "1", description = "the key to update") | ||
public String key; | ||
|
||
@CommandLine.Parameters(index = "2", description = "the value to assign to the key. If the value is an object, it must be in JSON format. If the value must be read from file, use -f parameter.") | ||
public String value; | ||
|
||
@CommandLine.Option(names = {"-e", "--expiration"}, description = "the duration after which the key should expire.") | ||
public String expiration; | ||
|
||
@CommandLine.Option(names = {"-t", "--type"}, description = "the type of the value. Optional and useful to override the deduced type (eg. numbers, booleans or JSON as full string). Must be one of STRING, NUMBER, BOOLEAN, DATETIME, DATE, DURATION, JSON.") | ||
public String type; | ||
|
||
@CommandLine.Option(names = {"-f", "--file-value"}, description = "the file from which to read the value to set. If this is provided, it will take precedence over any specified value.") | ||
public Path fileValue; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
super.call(); | ||
|
||
if (fileValue != null) { | ||
value = Files.readString(Path.of(fileValue.toString().trim())); | ||
} | ||
|
||
String formattedValue = value; | ||
if (type != null) { | ||
if (type.trim().equals("STRING") && !value.startsWith("\"")) { | ||
formattedValue = "\"" + value.replace("\"", "\\\"") + "\""; | ||
} | ||
} else if (STRING_PATTERN.matcher(value).matches()) { | ||
formattedValue = "\"" + value + "\""; | ||
} | ||
|
||
Duration ttl = expiration == null ? null : Duration.parse(expiration); | ||
String trimmedValue = formattedValue.trim(); | ||
boolean isJson = trimmedValue.startsWith("{") && trimmedValue.endsWith("}") || trimmedValue.startsWith("[") && trimmedValue.endsWith("]"); | ||
MutableHttpRequest<String> request = HttpRequest.PUT(apiUri("/namespaces/") + namespace + "/kv/" + key, formattedValue) | ||
.contentType(isJson ? MediaType.APPLICATION_JSON_TYPE : MediaType.TEXT_PLAIN_TYPE); | ||
|
||
if (ttl != null) { | ||
request.header("ttl", ttl.toString()); | ||
} | ||
|
||
try (DefaultHttpClient client = client()) { | ||
client.toBlocking().exchange(this.requestOptions(request)); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
cli/src/test/java/io/kestra/cli/commands/namespaces/kv/KvCommandTest.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,29 @@ | ||
package io.kestra.cli.commands.namespaces.kv; | ||
|
||
import io.kestra.cli.commands.namespaces.files.NamespaceFilesCommand; | ||
import io.micronaut.configuration.picocli.PicocliRunner; | ||
import io.micronaut.context.ApplicationContext; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.core.Is.is; | ||
|
||
class KvCommandTest { | ||
@Test | ||
void runWithNoParam() { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(out)); | ||
|
||
try (ApplicationContext ctx = ApplicationContext.builder().deduceEnvironment(false).start()) { | ||
String[] args = {}; | ||
Integer call = PicocliRunner.call(NamespaceFilesCommand.class, ctx, args); | ||
|
||
assertThat(call, is(0)); | ||
assertThat(out.toString(), containsString("Usage: kestra namespace kv")); | ||
Check failure on line 26 in cli/src/test/java/io/kestra/cli/commands/namespaces/kv/KvCommandTest.java GitHub Actions / JUnit Test ReportKvCommandTest.runWithNoParam()
Raw output
|
||
} | ||
} | ||
} |
184 changes: 184 additions & 0 deletions
184
cli/src/test/java/io/kestra/cli/commands/namespaces/kv/KvUpdateCommandTest.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,184 @@ | ||
package io.kestra.cli.commands.namespaces.kv; | ||
|
||
import io.kestra.core.exceptions.ResourceExpiredException; | ||
import io.kestra.core.services.KVStoreService; | ||
import io.kestra.core.storages.kv.KVStore; | ||
import io.micronaut.configuration.picocli.PicocliRunner; | ||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.context.env.Environment; | ||
import io.micronaut.runtime.server.EmbeddedServer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
class KvUpdateCommandTest { | ||
@Test | ||
void string() throws IOException, ResourceExpiredException { | ||
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) { | ||
|
||
EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class); | ||
embeddedServer.start(); | ||
|
||
String[] args = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.cli", | ||
"string", | ||
"stringValue" | ||
}; | ||
PicocliRunner.call(KvUpdateCommand.class, ctx, args); | ||
|
||
KVStoreService kvStoreService = ctx.getBean(KVStoreService.class); | ||
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null); | ||
|
||
assertThat(kvStore.get("string").get(), is("stringValue")); | ||
assertThat(kvStore.getRaw("string").get(), is("\"stringValue\"")); | ||
} | ||
} | ||
|
||
@Test | ||
void integer() throws IOException, ResourceExpiredException { | ||
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) { | ||
|
||
EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class); | ||
embeddedServer.start(); | ||
|
||
String[] args = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.cli", | ||
"int", | ||
"1" | ||
}; | ||
PicocliRunner.call(KvUpdateCommand.class, ctx, args); | ||
|
||
KVStoreService kvStoreService = ctx.getBean(KVStoreService.class); | ||
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null); | ||
|
||
assertThat(kvStore.get("int").get(), is(1)); | ||
assertThat(kvStore.getRaw("int").get(), is("1")); | ||
} | ||
} | ||
|
||
@Test | ||
void integerStr() throws IOException, ResourceExpiredException { | ||
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) { | ||
|
||
EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class); | ||
embeddedServer.start(); | ||
|
||
String[] args = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.cli", | ||
"intStr", | ||
"1", | ||
"-t STRING" | ||
}; | ||
PicocliRunner.call(KvUpdateCommand.class, ctx, args); | ||
|
||
KVStoreService kvStoreService = ctx.getBean(KVStoreService.class); | ||
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null); | ||
|
||
assertThat(kvStore.get("intStr").get(), is("1")); | ||
assertThat(kvStore.getRaw("intStr").get(), is("\"1\"")); | ||
} | ||
} | ||
|
||
@Test | ||
void object() throws IOException, ResourceExpiredException { | ||
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) { | ||
|
||
EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class); | ||
embeddedServer.start(); | ||
|
||
String[] args = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.cli", | ||
"object", | ||
"{\"some\":\"json\"}", | ||
}; | ||
PicocliRunner.call(KvUpdateCommand.class, ctx, args); | ||
|
||
KVStoreService kvStoreService = ctx.getBean(KVStoreService.class); | ||
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null); | ||
|
||
assertThat(kvStore.get("object").get(), is(Map.of("some", "json"))); | ||
assertThat(kvStore.getRaw("object").get(), is("{some:\"json\"}")); | ||
} | ||
} | ||
|
||
@Test | ||
void objectStr() throws IOException, ResourceExpiredException { | ||
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) { | ||
|
||
EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class); | ||
embeddedServer.start(); | ||
|
||
String[] args = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.cli", | ||
"objectStr", | ||
"{\"some\":\"json\"}", | ||
"-t STRING" | ||
}; | ||
PicocliRunner.call(KvUpdateCommand.class, ctx, args); | ||
|
||
KVStoreService kvStoreService = ctx.getBean(KVStoreService.class); | ||
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null); | ||
|
||
assertThat(kvStore.get("objectStr").get(), is("{\"some\":\"json\"}")); | ||
assertThat(kvStore.getRaw("objectStr").get(), is("\"{\\\"some\\\":\\\"json\\\"}\"")); | ||
} | ||
} | ||
|
||
@Test | ||
void fromFile() throws IOException, ResourceExpiredException { | ||
try (ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)) { | ||
|
||
EmbeddedServer embeddedServer = ctx.getBean(EmbeddedServer.class); | ||
embeddedServer.start(); | ||
|
||
File file = File.createTempFile("objectFromFile", ".json"); | ||
file.createNewFile(); | ||
file.deleteOnExit(); | ||
Files.write(file.toPath(), "{\"some\":\"json\",\"from\":\"file\"}".getBytes()); | ||
|
||
String[] args = { | ||
"--server", | ||
embeddedServer.getURL().toString(), | ||
"--user", | ||
"myuser:pass:word", | ||
"io.kestra.cli", | ||
"objectFromFile", | ||
"valueThatWillGetOverriden", | ||
"-f " + file.getAbsolutePath() | ||
}; | ||
PicocliRunner.call(KvUpdateCommand.class, ctx, args); | ||
|
||
KVStoreService kvStoreService = ctx.getBean(KVStoreService.class); | ||
KVStore kvStore = kvStoreService.namespaceKv(null, "io.kestra.cli", null); | ||
|
||
assertThat(kvStore.get("objectFromFile").get(), is(Map.of("some", "json", "from", "file"))); | ||
assertThat(kvStore.getRaw("objectFromFile").get(), is("{some:\"json\",from:\"file\"}")); | ||
} | ||
} | ||
} |