-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenFileManager for opening with the native file manager and optional
file selection support Closes #3197
- Loading branch information
1 parent
2e4fce7
commit fcf7da2
Showing
3 changed files
with
108 additions
and
10 deletions.
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
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 |
---|---|---|
@@ -1,24 +1,120 @@ | ||
package fileexplorer | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
||
ini "gopkg.in/ini.v1" | ||
) | ||
|
||
func Open(path string) error { | ||
var cmd *exec.Cmd | ||
type explorerBinArgs func(path string, selectFile bool) (string, []string, error) | ||
|
||
func OpenFileManager(path string, selectFile bool) error { | ||
path = os.ExpandEnv(path) | ||
if pathInfo, err := os.Stat(path); err != nil { | ||
return fmt.Errorf("failed to access the specified path stat: %w", err) | ||
} else { | ||
selectFile = selectFile && !pathInfo.IsDir() | ||
} | ||
|
||
var ( | ||
explorerBinArgs explorerBinArgs | ||
ignoreExitCode bool = false | ||
) | ||
|
||
switch runtime.GOOS { | ||
case "windows": | ||
cmd = exec.Command("explorer", path) | ||
explorerBinArgs = windowsExplorerBinArgs | ||
// NOTE: Disabling the exit code check on Windows system. Workaround for explorer.exe | ||
// exit code handling (https://github.com/microsoft/WSL/issues/6565) | ||
ignoreExitCode = true | ||
case "darwin": | ||
cmd = exec.Command("open", path) | ||
explorerBinArgs = darwinExplorerBinArgs | ||
case "linux": | ||
cmd = exec.Command("xdg-open", path) | ||
explorerBinArgs = linuxExplorerBinArgs | ||
default: | ||
return fmt.Errorf("unsupported platform") | ||
return errors.New("unsupported platform") | ||
} | ||
|
||
return cmd.Run() | ||
explorerBin, explorerArgs, err := explorerBinArgs(path, selectFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to determine the file explorer binary: %w", err) | ||
} | ||
|
||
cmd := exec.Command(explorerBin, explorerArgs...) | ||
cmd.Stdout = nil | ||
cmd.Stderr = nil | ||
|
||
if err := cmd.Run(); err != nil { | ||
if !ignoreExitCode { | ||
return fmt.Errorf("failed to open the file explorer: %w", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
var windowsExplorerBinArgs explorerBinArgs = func(path string, selectFile bool) (string, []string, error) { | ||
args := []string{} | ||
if selectFile { | ||
args = append(args, fmt.Sprintf("/select,\"%s\"", path)) | ||
} else { | ||
args = append(args, path) | ||
} | ||
return "explorer.exe", args, nil | ||
} | ||
|
||
var darwinExplorerBinArgs explorerBinArgs = func(path string, selectFile bool) (string, []string, error) { | ||
args := []string{} | ||
if selectFile { | ||
args = append(args, "-R") | ||
} | ||
|
||
args = append(args, path) | ||
return "open", args, nil | ||
} | ||
|
||
var linuxExplorerBinArgs explorerBinArgs = func(path string, selectFile bool) (string, []string, error) { | ||
fileManagerQuery := exec.Command("xdg-mime", "query", "default", "inode/directory") | ||
buf := new(bytes.Buffer) | ||
fileManagerQuery.Stdout = buf | ||
fileManagerQuery.Stderr = nil | ||
|
||
if err := fileManagerQuery.Run(); err == nil { | ||
var desktopFile string | ||
xdgPath := strings.TrimSpace(os.Getenv("XDG_DATA_HOME")) | ||
if xdgPath == "" { | ||
desktopFile = os.Getenv("HOME") + "/.local/share/applications" | ||
} | ||
desktopFile = xdgPath + strings.TrimSpace((buf.String())) | ||
|
||
if _, err := os.Stat(desktopFile); err != nil { | ||
desktopFile = "/usr/share/applications" + strings.TrimSpace((buf.String())) | ||
} | ||
|
||
cfg, err := ini.Load(desktopFile) | ||
if err != nil { | ||
// Opting to fallback rather than fail | ||
return linuxFallbackExplorerBinArgs(path, selectFile) | ||
} | ||
|
||
exec := cfg.Section("Desktop Entry").Key("Exec").String() | ||
args := strings.Split(exec, " ") | ||
args = append(args, path) | ||
|
||
return args[0], args[1:], nil | ||
} else { | ||
return linuxFallbackExplorerBinArgs(path, selectFile) | ||
} | ||
} | ||
|
||
var linuxFallbackExplorerBinArgs explorerBinArgs = func(path string, selectFile bool) (string, []string, error) { | ||
// NOTE: The linux fallback explorer opening is not supporting file selection | ||
path = filepath.Dir(path) | ||
return "xdg-open", []string{path}, nil | ||
} |
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