-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
95 lines (82 loc) · 1.83 KB
/
config.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
package main
import (
"errors"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
)
type config struct {
localTZ *time.Location
aliases map[string]string
}
func mustLoadAliases(in io.Reader) map[string]string {
bb, err := ioutil.ReadAll(in)
if err != nil {
log.Fatalf("couldn't load config: %s", err)
}
var user = make(map[string]string)
err = yaml.Unmarshal(bb, &user)
if err != nil {
log.Fatalf("couldn't unmarshal config: %s", err)
}
for k, v := range defaultAlias {
_, exists := user[k]
if !exists {
user[k] = v
}
}
return user
}
// https://stackoverflow.com/a/41786440
func userHomeDir() string {
env := "HOME"
if runtime.GOOS == "windows" {
env = "USERPROFILE"
} else if runtime.GOOS == "plan9" {
env = "home"
}
return os.Getenv(env)
}
func tzFromShell() (*time.Location, error) {
bb, err := exec.Command("date", "+%z").Output()
if err != nil {
return nil, err
}
name := strings.TrimSpace(string(bb))
dateRX := regexp.MustCompile(`^([+\-])(\d\d)(\d\d)\s?$`)
match := dateRX.FindSubmatch(bb)
if len(match) == 0 {
return nil, errors.New("date +%z output didn't match expected regex")
}
signString := string(match[1][0])
sign := 1
if signString == "-" {
sign = -1
}
if len(match[2]) != 2 {
return nil, errors.New(`date +%z output did not have two hour bytes`)
}
if len(match[3]) != 2 {
return nil, errors.New(`date +%z output did not have two minute bytes`)
}
hh := string(match[2])
mm := string(match[3])
h, err := strconv.Atoi(hh)
if err != nil {
return nil, errors.New("second and third bytes of date +%z were not integers")
}
m, err := strconv.Atoi(mm)
if err != nil {
return nil, errors.New("fourth and fifth bytes of date +%z were not integers")
}
s := (60*m + 3600*h) * sign
return time.FixedZone(name, s), nil
}