-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathutils.go
119 lines (101 loc) · 2.01 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package utils
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/github/hub/v2/ui"
"github.com/kballard/go-shellquote"
)
var timeNow = time.Now
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)
} else {
browser = os.ExpandEnv(browser)
}
if browser == "" {
return nil, errors.New("Please set $BROWSER to a web launcher")
}
return shellquote.Split(browser)
}
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 CommandPath(cmd string) (string, error) {
if runtime.GOOS == "windows" && !strings.HasSuffix(cmd, ".exe") {
cmd = cmd + ".exe"
}
path, err := exec.LookPath(cmd)
if err != nil {
return "", err
}
path, err = filepath.Abs(path)
if err != nil {
return "", err
}
return filepath.EvalSymlinks(path)
}
func TimeAgo(t time.Time) string {
duration := timeNow().Sub(t)
minutes := duration.Minutes()
hours := duration.Hours()
days := hours / 24
months := days / 30
years := months / 12
var val int
var unit string
if minutes < 1 {
return "now"
} else if hours < 1 {
val = int(minutes)
unit = "minute"
} else if days < 1 {
val = int(hours)
unit = "hour"
} else if months < 1 {
val = int(days)
unit = "day"
} else if years < 1 {
val = int(months)
unit = "month"
} else {
val = int(years)
unit = "year"
}
var plural string
if val > 1 {
plural = "s"
}
return fmt.Sprintf("%d %s%s ago", val, unit, plural)
}