-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
227 lines (197 loc) · 4.8 KB
/
config.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
package config
import (
"net/url"
"strings"
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"
"github.com/kozmod/progen/internal/entity"
)
const (
TagDirs = "dirs"
TagRm = "rm"
TagFiles = "files"
TagCmd = "cmd"
TagFS = "fs"
SettingsHTTP = "settings"
)
type Config struct {
Settings Settings `yaml:"settings"`
Dirs []Section[[]string] `yaml:"dirs,flow"`
Rm []Section[[]string] `yaml:"rm,flow"`
Files []Section[[]File] `yaml:"files,flow"`
Cmd []Section[[]Command] `yaml:"cmd,flow"`
FS []Section[[]string] `yaml:"fs,flow"`
}
type Settings struct {
HTTP *HTTPClient `yaml:"http"`
Groups Groups `yaml:"groups"`
}
type HTTPClient struct {
HTTPClientParams `yaml:",inline"`
BaseURL AddrURL `yaml:"base_url"`
Debug bool `yaml:"debug"`
}
type Groups []Group
func (g Groups) ManualActions() map[string]struct{} {
manual := make(map[string]struct{})
for _, group := range g {
if !group.Manual {
continue
}
for _, action := range group.Actions {
manual[action] = struct{}{}
}
}
return manual
}
func (g Groups) GroupByAction() map[string]map[string]struct{} {
manual := make(map[string]map[string]struct{})
for _, group := range g {
for _, action := range group.Actions {
groups, ok := manual[action]
if !ok {
groups = make(map[string]struct{})
}
groups[group.Name] = struct{}{}
manual[action] = groups
}
}
return manual
}
type Group struct {
Name string `yaml:"name"`
Actions []string `yaml:"actions"`
Manual bool `yaml:"manual"`
}
type Section[T any] struct {
Line int32
Tag string
Val T
}
type File struct {
Path string `yaml:"path"`
Data *Bytes `yaml:"data"`
Get *Get `yaml:"get"`
Local *string `yaml:"local"`
}
type Bytes []byte
func (fd *Bytes) UnmarshalYAML(node *yaml.Node) error {
*fd = Bytes(node.Value)
return nil
}
type Command struct {
Dir string `yaml:"dir"`
Exec string `yaml:"exec"`
Args []string `yaml:"args,flow"`
}
func (c *Command) UnmarshalYAML(unmarshal func(any) error) error {
var raw string
if err := unmarshal(&raw); err == nil {
*c, err = commandFromString(raw)
if err != nil {
return err
}
return nil
}
type alias Command
var cmd alias
if err := unmarshal(&cmd); err != nil {
return err
}
*c = (Command)(cmd)
return nil
}
type Script struct {
Dir string `yaml:"dir"`
Exec string `yaml:"exec"`
Args []string `yaml:"args,flow"`
Script string `yaml:"script,flow"`
}
type Get struct {
HTTPClientParams `yaml:",inline"`
URL string `yaml:"url"`
}
type HTTPClientParams struct {
Headers map[string]string `yaml:"headers"`
QueryParams map[string]string `yaml:"query_params"`
}
type AddrURL struct {
*url.URL `yaml:",inline"`
}
func (addr *AddrURL) UnmarshalYAML(unmarshal func(any) error) error {
var raw string
if err := unmarshal(&raw); err != nil {
return err
}
u, err := url.Parse(raw)
if err != nil {
return xerrors.Errorf("unmarshal url: %w", err)
}
addr.URL = u
return nil
}
func (c Config) Validate() error {
for i, files := range c.Files {
for _, file := range files.Val {
err := validateFile(file)
if err != nil {
return xerrors.Errorf("files: %d [%s]: %w", i, file.Path, err)
}
}
}
if err := validateGroups(c.Settings.Groups); err != nil {
return xerrors.Errorf("groups: %w", err)
}
if err := validateConfigSections(c); err != nil {
return xerrors.Errorf("sections: %w", err)
}
return nil
}
func validateFile(file File) error {
notNil := entity.NotNilValues(file.Get, file.Data, file.Local)
switch {
case notNil == 0:
return xerrors.Errorf("files: `get`, `data`, `local` - all are empty")
case notNil > 1:
return xerrors.Errorf("files: sections `get`, `data`, `local` tags - only one can be present")
case strings.TrimSpace(file.Path) == entity.Empty:
return xerrors.Errorf("files: save `path` are empty")
}
return nil
}
func validateGroups(groups Groups) error {
var (
groupNameSet = make(map[string]int, len(groups))
groupNames = make([]string, 0, len(groups))
)
for _, group := range groups {
var (
name = group.Name
quantity, ok = groupNameSet[name]
)
if ok && quantity == 1 {
groupNames = append(groupNames, name)
}
groupNameSet[name] = groupNameSet[name] + 1
}
if len(groupNames) > 0 {
return xerrors.Errorf("duplicate names [%s]", strings.Join(groupNames, entity.LogSliceSep))
}
return nil
}
func validateConfigSections(conf Config) error {
var (
files = len(conf.Files)
dirs = len(conf.Dirs)
rm = len(conf.Rm)
cmd = len(conf.Cmd)
fs = len(conf.FS)
)
if files == 0 && dirs == 0 && rm == 0 && cmd == 0 && fs == 0 {
return xerrors.Errorf(
"config not contains executable actions [dirs: %d, rm: %d, files: %d, cms: %d, fs: %d]",
dirs, rm, files, cmd, fs,
)
}
return nil
}