/
toml.go
81 lines (71 loc) · 2.11 KB
/
toml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package testutil
import (
"bytes"
"context"
"fmt"
"reflect"
"github.com/BurntSushi/toml"
"github.com/docker/docker/client"
"github.com/strangelove-ventures/interchaintest/v3/internal/dockerutil"
"go.uber.org/zap"
)
// Toml is used for holding the decoded state of a toml config file.
type Toml map[string]any
// recursiveModifyToml will apply toml modifications at the current depth,
// then recurse for new depths.
func recursiveModifyToml(c map[string]any, modifications Toml) error {
for key, value := range modifications {
if reflect.ValueOf(value).Kind() == reflect.Map {
cV, ok := c[key]
if !ok {
// Did not find section in existing config, populating fresh.
cV = make(Toml)
}
// Retrieve existing config to apply overrides to.
cVM, ok := cV.(map[string]any)
if !ok {
return fmt.Errorf("failed to convert section to (map[string]any), found (%T)", cV)
}
if err := recursiveModifyToml(cVM, value.(Toml)); err != nil {
return err
}
c[key] = cVM
} else {
// Not a map, so we can set override value directly.
c[key] = value
}
}
return nil
}
// ModifyTomlConfigFile reads, modifies, then overwrites a toml config file, useful for config.toml, app.toml, etc.
func ModifyTomlConfigFile(
ctx context.Context,
logger *zap.Logger,
dockerClient *client.Client,
testName string,
volumeName string,
filePath string,
modifications Toml,
) error {
fr := dockerutil.NewFileRetriever(logger, dockerClient, testName)
config, err := fr.SingleFileContent(ctx, volumeName, filePath)
if err != nil {
return fmt.Errorf("failed to retrieve %s: %w", filePath, err)
}
var c Toml
if err := toml.Unmarshal(config, &c); err != nil {
return fmt.Errorf("failed to unmarshal %s: %w", filePath, err)
}
if err := recursiveModifyToml(c, modifications); err != nil {
return err
}
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(c); err != nil {
return err
}
fw := dockerutil.NewFileWriter(logger, dockerClient, testName)
if err := fw.WriteFile(ctx, volumeName, filePath, buf.Bytes()); err != nil {
return fmt.Errorf("overwriting %s: %w", filePath, err)
}
return nil
}