diff --git a/sysutil/sysutil.go b/sysutil/sysutil.go index d76798ea0..88c5ad1b0 100644 --- a/sysutil/sysutil.go +++ b/sysutil/sysutil.go @@ -4,6 +4,7 @@ package sysutil import ( "os" "path" + "path/filepath" ) // Workdir get @@ -25,5 +26,19 @@ func BinFile() string { // Open file or url address func Open(fileOrUrl string) error { - return OpenBrowser(fileOrUrl) + return OpenURL(fileOrUrl) +} + +// OpenBrowser file or url address +func OpenBrowser(fileOrUrl string) error { + return OpenURL(fileOrUrl) +} + +// OpenFile opens new browser window for the file path. +func OpenFile(path string) error { + fpath, err := filepath.Abs(path) + if err != nil { + return err + } + return OpenURL("file://" + fpath) } diff --git a/sysutil/sysutil_darwin.go b/sysutil/sysutil_darwin.go new file mode 100644 index 000000000..3fe2da509 --- /dev/null +++ b/sysutil/sysutil_darwin.go @@ -0,0 +1,36 @@ +package sysutil + +import "os/exec" + +// IsWin system. linux windows darwin +func IsWin() bool { return false } + +// IsWindows system. linux windows darwin +func IsWindows() bool { return false } + +// IsMac system +func IsMac() bool { return true } + +// IsDarwin system +func IsDarwin() bool { return true } + +// IsLinux system +func IsLinux() bool { return false } + +// OpenURL Open browser URL +// +// Mac: +// +// open 'https://github.com/inhere' +// +// Linux: +// +// xdg-open URL +// x-www-browser 'https://github.com/inhere' +// +// Windows: +// +// cmd /c start https://github.com/inhere +func OpenURL(URL string) error { + return exec.Command("open", URL).Run() +} diff --git a/sysutil/sysutil_linux.go b/sysutil/sysutil_linux.go new file mode 100644 index 000000000..e41fc07b1 --- /dev/null +++ b/sysutil/sysutil_linux.go @@ -0,0 +1,52 @@ +package sysutil + +import ( + "os/exec" + "strings" +) + +// IsWin system. linux windows darwin +func IsWin() bool { return false } + +// IsWindows system. linux windows darwin +func IsWindows() bool { return false } + +// IsMac system +func IsMac() bool { return false } + +// IsDarwin system +func IsDarwin() bool { return false } + +// IsLinux system +func IsLinux() bool { + return true +} + +// There are multiple possible providers to open a browser on linux +// One of them is xdg-open, another is x-www-browser, then there's www-browser, etc. +// Look for one that exists and run it +var openBins = []string{"xdg-open", "x-www-browser", "www-browser"} + +// OpenURL Open file or browser URL +// +// Mac: +// +// open 'https://github.com/inhere' +// +// Linux: +// +// xdg-open URL +// x-www-browser 'https://github.com/inhere' +// +// Windows: +// +// cmd /c start https://github.com/inhere +func OpenURL(URL string) error { + for _, bin := range openBins { + if _, err := exec.LookPath(bin); err == nil { + return exec.Command(bin, URL).Run() + } + } + + return &exec.Error{Name: strings.Join(openBins, ","), Err: exec.ErrNotFound} +} diff --git a/sysutil/sysutil_nonwin.go b/sysutil/sysutil_nonwin.go index b12ba7020..bfafdbdee 100644 --- a/sysutil/sysutil_nonwin.go +++ b/sysutil/sysutil_nonwin.go @@ -3,36 +3,9 @@ package sysutil import ( - "os/exec" - "runtime" "syscall" ) -// IsWin system. linux windows darwin -func IsWin() bool { - return false -} - -// IsWindows system. linux windows darwin -func IsWindows() bool { - return false -} - -// IsMac system -func IsMac() bool { - return runtime.GOOS == "darwin" -} - -// IsDarwin system -func IsDarwin() bool { - return runtime.GOOS == "darwin" -} - -// IsLinux system -func IsLinux() bool { - return runtime.GOOS == "linux" -} - // Kill a process by pid func Kill(pid int, signal syscall.Signal) error { return syscall.Kill(pid, signal) @@ -42,26 +15,3 @@ func Kill(pid int, signal syscall.Signal) error { func ProcessExists(pid int) bool { return nil == syscall.Kill(pid, 0) } - -// OpenBrowser Open browser URL -// -// Mac: -// -// open 'https://github.com/inhere' -// -// Linux: -// -// xdg-open URL -// x-www-browser 'https://github.com/inhere' -// -// Windows: -// -// cmd /c start https://github.com/inhere -func OpenBrowser(URL string) error { - bin := "x-www-browser" - if IsDarwin() { - bin = "open" - } - - return exec.Command(bin, URL).Run() -} diff --git a/sysutil/sysutil_windows.go b/sysutil/sysutil_windows.go index b53cdc3aa..ba458b174 100644 --- a/sysutil/sysutil_windows.go +++ b/sysutil/sysutil_windows.go @@ -5,36 +5,25 @@ package sysutil import ( "errors" - "os/exec" "syscall" - "github.com/gookit/goutil/sysutil/process" + "golang.org/x/sys/windows" ) // IsWin system. linux windows darwin -func IsWin() bool { - return true -} +func IsWin() bool { return true } // IsWindows system. linux windows darwin -func IsWindows() bool { - return true -} +func IsWindows() bool { return true } // IsMac system -func IsMac() bool { - return false -} +func IsMac() bool { return false } // IsDarwin system -func IsDarwin() bool { - return false -} +func IsDarwin() bool { return false } // IsLinux system -func IsLinux() bool { - return false -} +func IsLinux() bool { return false } // Kill a process by pid func Kill(pid int, signal syscall.Signal) error { @@ -43,10 +32,12 @@ func Kill(pid int, signal syscall.Signal) error { // ProcessExists check process exists by pid func ProcessExists(pid int) bool { - return process.Exists(pid) + panic("TIP: please use sysutil/process.Exists()") } -// OpenBrowser Open browser URL +// OpenURL Open file or browser URL +// +// - refers https://github.com/pkg/browser // // Mac: // @@ -60,6 +51,7 @@ func ProcessExists(pid int) bool { // Windows: // // cmd /c start https://github.com/inhere -func OpenBrowser(URL string) error { - return exec.Command("cmd", "/c", "start", URL).Run() +func OpenURL(url string) error { + // return exec.Command("cmd", "/C", "start", URL).Run() + return windows.ShellExecute(0, nil, windows.StringToUTF16Ptr(url), nil, nil, windows.SW_SHOWNORMAL) }