-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
185 lines (163 loc) · 4.61 KB
/
fs.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package goutil
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/tkdeng/goregex"
"gopkg.in/yaml.v3"
)
// JoinPath joins multiple file types with safety from backtracking
func JoinPath(path ...string) (string, error) {
resPath, err := filepath.Abs(string(path[0]))
if err != nil {
return "", err
}
for i := 1; i < len(path); i++ {
p := filepath.Join(resPath, string(path[i]))
if p == resPath || !strings.HasPrefix(p, resPath) {
return "", errors.New("path leaked outside of root")
}
resPath = p
}
return resPath, nil
}
// AppendRoot will append a root path to a list of paths
func AppendRoot(root string, paths ...*string) error {
var err error
for _, path := range paths {
p, e := JoinPath(root, *path)
if e != nil {
err = errors.Join(err, e)
}
*path = p
}
return err
}
// Copy lets you copy files from the src to the dst
func CopyFile(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
// ReadYaml loads a yaml file into a struct
//
// this method will read the buffer, and normalize names so
// '-' and '_' characters are optional, and everything is lowercase
//
// this method is useful for loading a config file
func ReadYaml(path string, out interface{}) error {
b, err := os.ReadFile(path)
if err != nil {
return err
}
b = regex.Comp(`(?m)^(\s*(?:-\s+|))([\w_\-]+):`).RepFunc(b, func(data func(int) []byte) []byte {
return regex.JoinBytes(data(1), bytes.ReplaceAll(bytes.ReplaceAll(bytes.ToLower(data(2)), []byte{'-'}, []byte{}), []byte{'_'}, []byte{}), ':')
})
return yaml.Unmarshal(b, out)
}
// ReadJson loads a json file into a struct
//
// this method will read the buffer, and normalize names so
// '-' and '_' characters are optional, and everything is lowercase
//
// this method is useful for loading a config file
func ReadJson(path string, out interface{}) error {
b, err := os.ReadFile(path)
if err != nil {
return err
}
b = regex.Comp(`(?s)"([\w_-]+)"\s*:`).RepFunc(b, func(data func(int) []byte) []byte {
return regex.JoinBytes('"', bytes.ReplaceAll(bytes.ReplaceAll(bytes.ToLower(data(1)), []byte{'-'}, []byte{}), []byte{'_'}, []byte{}), '"', ':')
})
return json.Unmarshal(b, out)
}
// ReadConfig loads a config file into a struct
//
// this method will read the buffer, and normalize names so
// '-' and '_' characters are optional, and everything is lowercase
//
// this method will try different file types in the following order:
//
// [yml, yaml, json]
//
// you can specify the first file type to try, by adding a .ext of that file type to the path
//
// by accepting moltiple file types, the user can choose what type of file they want to use for their config file
func ReadConfig(path string, out interface{}) error {
t := "yaml"
var b []byte
var err error = io.EOF
// path .ext prioritize
if strings.HasSuffix(path, ".yml") {
t = "yaml"
path = strings.TrimSuffix(path, ".yml")
b, err = os.ReadFile(path + ".yml")
if err != nil {
b, err = os.ReadFile(path + ".yaml")
}
} else if strings.HasSuffix(path, ".yaml") {
t = "yaml"
path = strings.TrimSuffix(path, ".yaml")
b, err = os.ReadFile(path + ".yaml")
if err != nil {
b, err = os.ReadFile(path + ".yml")
}
} else if strings.HasSuffix(path, ".json") {
t = "json"
path = strings.TrimSuffix(path, ".json")
b, err = os.ReadFile(path + ".json")
}
// try yml
if err != nil {
t = "yaml"
b, err = os.ReadFile(path + ".yml")
}
// try yaml
if err != nil {
t = "yaml"
b, err = os.ReadFile(path + ".yaml")
}
// try json
if err != nil {
t = "json"
b, err = os.ReadFile(path + ".json")
}
if err != nil {
return io.EOF
}
switch t {
case "yaml":
b = regex.Comp(`(?m)^(\s*(?:-\s+|))([\w_\-]+):`).RepFunc(b, func(data func(int) []byte) []byte {
return regex.JoinBytes(data(1), bytes.ReplaceAll(bytes.ReplaceAll(bytes.ToLower(data(2)), []byte{'-'}, []byte{}), []byte{'_'}, []byte{}), ':')
})
return yaml.Unmarshal(b, out)
case "json":
b = regex.Comp(`(?s)"([\w_-]+)"\s*:`).RepFunc(b, func(data func(int) []byte) []byte {
return regex.JoinBytes('"', bytes.ReplaceAll(bytes.ReplaceAll(bytes.ToLower(data(1)), []byte{'-'}, []byte{}), []byte{'_'}, []byte{}), '"', ':')
})
return json.Unmarshal(b, out)
default:
return io.EOF
}
}