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

@ -116,7 +116,9 @@ public abstract class StoreEntryComp extends SimpleComp {
var loading = LoadingOverlayComp.noProgress(
Comp.of(() -> button),
wrapper.getBusy().or(wrapper.getEntry().getProvider().busy(wrapper)));
wrapper.getEntry().getValidity().isUsable() ?
wrapper.getBusy().or(wrapper.getEntry().getProvider().busy(wrapper)):
wrapper.getBusy());
return loading.createRegion();
}

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);

View file

@ -5,6 +5,7 @@ import io.xpipe.app.ext.ActionProvider;
import io.xpipe.app.storage.DataStoreEntry;
import io.xpipe.app.storage.DataStoreEntryRef;
import io.xpipe.core.process.CommandControl;
import io.xpipe.core.process.ElevationFunction;
import io.xpipe.core.process.ShellControl;
import io.xpipe.core.process.ShellDialects;
import io.xpipe.core.store.LocalStore;
@ -125,7 +126,7 @@ public class SampleAction implements ActionProvider {
// by using the information from the connection store.
// You can also set a custom working directory.
try (CommandControl cc = sc.command("kill <pid>")
.elevated("kill")
.elevated(ElevationFunction.elevated("kill"))
.withWorkingDirectory("/")
.start()) {
// Discard any output but throw an exception with the stderr contents if the exit code is not 0