Rework elevation function

This commit is contained in:
crschnick
2024-04-29 19:38:22 +00:00
parent 9c3eaa479c
commit 733df4c005
6 changed files with 76 additions and 18 deletions

View File

@@ -1,7 +1,6 @@
package io.xpipe.core.process;
import io.xpipe.core.util.FailableConsumer;
import io.xpipe.core.util.FailableFunction;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -64,11 +63,7 @@ public interface CommandControl extends ProcessControl {
long getExitCode();
default CommandControl elevated(String message) {
return elevated(message, (v) -> true);
}
CommandControl elevated(String message, FailableFunction<ShellControl, Boolean, Exception> elevationFunction);
CommandControl elevated(ElevationFunction function);
void withStdoutOrThrow(FailableConsumer<InputStreamReader, Exception> c);

View File

@@ -1,9 +0,0 @@
package io.xpipe.core.process;
import lombok.Value;
@Value
public class ElevationConfig {
boolean requiresPassword;
}

View File

@@ -0,0 +1,69 @@
package io.xpipe.core.process;
import io.xpipe.core.util.FailableFunction;
public interface ElevationFunction {
static ElevationFunction of(String prefix, FailableFunction<ShellControl, Boolean, Exception> f) {
return new ElevationFunction() {
@Override
public String getPrefix() {
return prefix;
}
@Override
public boolean isSpecified() {
return true;
}
@Override
public boolean apply(ShellControl shellControl) throws Exception {
return f.apply(shellControl);
}
};
}
static ElevationFunction elevated(String prefix) {
return new ElevationFunction() {
@Override
public String getPrefix() {
return prefix;
}
@Override
public boolean isSpecified() {
return true;
}
@Override
public boolean apply(ShellControl shellControl) {
return true;
}
};
}
static ElevationFunction none() {
return new ElevationFunction() {
@Override
public String getPrefix() {
return null;
}
@Override
public boolean isSpecified() {
return false;
}
@Override
public boolean apply(ShellControl shellControl) {
return false;
}
};
}
String getPrefix();
boolean isSpecified();
boolean apply(ShellControl shellControl) throws Exception;
}

View File

@@ -171,7 +171,7 @@ public interface ShellControl extends ProcessControl {
OsType.Any getOsType();
ShellControl elevated(String message, FailableFunction<ShellControl, Boolean, Exception> elevationFunction);
ShellControl elevated(ElevationFunction elevationFunction);
ShellControl withInitSnippet(ScriptSnippet snippet);