Rework on shells

This commit is contained in:
Christopher Schnick 2022-12-05 00:50:58 +01:00
parent 40df1a3233
commit 936095aa24
8 changed files with 104 additions and 33 deletions

View file

@ -11,6 +11,8 @@ public interface OsType {
Linux LINUX = new Linux();
Mac MAC = new Mac();
String getScriptFileEnding();
String getName();
String getTempDirectory(ShellProcessControl pc) throws Exception;
@ -40,6 +42,11 @@ public interface OsType {
static class Windows implements OsType {
@Override
public String getScriptFileEnding() {
return "bat";
}
@Override
public String getName() {
return "Windows";
@ -98,6 +105,11 @@ public interface OsType {
return String.join("/", file.split("[\\\\/]+"));
}
@Override
public String getScriptFileEnding() {
return "sh";
}
@Override
public String getName() {
return "Linux";
@ -168,6 +180,11 @@ public interface OsType {
return String.join("/", file.split("[\\\\/]+"));
}
@Override
public String getScriptFileEnding() {
return "sh";
}
@Override
public String getName() {
return "Mac";

View file

@ -11,6 +11,12 @@ import java.util.stream.Collectors;
public interface ShellProcessControl extends ProcessControl {
default String prepareOpen() throws Exception {
return prepareOpen(null);
}
String prepareOpen(String content) throws Exception;
default String executeSimpleCommand(String command) throws Exception {
try (CommandProcessControl c = command(command).start()) {
return c.readOrThrow();

View file

@ -10,6 +10,10 @@ import java.util.stream.Collectors;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface ShellType {
String createInitFileContent(String command);
List<String> getOpenWithInitFileCommand(String file);
default String flatten(List<String> command) {
return command.stream().map(s -> s.contains(" ") ? "\"" + s + "\"" : s).collect(Collectors.joining(" "));
}
@ -65,5 +69,7 @@ public interface ShellType {
String getDisplayName();
String getExecutable();
boolean echoesInput();
}

View file

@ -1,30 +0,0 @@
package io.xpipe.core.store;
public class DataStoreFormatter {
public static String ellipsis(String input, int length) {
if (input == null) {
return "";
}
var end = Math.min(input.length(), length);
if (end < input.length()) {
return input.substring(0, end) + "...";
}
return input;
}
public static String specialFormatHostName(String input) {
if (input.contains(":")) {
input = input.split(":")[0];
}
if (input.endsWith(".rds.amazonaws.com")) {
var split = input.split("\\.");
var name = split[0];
var region = split[2];
return String.format("RDS %s @ %s", name, region);
}
return null;
}
}

View file

@ -46,7 +46,7 @@ public class CoreJacksonModule extends SimpleModule {
new NamedType(WildcardType.class),
new NamedType(ShellTypes.Cmd.class),
new NamedType(ShellTypes.PowerShell.class),
new NamedType(ShellTypes.Sh.class),
new NamedType(ShellTypes.PosixBase.class),
new NamedType(BaseQueryElement.class),
new NamedType(ChoiceElement.class),
new NamedType(BusyElement.class),

View file

@ -31,6 +31,10 @@ public interface DataStoreActionProvider<T extends DataStore> {
Class<T> getApplicableClass();
default boolean isMajor() {
return false;
}
default boolean isActive() throws Exception {
return true;
}

View file

@ -30,11 +30,16 @@ public interface PrefsChoiceValue extends Translatable {
}
@SuppressWarnings("unchecked")
static <T extends PrefsChoiceValue> List<T> getSupported(Class<T> type) {
static <T> List<T> getSupported(Class<T> type) {
try {
return (List<T>) type.getDeclaredField("SUPPORTED").get(null);
} catch (IllegalAccessException | NoSuchFieldException e) {
return getAll(type).stream().filter(t -> t.isSupported()).toList();
var all = getAll(type);
if (all == null) {
throw new AssertionError();
}
return all.stream().filter(t -> ((PrefsChoiceValue)t).isSupported()).toList();
}
}

View file

@ -0,0 +1,63 @@
package io.xpipe.extension.util;
import io.xpipe.core.store.DataStore;
import io.xpipe.core.store.ShellStore;
import io.xpipe.extension.DataStoreProviders;
import java.util.function.IntFunction;
public class DataStoreFormatter {
public static String formatAtHost(IntFunction<String> func, DataStore at, int length) {
var atString = at instanceof ShellStore shellStore && !ShellStore.isLocal(shellStore)
? DataStoreProviders.byStore(at).toSummaryString(at, length)
: null;
if (atString == null) {
return func.apply(length);
}
var fileString = func.apply(length - atString.length() - 3);
return String.format("%s @ %s", fileString, atString);
}
public static String format(DataStore input, int length) {
var named = XPipeDaemon.getInstance().getStoreName(input);
if (named.isPresent()) {
return cut(named.get(), length);
}
return DataStoreProviders.byStore(input).toSummaryString(input, length);
}
public static String cut(String input, int length) {
if (input == null) {
return "";
}
var end = Math.min(input.length(), length);
if (end < input.length()) {
return input.substring(0, end) + "...";
}
return input;
}
public static String formatHostName(String input, int length) {
// Remove port
if (input.contains(":")) {
input = input.split(":")[0];
}
// Check for amazon web services
if (input.endsWith(".rds.amazonaws.com")) {
var split = input.split("\\.");
var name = split[0];
var region = split[2];
var lengthShare = (length - 3) / 2;
return String.format(
"%s @ %s",
DataStoreFormatter.cut(name, lengthShare), DataStoreFormatter.cut(region, length - lengthShare));
}
return cut(input, length);
}
}