-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_rules.go
54 lines (44 loc) · 1.07 KB
/
file_rules.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
package backpack
import (
"encoding/json"
"fmt"
"regexp"
)
type FileCommand string
const (
Copy FileCommand = "copy"
Sqlite FileCommand = "sqlite"
Ignore FileCommand = "ignore"
)
type FileRule struct {
Regex *regexp.Regexp
Command FileCommand
}
type DirRule struct {
SrcDir string `json:"path"`
FileRules []FileRule `json:"file_rules"`
}
func (fr *FileRule) UnmarshalJSON(data []byte) error {
var raw struct {
Regex string `json:"regex"`
Command string `json:"command"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return fmt.Errorf("error parsing json object for FileRule: %w", err)
}
if raw.Regex == "" {
return fmt.Errorf("error parsing json object: empty regexp")
}
re, err := regexp.Compile(raw.Regex)
if err != nil {
return fmt.Errorf("error parsing regexp in FileRule: %w", err)
}
if raw.Command != string(Copy) &&
raw.Command != string(Sqlite) &&
raw.Command != string(Ignore) {
return fmt.Errorf("unsupported command in FileRule: %v", raw.Command)
}
fr.Command = FileCommand(raw.Command)
fr.Regex = re
return nil
}