Skip to content

Commit 8df318e

Browse files
authored
Merge pull request #478 from ndeloof/inline_config
add support for inline config
2 parents d2e261c + ed0c8ae commit 8df318e

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

schema/compose-spec.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@
764764
"type": "object",
765765
"properties": {
766766
"name": {"type": "string"},
767+
"content": {"type": "string"},
768+
"environment": {"type": "string"},
767769
"file": {"type": "string"},
768770
"external": {
769771
"type": ["boolean", "object"],

types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ type FileObjectConfig struct {
699699
Name string `yaml:"name,omitempty" json:"name,omitempty"`
700700
File string `yaml:"file,omitempty" json:"file,omitempty"`
701701
Environment string `yaml:"environment,omitempty" json:"environment,omitempty"`
702+
Content string `yaml:"content,omitempty" json:"content,omitempty"`
702703
External External `yaml:"external,omitempty" json:"external,omitempty"`
703704
Labels Labels `yaml:"labels,omitempty" json:"labels,omitempty"`
704705
Driver string `yaml:"driver,omitempty" json:"driver,omitempty"`

validation/validation.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
package validation
1818

1919
import (
20+
"fmt"
21+
"strings"
22+
2023
"github.com/compose-spec/compose-go/v2/tree"
2124
)
2225

2326
type checkerFunc func(value any, p tree.Path) error
2427

2528
var checks = map[tree.Path]checkerFunc{
2629
"volumes.*": checkVolume,
30+
"configs.*": checkFileObject("file", "environment", "content"),
31+
"secrets.*": checkFileObject("file", "environment"),
2732
}
2833

2934
func Validate(dict map[string]any) error {
@@ -54,3 +59,29 @@ func check(value any, p tree.Path) error {
5459
}
5560
return nil
5661
}
62+
63+
func checkFileObject(keys ...string) checkerFunc {
64+
return func(value any, p tree.Path) error {
65+
66+
v := value.(map[string]any)
67+
count := 0
68+
for _, s := range keys {
69+
if _, ok := v[s]; ok {
70+
count++
71+
}
72+
}
73+
if count > 1 {
74+
return fmt.Errorf("%s: %s attributes are mutually exclusive", p, strings.Join(keys, "|"))
75+
}
76+
if count == 0 {
77+
if _, ok := v["driver"]; ok {
78+
// User specified a custom driver, which might have it's own way to set content
79+
return nil
80+
}
81+
if _, ok := v["external"]; !ok {
82+
return fmt.Errorf("%s: one of %s must be set", p, strings.Join(keys, "|"))
83+
}
84+
}
85+
return nil
86+
}
87+
}

validation/validation_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright 2020 The Compose Specification Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package validation
18+
19+
import (
20+
"testing"
21+
22+
"github.com/compose-spec/compose-go/v2/tree"
23+
"gopkg.in/yaml.v3"
24+
"gotest.tools/v3/assert"
25+
)
26+
27+
func TestValidateSecret(t *testing.T) {
28+
checker := checks["configs.*"]
29+
tests := []struct {
30+
name string
31+
input string
32+
err string
33+
}{
34+
{
35+
name: "file config",
36+
input: `
37+
name: test
38+
file: ./httpd.conf
39+
`,
40+
err: "",
41+
},
42+
{
43+
name: "environment config",
44+
input: `
45+
name: test
46+
environment: CONFIG
47+
`,
48+
err: "",
49+
},
50+
{
51+
name: "inlined config",
52+
input: `
53+
name: test
54+
content: foo=bar
55+
`,
56+
err: "",
57+
},
58+
{
59+
name: "conflict config",
60+
input: `
61+
name: test
62+
environment: CONFIG
63+
content: foo=bar
64+
`,
65+
err: "configs.test: file|environment|content attributes are mutually exclusive",
66+
},
67+
{
68+
name: "missing config",
69+
input: `
70+
name: test
71+
`,
72+
err: "configs.test: one of file|environment|content must be set",
73+
},
74+
{
75+
name: "external config",
76+
input: `
77+
name: test
78+
external: true
79+
`,
80+
err: "",
81+
},
82+
}
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
var input map[string]any
86+
err := yaml.Unmarshal([]byte(tt.input), &input)
87+
assert.NilError(t, err)
88+
err = checker(input, tree.NewPath("configs.test"))
89+
if tt.err == "" {
90+
assert.NilError(t, err)
91+
} else {
92+
assert.Equal(t, tt.err, err.Error())
93+
}
94+
})
95+
}
96+
}

0 commit comments

Comments
 (0)