-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from cryptomator/feature/reveal-path-service
Feature: Implement Provider for ReveaPathService
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/org/cryptomator/macos/revealpath/OpenCmdRevealPathService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/org/cryptomator/macos/revealpath/OpenCmdRevealPathServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
|
||
} |