From 6485927b89155f3e4470883335d41029c93ae290 Mon Sep 17 00:00:00 2001 From: Vuong <3168632+vuongggggg@users.noreply.github.com> Date: Tue, 10 Nov 2020 21:22:45 +0700 Subject: [PATCH] #66 Ability to open link in cmd and powershell --- cointop/common/open/open.go | 2 ++ cointop/common/open/open_win.go | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 cointop/common/open/open_win.go diff --git a/cointop/common/open/open.go b/cointop/common/open/open.go index 74f3efa..b64c965 100644 --- a/cointop/common/open/open.go +++ b/cointop/common/open/open.go @@ -1,3 +1,5 @@ +//+build !windows + package open import ( diff --git a/cointop/common/open/open_win.go b/cointop/common/open/open_win.go new file mode 100644 index 0000000..ca80ed0 --- /dev/null +++ b/cointop/common/open/open_win.go @@ -0,0 +1,52 @@ +package open + +import ( + "os/exec" +) + +var openCmd string +var possibleCmds = []string{ + "Start-Process", // windows +} + +var possibleShells = []string{ + "powershell.exe", + "explorer.exe", +} + +var mainShell string + +func init() { + for _, sh := range possibleShells { + shell, err := exec.LookPath(sh) + if err != nil { + continue + } + + mainShell = shell + break + } + + for _, cmd := range possibleCmds { + err := exec.Command(mainShell, "Get-Command", cmd).Run() + if err != nil { + continue + } + + openCmd = cmd + break + } +} + +// URL open url +func URL(s string) error { + if openCmd != "" { + return exec.Command(mainShell, openCmd, s).Run() + } + return nil +} + +// CommandExists returns true if an 'open' command exists +func CommandExists() bool { + return openCmd != "" +}