Files
Reims-Go/internal/host/open_windows.go
T

58 lines
1.2 KiB
Go

//go:build windows
package host
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
)
// OpenFolder shows dir in File Explorer. explorer.exe exits non-zero even on
// success, so the process is started and not waited on.
func OpenFolder(dir string) error {
info, err := os.Stat(dir)
if err != nil {
return err
}
if !info.IsDir() {
return fmt.Errorf("not a directory: %s", dir)
}
return exec.Command("explorer.exe", dir).Start()
}
// RevealFile selects a file in File Explorer.
func RevealFile(path string) error {
absPath, err := absoluteFilePath(path)
if err != nil {
return err
}
cmd := exec.Command("explorer.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: explorerSelectCommandLine(absPath)}
if err := cmd.Start(); err != nil {
return err
}
return cmd.Process.Release()
}
func explorerSelectCommandLine(absPath string) string {
return `explorer.exe /select,"` + absPath + `"`
}
func absoluteFilePath(path string) (string, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return "", err
}
info, err := os.Stat(absPath)
if err != nil {
return "", err
}
if info.IsDir() {
return "", fmt.Errorf("not a file: %s", absPath)
}
return absPath, nil
}