Skip to content

Commit

Permalink
Merge pull request #15 from cryptomator/feature/reveal-path-service
Browse files Browse the repository at this point in the history
Feature: Implement Provider for ReveaPathService
  • Loading branch information
infeo authored Jan 18, 2023
2 parents bb66448 + 53ab327 commit 314bdcf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<project.jdk.version>17</project.jdk.version>

<!-- runtime dependencies -->
<api.version>1.1.0</api.version>
<api.version>1.2.0-beta4</api.version>
<slf4j.version>1.7.36</slf4j.version>

<!-- test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.cryptomator.macos.revealpath;

import org.cryptomator.integrations.common.OperatingSystem;
import org.cryptomator.integrations.common.Priority;
import org.cryptomator.integrations.revealpath.RevealFailedException;
import org.cryptomator.integrations.revealpath.RevealPathService;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Priority(100)
@OperatingSystem(OperatingSystem.Value.MAC)
public class OpenCmdRevealPathService implements RevealPathService {

@Override
public void reveal(Path p) throws RevealFailedException {
try {
var attrs = Files.readAttributes(p, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
ProcessBuilder pb = new ProcessBuilder().command("open", attrs.isDirectory() ? "" : "-R", p.toString());
var process = pb.start();
try (var reader = process.errorReader()) {
if (process.waitFor(5000, TimeUnit.MILLISECONDS)) {
int exitValue = process.exitValue();
if (process.exitValue() != 0) {
String error = reader.lines().collect(Collectors.joining());
throw new RevealFailedException("open command exited with value " + exitValue + " and error message: " + error);
}
}
}
} catch (IOException e) {
throw new RevealFailedException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RevealFailedException(e);
}
}

@Override
public boolean isSupported() {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.cryptomator.macos.revealpath;

import org.cryptomator.integrations.revealpath.RevealFailedException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@Disabled
public class OpenCmdRevealPathServiceTest {

@TempDir
Path tmpDir;

OpenCmdRevealPathService inTest = new OpenCmdRevealPathService();

@Test
public void testRevealDirSuccess() {
Assertions.assertDoesNotThrow(() -> inTest.reveal(tmpDir));
}

@Test
public void testRevealFileSuccess() throws IOException {
var path = tmpDir.resolve("foobar.text");
Files.createFile(path);
Assertions.assertDoesNotThrow(() -> inTest.reveal(path));
}

@Test
public void testRevealFail() {
Assertions.assertThrows(RevealFailedException.class, () -> inTest.reveal(tmpDir.resolve("foobar")));
}

}

0 comments on commit 314bdcf

Please sign in to comment.