hub/utils/utils.go

61 строка
1006 B
Go

package utils
import (
"errors"
"log"
"os"
"os/exec"
"runtime"
"strings"
)
func Fatal(err error) {
if err != nil {
log.Fatal(err)
}
}
func Check(err error) {
if err != nil {
log.Fatalf("fatal: %v", err)
}
}
func ConcatPaths(paths ...string) string {
return strings.Join(paths, "/")
}
func BrowserLauncher() ([]string, error) {
browser := os.Getenv("BROWSER")
if browser == "" {
browser = searchBrowserLauncher(runtime.GOOS)
}
if browser == "" {
return nil, errors.New("Please set $BROWSER to a web launcher")
}
return strings.Split(browser, " "), nil
}
func searchBrowserLauncher(goos string) (browser string) {
switch goos {
case "darwin":
browser = "open"
case "windows":
browser = "cmd /c start"
default:
candidates := []string{"xdg-open", "cygstart", "x-www-browser", "firefox",
"opera", "mozilla", "netscape"}
for _, b := range candidates {
path, err := exec.LookPath(b)
if err == nil {
browser = path
break
}
}
}
return browser
}