-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
160 lines (133 loc) · 2.64 KB
/
entity.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
package entity
import (
"path/filepath"
"regexp"
"golang.org/x/xerrors"
)
type (
TemplateOptionsKey string
MissingKeyValue string
)
func (v MissingKeyValue) Valid() error {
switch v {
case MissingKeyDefault,
MissingKeyInvalid,
MissingKeyZero,
MissingKeyError:
return nil
default:
return xerrors.Errorf("templte option [%v] is not valid: %v", TemplateOptionsMissingKey, v)
}
}
//goland:noinspection SpellCheckingInspection
const (
TemplateOptionsMissingKey TemplateOptionsKey = "missingkey"
MissingKeyDefault MissingKeyValue = "default"
MissingKeyInvalid MissingKeyValue = "invalid"
MissingKeyZero MissingKeyValue = "zero"
MissingKeyError MissingKeyValue = "error"
Space = " "
Empty = ""
Dash = "-"
Dot = "."
EqualsSign = "="
LessThan = "<"
Tilda = "~"
NewLine = "\n"
)
type (
FileProducer interface {
Get() (DataFile, error)
}
FileStrategy interface {
Apply(file DataFile) (DataFile, error)
}
DirStrategy interface {
Apply(path string) (string, error)
}
TemplateProc interface {
Process(name, text string) (string, error)
}
Executor interface {
Exec() error
}
Preprocessor interface {
Process() error
}
//goland:noinspection SpellCheckingInspection
Logger interface {
Infof(format string, args ...any)
Errorf(format string, any ...any)
Warnf(format string, any ...any)
Debugf(format string, any ...any)
}
)
type DataFile struct {
FileInfo
Data []byte
}
type LocalFile struct {
FileInfo
LocalPath string
}
type RemoteFile struct {
FileInfo
HTTPClientParams
}
type FileInfo struct {
dir string
name string
path *string
}
func NewFileInfo(path string) FileInfo {
return FileInfo{
name: filepath.Base(path),
dir: filepath.Dir(path),
}
}
func (f *FileInfo) Name() string {
return f.name
}
func (f *FileInfo) Dir() string {
return f.dir
}
func (f *FileInfo) Path() string {
if f.path == nil {
path := filepath.Join(f.dir, f.name)
f.path = &path
}
return *f.path
}
type HTTPClientParams struct {
URL string
Headers map[string]string
QueryParams map[string]string
}
type Command struct {
Cmd string
Args []string
Dir string
}
type RegexpChain struct {
matchers []*regexp.Regexp
}
func NewRegexpChain(regexps ...string) *RegexpChain {
matchers := make([]*regexp.Regexp, 0, len(regexps))
for _, regex := range regexps {
matcher := regexp.MustCompile(regex)
if matcher != nil {
matchers = append(matchers, matcher)
}
}
return &RegexpChain{
matchers: matchers,
}
}
func (c *RegexpChain) MatchString(s string) bool {
for _, matcher := range c.matchers {
if matcher.MatchString(s) {
return true
}
}
return false
}