Skip to content

Commit bca6c99

Browse files
authored
Fix viper config parsing (#19)
This fixes viper's config parsing to properly read the settings config and fixes precedence of CLI flags -> config settings -> defaults.
1 parent 23fc058 commit bca6c99

4 files changed

Lines changed: 40 additions & 39 deletions

File tree

config/settings.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"time"
68

@@ -10,18 +12,18 @@ import (
1012

1113
// Settings holds all CLI-configurable parameters
1214
type Settings struct {
13-
Workers int `json:"workers"`
14-
TPS float64 `json:"tps"`
15-
StatsInterval Duration `json:"statsInterval"`
16-
BufferSize int `json:"bufferSize"`
17-
DryRun bool `json:"dryRun"`
18-
Debug bool `json:"debug"`
19-
TrackReceipts bool `json:"trackReceipts"`
20-
TrackBlocks bool `json:"trackBlocks"`
21-
TrackUserLatency bool `json:"trackUserLatency"`
22-
Prewarm bool `json:"prewarm"`
23-
RampUp bool `json:"rampUp"`
24-
ReportPath string `json:"reportPath"`
15+
Workers int `json:"workers,omitempty"`
16+
TPS float64 `json:"tps,omitempty"`
17+
StatsInterval Duration `json:"statsInterval,omitempty"`
18+
BufferSize int `json:"bufferSize,omitempty"`
19+
DryRun bool `json:"dryRun,omitempty"`
20+
Debug bool `json:"debug,omitempty"`
21+
TrackReceipts bool `json:"trackReceipts,omitempty"`
22+
TrackBlocks bool `json:"trackBlocks,omitempty"`
23+
TrackUserLatency bool `json:"trackUserLatency,omitempty"`
24+
Prewarm bool `json:"prewarm,omitempty"`
25+
RampUp bool `json:"rampUp,omitempty"`
26+
ReportPath string `json:"reportPath,omitempty"`
2527
}
2628

2729
// DefaultSettings returns the default configuration values
@@ -83,15 +85,21 @@ func InitializeViper(cmd *cobra.Command) error {
8385
return nil
8486
}
8587

86-
// LoadConfigFile reads and merges the config file into Viper
87-
func LoadConfigFile(configFile string) error {
88-
if configFile == "" {
89-
return fmt.Errorf("config file path is required")
88+
// LoadSettings reads and merges the config file into Viper
89+
func LoadSettings(settings *Settings) error {
90+
if settings == nil {
91+
return fmt.Errorf("config settings are required")
9092
}
9193

92-
viper.SetConfigFile(configFile)
93-
if err := viper.ReadInConfig(); err != nil {
94-
return fmt.Errorf("failed to read config file %s: %w", configFile, err)
94+
// settings converted to JSON bytes
95+
settingsJSON, err := json.Marshal(settings)
96+
if err != nil {
97+
return fmt.Errorf("failed to marshal settings: %w", err)
98+
}
99+
100+
viper.SetConfigType("json")
101+
if err := viper.ReadConfig(bytes.NewBuffer(settingsJSON)); err != nil {
102+
return fmt.Errorf("failed to read config: %w", err)
95103
}
96104

97105
return nil

config/settings_test.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package config
22

33
import (
4-
"os"
5-
"path/filepath"
4+
"encoding/json"
65
"testing"
76
"time"
87

@@ -71,8 +70,10 @@ func TestArgumentPrecedence(t *testing.T) {
7170
// Reset viper for each test
7271
viper.Reset()
7372

74-
// Create temporary config file
75-
configFile := createTempConfigFile(t, tt.configContent)
73+
// create Settings struct
74+
configSettings := &Settings{}
75+
err := json.Unmarshal([]byte(tt.configContent), configSettings)
76+
require.NoError(t, err, "Failed to unmarshal config file")
7677

7778
// Create test command with flags
7879
cmd := &cobra.Command{
@@ -102,8 +103,8 @@ func TestArgumentPrecedence(t *testing.T) {
102103
// Initialize Viper
103104
require.NoError(t, InitializeViper(cmd), "Failed to initialize Viper")
104105

105-
// Load config file
106-
require.NoError(t, LoadConfigFile(configFile), "Failed to load config file")
106+
// Load settings
107+
require.NoError(t, LoadSettings(configSettings), "Failed to load settings")
107108

108109
// Resolve settings
109110
settings := ResolveSettings()
@@ -138,12 +139,3 @@ func TestDefaultSettings(t *testing.T) {
138139
t.Errorf("DefaultSettings mismatch.\nExpected: %+v\nGot: %+v", expected, defaults)
139140
}
140141
}
141-
142-
// Helper function to create temporary config files for testing
143-
func createTempConfigFile(t *testing.T, content string) string {
144-
t.Helper()
145-
destination := filepath.Join(t.TempDir(), "test-config.json")
146-
err := os.WriteFile(destination, []byte(content), 0644)
147-
require.NoError(t, err, "Failed to create temp config file: %v", err)
148-
return destination
149-
}

main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ func main() {
8787
}
8888

8989
func runLoadTest(ctx context.Context, cmd *cobra.Command, args []string) error {
90-
// Load config file into Viper
91-
if err := config.LoadConfigFile(configFile); err != nil {
92-
return fmt.Errorf("failed to load config file: %w", err)
93-
}
94-
9590
// Parse the config file into a config.LoadConfig struct
9691
cfg, err := loadConfig(configFile)
9792
if err != nil {
9893
return fmt.Errorf("failed to load config: %w", err)
9994
}
10095

96+
// Load settings into Viper
97+
if err := config.LoadSettings(cfg.Settings); err != nil {
98+
return fmt.Errorf("failed to load config file: %w", err)
99+
}
100+
101101
// Get resolved settings from the config package
102102
settings := config.ResolveSettings()
103103

profiles/autobake_evm_transfer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"trackBlocks": false,
2626
"trackUserLatency": false,
2727
"prewarm": false,
28+
"rampUp": false,
2829
"reportPath": "/home/ubuntu/evm_transfer_report.txt"
2930
}
3031
}

0 commit comments

Comments
 (0)