forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
60 lines (50 loc) · 1.07 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package utils
import (
"errors"
"os"
"os/exec"
"runtime"
"strings"
"github.com/github/hub/ui"
)
func Check(err error) {
if err != nil {
ui.Errorln(err)
os.Exit(1)
}
}
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
}
func IsOption(confirm, short, long string) bool {
return strings.EqualFold(confirm, short) || strings.EqualFold(confirm, long)
}