Skip to content

Commit

Permalink
add support for inline config
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Nov 7, 2023
1 parent d2e261c commit 6ea2d78
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,8 @@
"type": "object",
"properties": {
"name": {"type": "string"},
"content": {"type": "string"},
"environment": {"type": "string"},
"file": {"type": "string"},
"external": {
"type": ["boolean", "object"],
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ type FileObjectConfig struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
File string `yaml:"file,omitempty" json:"file,omitempty"`
Environment string `yaml:"environment,omitempty" json:"environment,omitempty"`
Content string `yaml:"content,omitempty" json:"content,omitempty"`
External External `yaml:"external,omitempty" json:"external,omitempty"`
Labels Labels `yaml:"labels,omitempty" json:"labels,omitempty"`
Driver string `yaml:"driver,omitempty" json:"driver,omitempty"`
Expand Down
27 changes: 27 additions & 0 deletions validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
package validation

import (
"fmt"
"strings"

"github.com/compose-spec/compose-go/v2/tree"
)

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

var checks = map[tree.Path]checkerFunc{
"volumes.*": checkVolume,
"configs.*": checkFileObject("file", "environment", "content"),
"secrets.*": checkFileObject("file", "environment"),
}

func Validate(dict map[string]any) error {
Expand Down Expand Up @@ -54,3 +59,25 @@ func check(value any, p tree.Path) error {
}
return nil
}

func checkFileObject(keys ...string) checkerFunc {
return func(value any, p tree.Path) error {

v := value.(map[string]any)
count := 0
for _, s := range keys {
if _, ok := v[s]; ok {
count++
}
}
if count > 1 {
return fmt.Errorf("%s: %s attributes are mutually exclusive", p, strings.Join(keys, "|"))
}
if count == 0 {
if _, ok := v["external"]; !ok {
return fmt.Errorf("%s: one of %s must be set", p, strings.Join(keys, "|"))
}
}
return nil
}
}
96 changes: 96 additions & 0 deletions validation/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2020 The Compose Specification Authors.
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 validation

import (
"testing"

"github.com/compose-spec/compose-go/v2/tree"
"gopkg.in/yaml.v3"
"gotest.tools/v3/assert"
)

func TestValidateSecret(t *testing.T) {
checker := checks["configs.*"]
tests := []struct {
name string
input string
err string
}{
{
name: "file config",
input: `
name: test
file: ./httpd.conf
`,
err: "",
},
{
name: "environment config",
input: `
name: test
environment: CONFIG
`,
err: "",
},
{
name: "inlined config",
input: `
name: test
content: foo=bar
`,
err: "",
},
{
name: "conflict config",
input: `
name: test
environment: CONFIG
content: foo=bar
`,
err: "configs.test: file|environment|content attributes are mutually exclusive",
},
{
name: "missing config",
input: `
name: test
`,
err: "configs.test: one of file|environment|content must be set",
},
{
name: "external config",
input: `
name: test
external: true
`,
err: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var input map[string]any
err := yaml.Unmarshal([]byte(tt.input), &input)
assert.NilError(t, err)
err = checker(input, tree.NewPath("configs.test"))
if tt.err == "" {
assert.NilError(t, err)
} else {
assert.Equal(t, tt.err, err.Error())
}
})
}
}

0 comments on commit 6ea2d78

Please sign in to comment.