Skip to content

Commit

Permalink
👔 up(sys): update the os check and open URL logic
Browse files Browse the repository at this point in the history
- fix open lang url fail on Windows
  • Loading branch information
inhere committed Feb 23, 2023
1 parent e6498a3 commit e500a04
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 72 deletions.
17 changes: 16 additions & 1 deletion sysutil/sysutil.go
Expand Up @@ -4,6 +4,7 @@ package sysutil
import (
"os"
"path"
"path/filepath"
)

// Workdir get
Expand All @@ -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)
}
36 changes: 36 additions & 0 deletions 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()
}
52 changes: 52 additions & 0 deletions 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}
}
50 changes: 0 additions & 50 deletions sysutil/sysutil_nonwin.go
Expand Up @@ -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)
Expand All @@ -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()
}
34 changes: 13 additions & 21 deletions sysutil/sysutil_windows.go
Expand Up @@ -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 {
Expand All @@ -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:
//
Expand All @@ -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)
}

0 comments on commit e500a04

Please sign in to comment.