Skip to content

Commit

Permalink
Add overwrites (#109)
Browse files Browse the repository at this point in the history
Relates to kairos-io/kairos#2492

Signed-off-by: Mauro Morales <[email protected]>
  • Loading branch information
mauromorales authored May 3, 2024
1 parent 5744b1a commit 14f7a86
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
11 changes: 10 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,16 @@ func Scan(o *Options, filter func(d []byte) ([]byte, error)) (*Config, error) {
}
}

return configs.Merge()
mergedConfig, err := configs.Merge()
if err != nil {
return mergedConfig, err
}

if o.Overwrites != "" {
yaml.Unmarshal([]byte(o.Overwrites), &mergedConfig) //nolint:errcheck
}

return mergedConfig, nil
}

func allFiles(dir []string) []string {
Expand Down
77 changes: 77 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,83 @@ stages:
})
})

Context("With Overwrittes", func() {
var tmpDir1 string
var err error

BeforeEach(func() {
tmpDir1, err = os.MkdirTemp("", "config1")
Expect(err).ToNot(HaveOccurred())
err := os.WriteFile(path.Join(tmpDir1, "local_config_1.yaml"), []byte(`#cloud-config
install:
auto: false
foo: bar
stages:
initramfs:
- users:
kairos:
groups:
- sudo
passwd: kairos
`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
})

AfterEach(func() {
err = os.RemoveAll(tmpDir1)
Expect(err).ToNot(HaveOccurred())
})

It("replaces completely the keys given by the overwrite", func() {
o := &Options{}
overwriteYaml := `#cloud-config
install:
auto: true
options:
device: /dev/sda
stages:
initramfs:
- users:
kairos:
groups:
- sudo
passwd: kairos
foobar:
groups:
- sudo
passwd: barbaz
`
err = o.Apply(
Directories(tmpDir1),
Overwrites(overwriteYaml),
)
Expect(err).ToNot(HaveOccurred())

c, err := Scan(o, FilterKeysTestMerge)
Expect(err).ToNot(HaveOccurred())

Expect(c.String()).To(Equal(`#cloud-config
foo: bar
install:
auto: true
options:
device: /dev/sda
stages:
initramfs:
- users:
foobar:
groups:
- sudo
passwd: barbaz
kairos:
groups:
- sudo
passwd: kairos
`))
})
})

Context("Deep merge maps within arrays", func() {
var cmdLinePath, tmpDir1 string
var err error
Expand Down
8 changes: 8 additions & 0 deletions collector/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Options struct {
NoLogs bool
StrictValidation bool
Readers []io.Reader
Overwrites string
}

type Option func(o *Options) error
Expand Down Expand Up @@ -72,3 +73,10 @@ func Readers(r ...io.Reader) Option {
return nil
}
}

func Overwrites(m string) Option {
return func(o *Options) error {
o.Overwrites = m
return nil
}
}

0 comments on commit 14f7a86

Please sign in to comment.