-
Notifications
You must be signed in to change notification settings - Fork 33
/
config.go
236 lines (191 loc) · 5.54 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
228
229
230
231
232
233
234
235
236
package config
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"github.com/bufbuild/protovalidate-go"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
"gopkg.in/yaml.v3"
configv1alpha1 "github.com/stateful/runme/v3/internal/gen/proto/go/runme/config/v1alpha1"
)
// Config is a flatten configuration of runme.yaml. The purpose of it is to
// unify all the different configuration versions into a single struct.
type Config struct {
// Dir- or git-based project fields.
DisableGitignore bool
IgnorePaths []string
FindRepoUpward bool
ProjectDir string
// Filemode fields.
Filename string
// Environment variable fields.
EnvSourceFiles []string
UseSystemEnv bool
Filters []*Filter
// Log related fields.
LogEnabled bool
LogPath string
LogVerbose bool
// Server related fields.
ServerAddress string
ServerTLSEnabled bool
ServerTLSCertFile string
ServerTLSKeyFile string
// Kernel related fields.
Kernels []Kernel
}
func ParseYAML(data []byte) (*Config, error) {
version, err := parseVersionFromYAML(data)
if err != nil {
return nil, err
}
switch version {
case "v1alpha1":
cfg, err := parseYAMLv1alpha1(data)
if err != nil {
return nil, err
}
if err := validateProto(cfg); err != nil {
return nil, errors.Wrap(err, "failed to validate v1alpha1 config")
}
config, err := configV1alpha1ToConfig(cfg)
if err != nil {
return nil, errors.Wrap(err, "failed to convert v1alpha1 config")
}
if err := validateConfig(config); err != nil {
return nil, errors.Wrap(err, "failed to validate config")
}
return config, nil
default:
return nil, errors.Errorf("unknown version: %s", version)
}
}
type versionOnly struct {
Version string `yaml:"version"`
}
func parseVersionFromYAML(data []byte) (string, error) {
var result versionOnly
if err := yaml.Unmarshal(data, &result); err != nil {
return "", errors.Wrap(err, "failed to unmarshal version")
}
return result.Version, nil
}
func parseYAMLv1alpha1(data []byte) (*configv1alpha1.Config, error) {
mmap := make(map[string]any)
if err := yaml.Unmarshal(data, &mmap); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal yaml")
}
delete(mmap, "version")
// In order to properly handle JSON-related field options like `json_name`,
// the YAML data is first marshaled to JSON and then unmarshaled to a proto message
// using the protojson package.
configJSONRaw, err := json.Marshal(mmap)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal yaml to json")
}
var cfg configv1alpha1.Config
if err := protojson.Unmarshal(configJSONRaw, &cfg); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal json to proto")
}
return &cfg, nil
}
func configV1alpha1ToConfig(c *configv1alpha1.Config) (*Config, error) {
project := c.GetProject()
log := c.GetLog()
var filters []*Filter
for _, f := range c.GetFilters() {
filters = append(filters, &Filter{
Type: f.GetType().String(),
Condition: f.GetCondition(),
})
}
var kernels []Kernel
for _, k := range c.GetKernels() {
msg, err := k.UnmarshalNew()
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal kernel")
}
switch k := msg.(type) {
case *configv1alpha1.Config_LocalKernel:
kernels = append(kernels, &LocalKernel{
Name: k.GetName(),
})
case *configv1alpha1.Config_DockerKernel:
kernels = append(kernels, &DockerKernel{
Build: struct {
Context string
Dockerfile string
}{
Context: k.GetBuild().GetContext(),
Dockerfile: k.GetBuild().GetDockerfile(),
},
Image: k.GetImage(),
Name: k.GetName(),
})
default:
return nil, errors.Errorf("unknown kernel type: %s", k.ProtoReflect().Type().Descriptor().FullName())
}
}
cfg := &Config{
ProjectDir: project.GetDir(),
FindRepoUpward: project.GetFindRepoUpward(),
IgnorePaths: project.GetIgnorePaths(),
DisableGitignore: project.GetDisableGitignore(),
Filename: c.GetFilename(),
UseSystemEnv: c.GetEnv().GetUseSystemEnv(),
EnvSourceFiles: c.GetEnv().GetSources(),
Filters: filters,
LogEnabled: log.GetEnabled(),
LogPath: log.GetPath(),
LogVerbose: log.GetVerbose(),
ServerAddress: c.GetServer().GetAddress(),
ServerTLSEnabled: c.GetServer().GetTls().GetEnabled(),
ServerTLSCertFile: c.GetServer().GetTls().GetCertFile(),
ServerTLSKeyFile: c.GetServer().GetTls().GetKeyFile(),
Kernels: kernels,
}
return cfg, nil
}
func validateConfig(cfg *Config) error {
cwd, err := os.Getwd()
if err != nil {
cwd = "."
}
if err := validateProjectDir(cfg, cwd); err != nil {
return errors.Wrap(err, "failed to validate project dir")
}
if err := validateFilename(cfg, cwd); err != nil {
return errors.Wrap(err, "failed to validate filename")
}
return nil
}
func validateProjectDir(cfg *Config, cwd string) error {
rel, err := filepath.Rel(cwd, filepath.Join(cwd, cfg.ProjectDir))
if err != nil {
return errors.WithStack(err)
}
if strings.HasPrefix(rel, "..") {
return errors.New("outside of current working directory")
}
return nil
}
func validateFilename(cfg *Config, cwd string) error {
rel, err := filepath.Rel(cwd, filepath.Join(cwd, cfg.Filename))
if err != nil {
return errors.WithStack(err)
}
if strings.HasPrefix(rel, "..") {
return errors.New("outside of current working directory")
}
return nil
}
func validateProto(m protoreflect.ProtoMessage) error {
v, err := protovalidate.New()
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(v.Validate(m))
}