forked from nikepan/clickhouse-bulk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
47 lines (41 loc) · 1.03 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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
)
// ReadJSON - read json file to struct
func ReadJSON(fn string, v interface{}) error {
file, err := os.Open(fn)
defer file.Close()
if err != nil {
return err
}
decoder := json.NewDecoder(file)
return decoder.Decode(v)
}
// HasPrefix tests case insensitive whether the string s begins with prefix.
func HasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && strings.ToLower(s[0:len(prefix)]) == strings.ToLower(prefix)
}
// Dumper - interface for dump data
type Dumper interface {
Dump(params string, data string) error
}
// FileDumper - dumps data to file system
type FileDumper struct {
Path string
DumpNum int
}
// Dump - dumps data to files
func (d *FileDumper) Dump(params string, data string) error {
if _, err := os.Stat(d.Path); os.IsNotExist(err) {
os.Mkdir(d.Path, 644)
}
d.DumpNum++
err := ioutil.WriteFile(path.Join(d.Path, "dump"+strconv.Itoa(d.DumpNum)+".dmp"), []byte(params+"\n"+data), 0644)
return err
}