-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
63 lines (58 loc) · 1.15 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
package main
import (
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
func writeToFile(filePath, content string) error {
fs, err := os.Create(filePath)
defer fs.Close()
if err != nil {
return err
}
fs.WriteString(content)
return nil
}
// expand ~ to user home directory (platform independent)
func expandPath(p string) string {
expandedPath := os.ExpandEnv(p)
if strings.HasPrefix(p, "~") {
pos := strings.Index(expandedPath, "/")
HomeDir := ""
if runtime.GOOS == "windows" {
HomeDir = os.Getenv("USERPROFILE")
} else {
HomeDir = os.Getenv("HOME")
}
p = filepath.Join(HomeDir, expandedPath[pos:len(expandedPath)])
}
p, _ = filepath.Abs(p)
return p
}
func fileExists(filePath string) bool {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return false
}
return true
}
func parseTime(formatted string) (time.Time, error) {
var layouts = [...]string{
"20060102",
"2006/01/02",
"2006/1/2",
"2006-01-02",
"2006-1-2",
}
var t time.Time
var err error
formatted = strings.TrimSpace(formatted)
for _, layout := range layouts {
t, err = time.Parse(layout, formatted)
if !t.IsZero() {
break
}
}
return t, err
}