This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
forked from gomodules/x
/
helpers.go
135 lines (122 loc) · 2.54 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package ioutil
import (
"bufio"
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func ReadFile(path string) (string, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(content), nil
}
func ReadFileAs(path string, obj interface{}) error {
d, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = json.Unmarshal(d, obj)
if err != nil {
return err
}
return nil
}
/*
ReadINIConfig loads a ini config file without any sections. Example:
--- --- ---
a=b
c=d
--- --- ---
*/
func ReadINIConfig(path string) (map[string]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
mp := make(map[string]string)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
result := strings.Split(scanner.Text(), "=")
if len(result) != 2 {
continue
}
mp[string(result[0])] = result[1]
}
if err := scanner.Err(); err != nil {
return nil, err
}
return mp, nil
}
func WriteJson(path string, obj interface{}) error {
d, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
EnsureDirectory(path)
err = ioutil.WriteFile(path, d, os.ModePerm)
if err != nil {
return err
}
return nil
}
func WriteString(path string, data string) bool {
EnsureDirectory(path)
err := ioutil.WriteFile(path, []byte(data), os.ModePerm)
if err != nil {
return false
}
return true
}
func AppendToFile(path string, values string) error {
EnsureDirectory(path)
if _, err := os.Stat(path); err != nil {
ioutil.WriteFile(path, []byte(""), os.ModePerm)
}
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return err
}
defer f.Close()
f.WriteString("\n")
_, err = f.WriteString(values)
if err != nil {
return err
}
return nil
}
func EnsureDirectory(path string) error {
parent := filepath.Dir(path)
if _, err := os.Stat(parent); err != nil {
return os.MkdirAll(parent, os.ModePerm)
}
return nil
}
func IsFileExists(path string) bool {
EnsureDirectory(path)
if _, err := os.Stat(path); err != nil {
return false
}
return true
}
// WriteFile writes the contents from src to dst using io.Copy.
// If dst does not exist, WriteFile creates it with permissions perm;
// otherwise WriteFile truncates it before writing.
func WriteFile(dst string, src io.Reader, perm os.FileMode) (err error) {
out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return
}
defer func() {
if e := out.Close(); e != nil {
err = e
}
}()
_, err = io.Copy(out, src)
return
}