/
utils.go
248 lines (217 loc) · 5.74 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package app
import (
"crypto/md5"
"encoding/xml"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
)
// QueryString retrieves a string param from the gin request querystring
func QueryString(c echo.Context, name string) string {
return c.QueryParam(name)
}
func QueryDefaultString(c echo.Context, name, def string) string {
v := c.QueryParam(name)
if v == "" {
return def
}
return v
}
// QueryInt retrieves an integer param from the gin request querystring
func QueryInt(c echo.Context, name string) int {
v := c.QueryParam(name)
i, _ := strconv.Atoi(v)
return i
}
// QueryDefaultInt retrieves an integer param from the gin request querystring
// defaults to def argument if not found
func QueryDefaultInteger(c echo.Context, name string, def int) (int, error) {
v := c.QueryParam(name)
if v == "" {
return def, nil
}
n, err := strconv.Atoi(v)
if err != nil {
return def, err
}
if n < 0 {
return def, fmt.Errorf("less than zero")
}
return n, nil
}
// QueryBool retrieves a boolean param from the gin request querystring
func QueryBool(c echo.Context, name string) bool {
return c.QueryParam(name) == "true"
}
var pathQuoteRegex = regexp.MustCompile(`'(\w{1,2})`)
var pathCharRegex = regexp.MustCompile(`[^a-zA-Z0-9]+`)
var pathExtraRegex = regexp.MustCompile(`\{(\w+)-(\d+)\}`)
func path(title string) string {
// fmt.Printf("path: %s\n", title)
var s string
s = pathQuoteRegex.ReplaceAllString(title, "$1")
s = strings.ToLower(s)
s = pathCharRegex.ReplaceAllString(s, " ")
s = strings.TrimSpace(s)
// fmt.Printf("path: %s\n", s)
return s
}
// stolen from gin gonic
// H is a shortcut for map[string]any
type H map[string]any
// MarshalXML allows type H to be used with xml.Marshal.
func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{
Space: "",
Local: "map",
}
if err := e.EncodeToken(start); err != nil {
return err
}
for key, value := range h {
elem := xml.StartElement{
Name: xml.Name{Space: "", Local: key},
Attr: []xml.Attr{},
}
if err := e.EncodeElement(value, elem); err != nil {
return err
}
}
return e.EncodeToken(xml.EndElement{Name: start.Name})
}
func directory(title string) string {
// fmt.Printf("directory: %s\n", title)
s := title
var extra string
if matches := pathExtraRegex.FindStringSubmatch(title); len(matches) > 0 {
extra = fmt.Sprintf(" {%s-%s}", matches[1], matches[2])
s = pathExtraRegex.ReplaceAllString(title, "")
}
s = path(s)
if extra != "" {
s = s + extra
}
// fmt.Printf("directory: %s\n", s)
return s
}
func exists(path string) bool {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return false
} else {
return true
}
}
func sumFile(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", errors.Wrap(err, "failed to open file")
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return "", errors.Wrap(err, "failed to hash file")
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
func sumFiles(source, destination string) (bool, error) {
type response struct {
Hash string
Err error
}
ch_source := make(chan response)
ch_dest := make(chan response)
go func() {
sum, err := sumFile(source)
ch_source <- response{sum, err}
}()
go func() {
sum, err := sumFile(destination)
ch_dest <- response{sum, err}
}()
r_source := <-ch_source
r_dest := <-ch_dest
if r_source.Err != nil {
return false, errors.Wrap(r_source.Err, "failed to sum source")
}
if r_dest.Err != nil {
return false, errors.Wrap(r_dest.Err, "failed to sum destination")
}
return r_source.Hash == r_dest.Hash, nil
}
// https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file
// Copy copies the contents of the file at srcpath to a regular file at dstpath.
// If dstpath already exists and is not a directory, the function truncates it.
// The function does not copy file modes or file attributes.
func FileCopy(srcpath, dstpath string) (err error) {
r, err := os.Open(srcpath)
if err != nil {
return errors.Wrap(err, "source")
}
defer r.Close() // ignore error: file was opened read-only.
w, err := os.Create(dstpath)
if err != nil {
return errors.Wrap(err, "destination")
}
defer func() {
// Report the error from Close, if any,
// but do so only if there isn't already
// an outgoing error.
if c := w.Close(); c != nil && err == nil {
err = c
}
}()
_, err = io.Copy(w, r)
return err
}
// FileLink creates hard link, if destination exists and force is true, it will remove the destination before linking
func FileLink(srcpath, dstpath string, force bool) error {
if err := FileDir(dstpath); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
if exists(dstpath) {
if !force {
return fmt.Errorf("destination exists, force false")
}
if err := os.Remove(dstpath); err != nil {
return fmt.Errorf("failed to remove destination: %w", err)
}
}
return os.Link(srcpath, dstpath)
}
func FileDir(file string) error {
path := filepath.Dir(file)
return os.MkdirAll(path, 0755)
}
// WithTimeout runs a delegate function with a timeout,
//
// Example: Wait for a channel
//
// if value, ok := WithTimeout(func()interface{}{return <- inbox}, time.Second); ok {
// // returned
// } else {
// // didn't return
// }
//
// Example: To send to a channel
//
// _, ok := WithTimeout(func()interface{}{outbox <- myValue; return nil}, time.Second)
// if !ok {
// // didn't send
// }
func WithTimeout(delegate func() interface{}, timeout time.Duration) (ret interface{}, ok bool) {
ch := make(chan interface{}, 1) // buffered
go func() { ch <- delegate() }()
select {
case ret = <-ch:
return ret, true
case <-time.After(timeout):
}
return nil, false
}