github.com/echohead/hub@v2.2.1+incompatible/utils/utils.go (about) 1 package utils 2 3 import ( 4 "errors" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "runtime" 9 "strings" 10 11 "github.com/github/hub/ui" 12 ) 13 14 func Check(err error) { 15 if err != nil { 16 ui.Errorln(err) 17 os.Exit(1) 18 } 19 } 20 21 func ConcatPaths(paths ...string) string { 22 return strings.Join(paths, "/") 23 } 24 25 func BrowserLauncher() ([]string, error) { 26 browser := os.Getenv("BROWSER") 27 if browser == "" { 28 browser = searchBrowserLauncher(runtime.GOOS) 29 } 30 31 if browser == "" { 32 return nil, errors.New("Please set $BROWSER to a web launcher") 33 } 34 35 return strings.Split(browser, " "), nil 36 } 37 38 func searchBrowserLauncher(goos string) (browser string) { 39 switch goos { 40 case "darwin": 41 browser = "open" 42 case "windows": 43 browser = "cmd /c start" 44 default: 45 candidates := []string{"xdg-open", "cygstart", "x-www-browser", "firefox", 46 "opera", "mozilla", "netscape"} 47 for _, b := range candidates { 48 path, err := exec.LookPath(b) 49 if err == nil { 50 browser = path 51 break 52 } 53 } 54 } 55 56 return browser 57 } 58 59 func DirName() (string, error) { 60 dir, err := os.Getwd() 61 if err != nil { 62 return "", err 63 } 64 65 name := filepath.Base(dir) 66 name = strings.Replace(name, " ", "-", -1) 67 return name, nil 68 } 69 70 func IsOption(confirm, short, long string) bool { 71 return strings.EqualFold(confirm, short) || strings.EqualFold(confirm, long) 72 }