Skip to content

Commit

Permalink
Rework elevation function
Browse files Browse the repository at this point in the history
  • Loading branch information
crschnick committed Apr 29, 2024
1 parent 9c3eaa4 commit 733df4c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ protected final Region createSimple() {

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

Expand Down
7 changes: 1 addition & 6 deletions core/src/main/java/io/xpipe/core/process/CommandControl.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -64,11 +63,7 @@ default boolean executeAndCheck() throws Exception {

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

Expand Down
9 changes: 0 additions & 9 deletions core/src/main/java/io/xpipe/core/process/ElevationConfig.java

This file was deleted.

69 changes: 69 additions & 0 deletions core/src/main/java/io/xpipe/core/process/ElevationFunction.java
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion core/src/main/java/io/xpipe/core/process/ShellControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ String buildElevatedCommand(CommandConfiguration input, String prefix, UUID requ

OsType.Any getOsType();

ShellControl elevated(String message, FailableFunction<ShellControl, Boolean, Exception> elevationFunction);
ShellControl elevated(ElevationFunction elevationFunction);

ShellControl withInitSnippet(ScriptSnippet snippet);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -125,7 +126,7 @@ public void execute() throws Exception {
// 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
Expand Down

0 comments on commit 733df4c

Please sign in to comment.