-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathopen_directory.go
More file actions
39 lines (32 loc) · 793 Bytes
/
open_directory.go
File metadata and controls
39 lines (32 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"fmt"
"os/exec"
"path/filepath"
"runtime"
)
// openDirectory attempts to reveal the given directory in the default file explorer.
func openDirectory(path string) error {
if path == "" {
return fmt.Errorf("no path provided")
}
absPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("resolve path: %w", err)
}
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("explorer", absPath)
case "darwin":
cmd = exec.Command("open", absPath)
default:
cmd = exec.Command("xdg-open", absPath)
}
hideWindow := runtime.GOOS != "windows" // keep Explorer visible on Windows
configureCommand(cmd, hideWindow)
if err := cmd.Start(); err != nil {
return fmt.Errorf("launch file explorer: %w", err)
}
return nil
}