forked from prasmussen/gdrive
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.go
84 lines (68 loc) · 1.18 KB
/
util.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
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"github.com/adrg/xdg"
)
func GetDefaultConfigDir() string {
return filepath.Join(xdg.ConfigHome, "gdrive")
}
func ConfigFilePath(basePath, name string) string {
return filepath.Join(basePath, name)
}
func equal(a, b []string) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func ExitF(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
fmt.Println("")
os.Exit(1)
}
func checkErr(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func writeJson(path string, data interface{}) error {
tmpFile := path + ".tmp"
f, err := os.Create(tmpFile)
if err != nil {
return err
}
err = json.NewEncoder(f).Encode(data)
f.Close()
if err != nil {
os.Remove(tmpFile)
return err
}
return os.Rename(tmpFile, path)
}
func md5sum(path string) string {
h := md5.New()
f, err := os.Open(path)
if err != nil {
return ""
}
defer f.Close()
io.Copy(h, f)
return fmt.Sprintf("%x", h.Sum(nil))
}