-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathio.go
More file actions
131 lines (111 loc) · 3.48 KB
/
io.go
File metadata and controls
131 lines (111 loc) · 3.48 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package seiconfig
import (
"bytes"
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
const (
configDir = "config"
configTomlFile = "config.toml"
appTomlFile = "app.toml"
)
// ReadConfigFromDir reads config.toml and app.toml from homeDir/config/ and
// merges them into a unified SeiConfig.
func ReadConfigFromDir(homeDir string) (*SeiConfig, error) {
cfgDir := filepath.Join(homeDir, configDir)
configPath := filepath.Join(cfgDir, configTomlFile)
appPath := filepath.Join(cfgDir, appTomlFile)
var tm legacyTendermintConfig
if _, err := toml.DecodeFile(configPath, &tm); err != nil {
return nil, fmt.Errorf("reading %s: %w", configPath, err)
}
var app legacyAppConfig
if _, err := toml.DecodeFile(appPath, &app); err != nil {
return nil, fmt.Errorf("reading %s: %w", appPath, err)
}
cfg := fromLegacy(tm, app)
return cfg, nil
}
// WriteConfigToDir writes the SeiConfig as config.toml and app.toml into
// homeDir/config/. Writes are atomic (temp file + rename) to prevent
// corruption on crash.
func WriteConfigToDir(cfg *SeiConfig, homeDir string) error {
cfgDir := filepath.Join(homeDir, configDir)
if err := os.MkdirAll(cfgDir, 0o755); err != nil {
return fmt.Errorf("creating config directory: %w", err)
}
configPath := filepath.Join(cfgDir, configTomlFile)
appPath := filepath.Join(cfgDir, appTomlFile)
tm := cfg.toLegacyTendermint()
if err := atomicWriteTOML(configPath, tm); err != nil {
return fmt.Errorf("writing %s: %w", configPath, err)
}
app := cfg.toLegacyApp()
if err := atomicWriteTOML(appPath, app); err != nil {
return fmt.Errorf("writing %s: %w", appPath, err)
}
return nil
}
// ApplyOverrides applies a map of dotted-key overrides to a SeiConfig.
// Keys use the unified schema paths (e.g. "evm.http_port", "storage.pruning").
// This is the primary mechanism for the sidecar's ConfigApplyTask and the
// controller's spec.config.overrides.
//
// Each TOML key is resolved to its Go struct field path via the Registry, then
// set directly through reflection — the same path used by ResolveEnv.
func ApplyOverrides(cfg *SeiConfig, overrides map[string]string) error {
if len(overrides) == 0 {
return nil
}
reg := BuildRegistry()
for key, val := range overrides {
f := reg.Field(key)
if f == nil {
return fmt.Errorf("unknown override key %q", key)
}
if err := setFieldByPath(cfg, f.FieldPath, val); err != nil {
return fmt.Errorf("applying override %q=%q: %w", key, val, err)
}
}
return nil
}
// atomicWriteTOML encodes v as TOML and writes it atomically to path.
func atomicWriteTOML(path string, v any) error {
var buf bytes.Buffer
enc := toml.NewEncoder(&buf)
if err := enc.Encode(v); err != nil {
return err
}
dir := filepath.Dir(path)
tmp, err := os.CreateTemp(dir, ".sei-config-*.tmp")
if err != nil {
return fmt.Errorf("creating temp file: %w", err)
}
tmpPath := tmp.Name()
cleanup := func() { _ = os.Remove(tmpPath) }
if _, err := tmp.Write(buf.Bytes()); err != nil {
_ = tmp.Close()
cleanup()
return fmt.Errorf("writing temp file: %w", err)
}
if err := tmp.Sync(); err != nil {
_ = tmp.Close()
cleanup()
return fmt.Errorf("syncing temp file: %w", err)
}
if err := tmp.Close(); err != nil {
cleanup()
return fmt.Errorf("closing temp file: %w", err)
}
if err := os.Chmod(tmpPath, 0o644); err != nil {
cleanup()
return fmt.Errorf("setting permissions: %w", err)
}
if err := os.Rename(tmpPath, path); err != nil {
cleanup()
return fmt.Errorf("renaming temp file: %w", err)
}
return nil
}