talos/internal/pkg/toml/merge.go
Andrey Smirnov 3c9f7a7de6
chore: re-enable nolintlint and typecheck linters
Drop startup/rand.go, as since Go 1.20 `rand.Seed` is done
automatically.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
2023-08-25 01:05:41 +04:00

49 lines
1.2 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package toml
import (
"bytes"
"fmt"
"github.com/BurntSushi/toml"
"github.com/siderolabs/talos/pkg/machinery/config/merge"
)
// Merge several TOML documents in files into one.
//
// Merge process relies on generic map[string]interface{} merge which might not always be correct.
func Merge(parts []string) ([]byte, error) {
merged := map[string]interface{}{}
var header []byte
for _, part := range parts {
partial := map[string]interface{}{}
if _, err := toml.DecodeFile(part, &partial); err != nil {
return nil, fmt.Errorf("error decoding %q: %w", part, err)
}
if err := merge.Merge(merged, partial); err != nil {
return nil, fmt.Errorf("error merging %q: %w", part, err)
}
header = append(header, []byte(fmt.Sprintf("## %s\n", part))...)
}
var out bytes.Buffer
_, _ = out.Write(header)
_ = out.WriteByte('\n')
if err := toml.NewEncoder(&out).Encode(merged); err != nil {
return nil, fmt.Errorf("error encoding merged config: %w", err)
}
return out.Bytes(), nil
}