-
-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#66 Ability to open link in cmd and powershell
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//+build !windows | ||
|
||
package open | ||
|
||
import ( | ||
|
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 |
---|---|---|
@@ -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 != "" | ||
} |