-
Notifications
You must be signed in to change notification settings - Fork 61
/
project_options.go
325 lines (271 loc) · 7.93 KB
/
project_options.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2020 The Compose Specification Authors.
// Copyright 2022 Unikraft GmbH. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
interp "github.com/compose-spec/compose-go/interpolation"
"github.com/compose-spec/compose-go/template"
"kraftkit.sh/kconfig"
)
// kraftfile is a filename and the contents of the file
type Kraftfile struct {
// path is the path of the yaml configuration file
path string
// content is the raw yaml content. Will be loaded from Filename if not set
content []byte
// config if the yaml tree for this config file. Will be parsed from Content
// if not set
config map[string]interface{}
}
// ProjectOptions group configuration options used to instantiate a new
// ApplicationConfig from a working directory and a kraftfile
type ProjectOptions struct {
name string
workdir string
kraftfile *Kraftfile
kconfig kconfig.KeyValueMap
skipValidation bool
skipInterpolation bool
skipNormalization bool
resolvePaths bool
interpolate *interp.Options
// Indicates when the projectName was imperatively set or guessed from path
projectNameImperativelySet bool
}
// Workdir returns the working directory determined by provided kraft files
func (popts *ProjectOptions) Workdir() (string, error) {
if popts.workdir != "" {
return popts.workdir, nil
}
if popts.kraftfile == nil {
return os.Getwd()
}
if popts.kraftfile.path != "" && popts.kraftfile.path != "-" {
absPath, err := filepath.Abs(popts.kraftfile.path)
if err != nil {
return "", err
}
return filepath.Dir(absPath), nil
}
return os.Getwd()
}
// RelativePath resolve a relative path based project's working directory
func (popts *ProjectOptions) RelativePath(path string) string {
if path[0] == '~' {
home, _ := os.UserHomeDir()
path = filepath.Join(home, path[1:])
}
if filepath.IsAbs(path) {
return path
}
return filepath.Join(popts.workdir, path)
}
// LookupConfig provides a lookup function for config variables
func (popts *ProjectOptions) LookupConfig(key string) (string, bool) {
v, ok := popts.kconfig[key]
return v.Value, ok
}
// SetProjectName sets the project name along with whether the name is set
// imperatively (i.e. whether it was derived from the working directory)
func (popts *ProjectOptions) SetProjectName(name string, imperativelySet bool) {
popts.name = name
popts.projectNameImperativelySet = imperativelySet
}
// GetProjectName returns the project name and whether the project name was set
// impartively (i.e. by the working directory's name)
func (popts *ProjectOptions) GetProjectName() (string, bool) {
return popts.name, popts.projectNameImperativelySet
}
// AddKraftfile adds and extracts the file contents and attaches it to the
// ProjectOptions.
func (popts *ProjectOptions) AddKraftfile(file string) error {
var b []byte
var err error
if file == "-" {
b, err = io.ReadAll(os.Stdin)
if err != nil {
return err
}
} else {
abs, err := filepath.Abs(file)
if err != nil {
return err
}
f := abs
if _, err := os.Stat(f); err != nil {
return err
}
b, err = os.ReadFile(f)
if err != nil {
return err
}
}
popts.kraftfile = &Kraftfile{
path: file,
content: b,
}
return nil
}
// AddKraftfileFromBytes adds and extracts the file contents from a given byte slice
// and attaches it to the ProjectOptions.
func (popts *ProjectOptions) AddKraftfileFromBytes(content []byte) error {
popts.kraftfile = &Kraftfile{
content: content,
}
return nil
}
type ProjectOption func(*ProjectOptions) error
// NewProjectOptions creates ProjectOptions
func NewProjectOptions(opts ...ProjectOption) (*ProjectOptions, error) {
popts := &ProjectOptions{
kconfig: kconfig.KeyValueMap{},
}
popts.interpolate = &interp.Options{
Substitute: template.Substitute,
LookupValue: popts.LookupConfig,
}
for _, o := range opts {
err := o(popts)
if err != nil {
return nil, err
}
}
return popts, nil
}
// WithProjectName defines ProjectOptions' name
func WithProjectName(name string) ProjectOption {
return func(popts *ProjectOptions) error {
popts.name = name
return nil
}
}
// WithProjectWorkdir defines ProjectOptions' working directory
func WithProjectWorkdir(workdir string) ProjectOption {
return func(popts *ProjectOptions) error {
if workdir == "" {
return nil
}
abs, err := filepath.Abs(workdir)
if err != nil {
return err
}
popts.workdir = abs
return nil
}
}
// WithProjectConfig defines a key=value set of variables used for kraft file
// interpolation as well as with Unikraft's build system
func WithProjectConfig(config []string) ProjectOption {
return func(popts *ProjectOptions) error {
for k, v := range getAsEqualsMap(config) {
popts.kconfig.Set(k, v)
}
return nil
}
}
// WithProjectKraftfile adds a kraft file to the project
func WithProjectKraftfile(file string) ProjectOption {
return func(popts *ProjectOptions) error {
return popts.AddKraftfile(file)
}
}
// WithProjectKraftfile adds a kraft file to the project
func WithProjectKraftfileFromBytes(content []byte) ProjectOption {
return func(popts *ProjectOptions) error {
return popts.AddKraftfileFromBytes(content)
}
}
// WithProjectInterpolation set ProjectOptions to enable/skip interpolation
func WithProjectInterpolation(interpolation bool) ProjectOption {
return func(popts *ProjectOptions) error {
popts.skipInterpolation = !interpolation
return nil
}
}
// WithProjectNormalization set ProjectOptions to enable/skip normalization
func WithProjectNormalization(normalization bool) ProjectOption {
return func(popts *ProjectOptions) error {
popts.skipNormalization = !normalization
return nil
}
}
// WithProjectResolvedPaths set ProjectOptions to enable paths resolution
func WithProjectResolvedPaths(resolve bool) ProjectOption {
return func(popts *ProjectOptions) error {
popts.resolvePaths = resolve
return nil
}
}
// WithSkipValidation sets the LoaderOptions to skip validation when loading
// sections
func WithProjectSkipValidation(skipValidation bool) ProjectOption {
return func(popts *ProjectOptions) error {
popts.skipValidation = true
return nil
}
}
func findFiles(names []string, pwd string) []string {
candidates := []string{}
for _, n := range names {
f := filepath.Join(pwd, n)
if _, err := os.Stat(f); err == nil {
candidates = append(candidates, f)
}
}
return candidates
}
// getAsEqualsMap split key=value formatted strings into a key : value map
func getAsEqualsMap(em []string) map[string]string {
m := make(map[string]string)
for _, v := range em {
kv := strings.SplitN(v, "=", 2)
m[kv[0]] = kv[1]
}
return m
}
// WithProjectDefaultKraftfiles searches for default kraft files from working
// directory
func WithProjectDefaultKraftfiles() ProjectOption {
return func(popts *ProjectOptions) error {
if popts.kraftfile != nil {
return nil
}
pwd, err := popts.Workdir()
if err != nil {
return err
}
for {
candidates := findFiles(DefaultFileNames, pwd)
if len(candidates) > 0 {
if len(candidates) > 1 {
return fmt.Errorf("found multiple config files with supported names: %s", strings.Join(candidates, ", "))
}
return popts.AddKraftfile(candidates[0])
}
parent := filepath.Dir(pwd)
if parent == pwd {
// no config file found, but that's not a blocker if caller only needs project name
return nil
}
pwd = parent
}
}
}