forked from krystal/kcm-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
69 lines (57 loc) · 1.25 KB
/
helpers.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func fileMissing(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return false, nil
}
if os.IsNotExist(err) {
return true, nil
}
return false, err
}
func fileMatches(path string, otherContent string) (bool, error) {
fileMissing, err := fileMissing(path)
if err != nil {
return false, err
}
// If the file does not exist and the content is empty, the file matches
// and we should return true.
if otherContent == "" && fileMissing {
return true, nil
}
// If the file is missing but we have content, the file cannot match
// so we return false.
if fileMissing {
return false, nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
// If the actual content of the file does not match...
return string(content) == otherContent, nil
}
func getURLContents(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode == 404 {
return "", nil
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), err
}