Skip to content

Commit 9efe7f6

Browse files
idsulikndeloof
authored andcommitted
removed required from LabelFile
Signed-off-by: Suleiman Dibirov <[email protected]>
1 parent 1e3b58d commit 9efe7f6

File tree

7 files changed

+76
-20
lines changed

7 files changed

+76
-20
lines changed

loader/example1.label

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# passed through
2+
FOO=foo_from_label_file
3+
LABEL.WITH.DOT=ok
4+
LABEL_WITH_UNDERSCORE=ok
5+
6+
# overridden in example2.label
7+
BAR=bar_from_label_file
8+
9+
# overridden in full-example.yml
10+
BAZ=baz_from_label_file

loader/example2.label

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BAR=bar_from_label_file_2
2+
3+
# overridden in configDetails.Labels
4+
QUX=quz_from_label_file_2

loader/full-example.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ services:
210210
# - "com.example.number=42"
211211
# - "com.example.empty-label"
212212

213+
label_file:
214+
- ./example1.label
215+
- ./example2.label
216+
213217
links:
214218
- db
215219
- db:database

loader/full-struct_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,24 @@ func services(workingDir, homeDir string) types.Services {
217217
Ipc: "host",
218218
Uts: "host",
219219
Labels: map[string]string{
220+
"FOO": "foo_from_label_file",
221+
"BAR": "bar_from_label_file_2",
222+
"BAZ": "baz_from_label_file",
223+
"QUX": "quz_from_label_file_2",
224+
"LABEL.WITH.DOT": "ok",
225+
"LABEL_WITH_UNDERSCORE": "ok",
220226
"com.example.description": "Accounting webapp",
221227
"com.example.number": "42",
222228
"com.example.empty-label": "",
223229
},
230+
LabelFiles: []types.LabelFile{
231+
{
232+
Path: filepath.Join(workingDir, "example1.label"),
233+
},
234+
{
235+
Path: filepath.Join(workingDir, "example2.label"),
236+
},
237+
},
224238
Links: []string{
225239
"db",
226240
"db:database",
@@ -770,9 +784,18 @@ services:
770784
image: redis
771785
ipc: host
772786
labels:
787+
BAR: bar_from_label_file_2
788+
BAZ: baz_from_label_file
789+
FOO: foo_from_label_file
790+
LABEL.WITH.DOT: ok
791+
LABEL_WITH_UNDERSCORE: ok
792+
QUX: quz_from_label_file_2
773793
com.example.description: Accounting webapp
774794
com.example.empty-label: ""
775795
com.example.number: "42"
796+
label_file:
797+
- %s
798+
- %s
776799
links:
777800
- db
778801
- db:database
@@ -1058,6 +1081,8 @@ x-nested:
10581081
filepath.Join(workingDir, "bar"),
10591082
filepath.Join(workingDir, "example1.env"),
10601083
filepath.Join(workingDir, "example2.env"),
1084+
filepath.Join(workingDir, "example1.label"),
1085+
filepath.Join(workingDir, "example2.label"),
10611086
workingDir,
10621087
filepath.Join(workingDir, "static"),
10631088
filepath.Join(homeDir, "configs"),
@@ -1382,10 +1407,20 @@ func fullExampleJSON(workingDir, homeDir string) string {
13821407
"image": "redis",
13831408
"ipc": "host",
13841409
"labels": {
1410+
"BAR": "bar_from_label_file_2",
1411+
"BAZ": "baz_from_label_file",
1412+
"FOO": "foo_from_label_file",
1413+
"LABEL.WITH.DOT": "ok",
1414+
"LABEL_WITH_UNDERSCORE": "ok",
1415+
"QUX": "quz_from_label_file_2",
13851416
"com.example.description": "Accounting webapp",
13861417
"com.example.empty-label": "",
13871418
"com.example.number": "42"
13881419
},
1420+
"label_file": [
1421+
"%s",
1422+
"%s"
1423+
],
13891424
"links": [
13901425
"db",
13911426
"db:database",
@@ -1707,6 +1742,8 @@ func fullExampleJSON(workingDir, homeDir string) string {
17071742
toPath(workingDir, "bar"),
17081743
toPath(workingDir, "example1.env"),
17091744
toPath(workingDir, "example2.env"),
1745+
toPath(workingDir, "example1.label"),
1746+
toPath(workingDir, "example2.label"),
17101747
toPath(workingDir),
17111748
toPath(workingDir, "static"),
17121749
toPath(homeDir, "configs"),

loader/loader_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ func TestLoadServiceWithLabelFile(t *testing.T) {
23322332
"test": {
23332333
Name: "test",
23342334
LabelFiles: []types.LabelFile{
2335-
{Path: file.Name(), Required: true},
2335+
{Path: file.Name()},
23362336
},
23372337
},
23382338
},
@@ -2344,6 +2344,21 @@ func TestLoadServiceWithLabelFile(t *testing.T) {
23442344
assert.Equal(t, "MY_VALUE", service.Labels["MY_LABEL"])
23452345
}
23462346

2347+
func TestLoadServiceWithLabelFile_NotExists(t *testing.T) {
2348+
p := &types.Project{
2349+
Services: types.Services{
2350+
"test": {
2351+
Name: "test",
2352+
LabelFiles: []types.LabelFile{
2353+
{Path: "test"},
2354+
},
2355+
},
2356+
},
2357+
}
2358+
p, err := p.WithServicesLabelsResolved(false)
2359+
assert.Error(t, err, "label file test not found: stat test: no such file or directory")
2360+
}
2361+
23472362
func TestLoadNoSSHInBuildConfig(t *testing.T) {
23482363
actual, err := loadYAML(`
23492364
name: load-no-ssh-in-build-config

types/labelfile.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,16 @@ import (
2121
)
2222

2323
type LabelFile struct {
24-
Path string `yaml:"path,omitempty" json:"path,omitempty"`
25-
Required bool `yaml:"required" json:"required"`
26-
Format string `yaml:"format,omitempty" json:"format,omitempty"`
24+
Path string `yaml:"path,omitempty" json:"path,omitempty"`
25+
Format string `yaml:"format,omitempty" json:"format,omitempty"`
2726
}
2827

2928
// MarshalYAML makes LabelFile implement yaml.Marshaler
3029
func (e LabelFile) MarshalYAML() (interface{}, error) {
31-
if e.Required {
32-
return e.Path, nil
33-
}
34-
return map[string]any{
35-
"path": e.Path,
36-
"required": e.Required,
37-
}, nil
30+
return e.Path, nil
3831
}
3932

4033
// MarshalJSON makes LabelFile implement json.Marshaler
4134
func (e *LabelFile) MarshalJSON() ([]byte, error) {
42-
if e.Required {
43-
return json.Marshal(e.Path)
44-
}
45-
// Pass as a value to avoid re-entering this method and use the default implementation
46-
return json.Marshal(*e)
35+
return json.Marshal(e.Path)
4736
}

types/project.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,7 @@ func loadEnvFile(envFile EnvFile, resolve dotenv.LookupFn) (Mapping, error) {
714714

715715
func loadLabelFile(labelFile LabelFile, resolve dotenv.LookupFn) (Mapping, error) {
716716
if _, err := os.Stat(labelFile.Path); os.IsNotExist(err) {
717-
if labelFile.Required {
718-
return nil, fmt.Errorf("label file %s not found: %w", labelFile.Path, err)
719-
}
720-
return nil, nil
717+
return nil, fmt.Errorf("label file %s not found: %w", labelFile.Path, err)
721718
}
722719

723720
return loadMappingFile(labelFile.Path, labelFile.Format, resolve)

0 commit comments

Comments
 (0)