Skip to content

Commit

Permalink
bugfix: ignore extra fields in yaml (#107)
Browse files Browse the repository at this point in the history
Ignoring extra fields makes the code forward compatible with changes in
the yaml format, without a version bump being necessary.
  • Loading branch information
letFunny authored Dec 13, 2023
1 parent de42fd9 commit 2266288
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func readSlices(release *Release, baseDir, dirName string) error {

type yamlRelease struct {
Format string `yaml:"format"`
Archives map[string]yamlArchive `yaml:"archives`
Archives map[string]yamlArchive `yaml:"archives"`
Keyrings map[string]string `yaml:"public-keys"`
}

Expand Down Expand Up @@ -406,7 +406,7 @@ func parseRelease(baseDir, filePath string, data []byte) (*Release, error) {

yamlVar := yamlRelease{}
dec := yaml.NewDecoder(bytes.NewBuffer(data))
dec.KnownFields(true)
dec.KnownFields(false)
err := dec.Decode(&yamlVar)
if err != nil {
return nil, fmt.Errorf("%s: cannot parse release definition: %v", fileName, err)
Expand Down Expand Up @@ -479,7 +479,7 @@ func parsePackage(baseDir, pkgName, pkgPath string, data []byte) (*Package, erro

yamlPkg := yamlPackage{}
dec := yaml.NewDecoder(bytes.NewBuffer(data))
dec.KnownFields(true)
dec.KnownFields(false)
err := dec.Decode(&yamlPkg)
if err != nil {
return nil, fmt.Errorf("cannot parse package %q slice definitions: %v", pkgName, err)
Expand Down
51 changes: 51 additions & 0 deletions internal/setup/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,57 @@ var setupTests = []setupTest{{
`,
},
relerror: `chisel.yaml: cannot parse keyring "ubuntu-master": openpgp: invalid argument: no armored data found`,
}, {
summary: "Extra fields in YAML are ignored (necessary for forward compatibility)",
input: map[string]string{
"chisel.yaml": `
format: chisel-v1
archives:
ubuntu:
version: 22.04
components: [main, other]
suites: [jammy, jammy-security]
madeUpKey1: whatever
madeUpKey2: whatever
`,
"slices/mydir/mypkg.yaml": `
package: mypkg
madeUpKey3: whatever
slices:
myslice:
madeUpKey4: whatever
contents:
/path: {madeUpKey5: whatever}
`,
},
release: &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Version: "22.04",
Suites: []string{"jammy", "jammy-security"},
Components: []string{"main", "other"},
},
},
Packages: map[string]*setup.Package{
"mypkg": {
Archive: "ubuntu",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{
"myslice": {
Package: "mypkg",
Name: "myslice",
Contents: map[string]setup.PathInfo{
"/path": {Kind: "copy"},
},
},
},
},
},
},
}}

const defaultChiselYaml = `
Expand Down

0 comments on commit 2266288

Please sign in to comment.