mirror of
https://github.com/xpipe-io/xpipe.git
synced 2026-04-26 01:17:52 -04:00
Add validators, rework machine stores
This commit is contained in:
@@ -16,6 +16,10 @@ import java.util.Optional;
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
|
||||
public interface DataStore {
|
||||
|
||||
default boolean isComplete() {
|
||||
return true;
|
||||
}
|
||||
|
||||
default void validate() throws Exception {
|
||||
}
|
||||
|
||||
|
||||
@@ -13,22 +13,29 @@ import java.nio.file.Path;
|
||||
public class FileStore implements StreamDataStore, FilenameStore {
|
||||
|
||||
public static FileStore local(Path p) {
|
||||
return new FileStore(MachineStore.local(), p.toString());
|
||||
return new FileStore(MachineFileStore.local(), p.toString());
|
||||
}
|
||||
|
||||
public static FileStore local(String p) {
|
||||
return new FileStore(MachineStore.local(), p);
|
||||
return new FileStore(MachineFileStore.local(), p);
|
||||
}
|
||||
|
||||
MachineStore machine;
|
||||
MachineFileStore machine;
|
||||
String file;
|
||||
|
||||
@JsonCreator
|
||||
public FileStore(MachineStore machine, String file) {
|
||||
public FileStore(MachineFileStore machine, String file) {
|
||||
this.machine = machine;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate() throws Exception {
|
||||
if (!machine.exists(file)) {
|
||||
throw new IllegalStateException("File " + file + " could not be found on machine " + machine.toDisplay());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream openInput() throws Exception {
|
||||
return machine.openInput(file);
|
||||
@@ -40,7 +47,7 @@ public class FileStore implements StreamDataStore, FilenameStore {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canOpen() {
|
||||
public boolean canOpen() throws Exception {
|
||||
return machine.exists(file);
|
||||
}
|
||||
|
||||
@@ -56,6 +63,6 @@ public class FileStore implements StreamDataStore, FilenameStore {
|
||||
|
||||
@Override
|
||||
public String getFileName() {
|
||||
return Path.of(file).getFileName().toString();
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
package io.xpipe.core.store;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import lombok.Value;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
@JsonTypeName("local")
|
||||
public class LocalMachineStore implements MachineStore {
|
||||
@Value
|
||||
public class LocalStore implements ShellStore {
|
||||
|
||||
@Override
|
||||
public boolean exists(String file) {
|
||||
@@ -31,4 +35,19 @@ public class LocalMachineStore implements MachineStore {
|
||||
var p = Path.of(file);
|
||||
return Files.newOutputStream(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeAndRead(List<String> cmd) throws Exception {
|
||||
var p = prepare(cmd).redirectErrorStream(true);
|
||||
var proc = p.start();
|
||||
var b = proc.getInputStream().readAllBytes();
|
||||
proc.waitFor();
|
||||
//TODO
|
||||
return new String(b, StandardCharsets.UTF_16LE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> createCommand(List<String> cmd) {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,15 @@ package io.xpipe.core.store;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public interface MachineStore extends DataStore {
|
||||
public interface MachineFileStore extends DataStore {
|
||||
|
||||
static MachineStore local() {
|
||||
return new LocalMachineStore();
|
||||
static MachineFileStore local() {
|
||||
return new LocalStore();
|
||||
}
|
||||
|
||||
InputStream openInput(String file) throws Exception;
|
||||
|
||||
OutputStream openOutput(String file) throws Exception;
|
||||
|
||||
public boolean exists(String file);
|
||||
public boolean exists(String file) throws Exception;
|
||||
}
|
||||
19
core/src/main/java/io/xpipe/core/store/ShellStore.java
Normal file
19
core/src/main/java/io/xpipe/core/store/ShellStore.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package io.xpipe.core.store;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ShellStore extends MachineFileStore {
|
||||
|
||||
static ShellStore local() {
|
||||
return new LocalStore();
|
||||
}
|
||||
|
||||
default ProcessBuilder prepare(List<String> cmd) throws Exception {
|
||||
var toExec = createCommand(cmd);
|
||||
return new ProcessBuilder(toExec);
|
||||
}
|
||||
|
||||
String executeAndRead(List<String> cmd) throws Exception;
|
||||
|
||||
List<String> createCommand(List<String> cmd);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package io.xpipe.core.store;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
public interface StandardShellStore extends ShellStore {
|
||||
|
||||
static interface ShellType {
|
||||
|
||||
List<String> createFileReadCommand(String file);
|
||||
|
||||
List<String> createFileWriteCommand(String file);
|
||||
|
||||
List<String> createFileExistsCommand(String file);
|
||||
|
||||
Charset getCharset();
|
||||
|
||||
String getName();
|
||||
}
|
||||
|
||||
default String executeAndRead(List<String> cmd) throws Exception {
|
||||
var type = determineType();
|
||||
var p = prepare(cmd).redirectErrorStream(true);
|
||||
var proc = p.start();
|
||||
var s = new String(proc.getInputStream().readAllBytes(), type.getCharset());
|
||||
return s;
|
||||
}
|
||||
|
||||
List<String> createCommand(List<String> cmd);
|
||||
|
||||
ShellType determineType();
|
||||
|
||||
@Override
|
||||
default InputStream openInput(String file) throws Exception {
|
||||
var type = determineType();
|
||||
var cmd = type.createFileReadCommand(file);
|
||||
var p = prepare(cmd).redirectErrorStream(true);
|
||||
var proc = p.start();
|
||||
return proc.getInputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
default OutputStream openOutput(String file) throws Exception {
|
||||
var type = determineType();
|
||||
var cmd = type.createFileWriteCommand(file);
|
||||
var p = prepare(cmd).redirectErrorStream(true);
|
||||
var proc = p.start();
|
||||
return proc.getOutputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean exists(String file) throws Exception {
|
||||
var type = determineType();
|
||||
var cmd = type.createFileExistsCommand(file);
|
||||
var p = prepare(cmd).redirectErrorStream(true);
|
||||
var proc = p.start();
|
||||
return proc.waitFor() == 0;
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public interface StreamDataStore extends DataStore {
|
||||
throw new UnsupportedOperationException("Can't open store output");
|
||||
}
|
||||
|
||||
default boolean canOpen() {
|
||||
default boolean canOpen() throws Exception {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.xpipe.core.store;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Value;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -10,11 +10,15 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Value
|
||||
@JsonTypeName("string")
|
||||
@AllArgsConstructor
|
||||
public class StringStore implements StreamDataStore {
|
||||
|
||||
byte[] value;
|
||||
|
||||
@JsonCreator
|
||||
public StringStore(byte[] value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public StringStore(String s) {
|
||||
value = s.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public class CoreJacksonModule extends SimpleModule {
|
||||
new NamedType(LocalDirectoryDataStore.class),
|
||||
new NamedType(CollectionEntryDataStore.class),
|
||||
new NamedType(StringStore.class),
|
||||
new NamedType(LocalMachineStore.class),
|
||||
new NamedType(LocalStore.class),
|
||||
new NamedType(NamedStore.class),
|
||||
|
||||
new NamedType(ValueType.class),
|
||||
|
||||
Reference in New Issue
Block a user