Add overwrites (#109)

Relates to kairos-io/kairos#2492

Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>
This commit is contained in:
Mauro Morales 2024-05-03 18:01:32 +02:00 committed by GitHub
parent 5744b1abb2
commit 14f7a86c1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 95 additions and 1 deletions

View File

@ -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 {

View File

@ -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

View File

@ -12,6 +12,7 @@ type Options struct {
NoLogs bool
StrictValidation bool
Readers []io.Reader
Overwrites string
}
type Option func(o *Options) error
@ -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
}
}