@@ -8,14 +8,14 @@ import (
8
8
)
9
9
10
10
// Utility function to clean the string by trimming spaces and removing ^M characters
11
- func cleanString (input string ) string {
11
+ func CleanString (input string ) string {
12
12
return strings .TrimSpace (strings .ReplaceAll (input , "\r " , "" ))
13
13
}
14
14
15
15
// UpdateTomlValue updates a TOML file based on the provided key and value.
16
16
// The key can be a field in a section (e.g., "api.enable") or a top-level field (e.g., "minimum-gas-prices").
17
17
func UpdateTomlValue (filePath , key , value string ) error {
18
- value = cleanString (value )
18
+ value = CleanString (value )
19
19
// Open the TOML file for reading
20
20
file , err := os .Open (filePath )
21
21
if err != nil {
@@ -83,15 +83,27 @@ func getSectionName(header string) string {
83
83
return strings .Trim (header , "[]" )
84
84
}
85
85
86
- // shouldModifyField checks if the current line should be modified.
86
+ // shouldModifyField checks if the current line should be modified by splitting and trimming the key and value .
87
87
func shouldModifyField (inTargetSection bool , currentSection , field , line string ) bool {
88
- // If there is no section (top-level), and the line starts with the field, modify it
89
- if currentSection == "" && strings .HasPrefix (strings .TrimSpace (line ), field ) {
90
- return true
88
+ trimmedLine := strings .TrimSpace (line )
89
+
90
+ // Check if the line contains the '=' delimiter
91
+ if ! strings .Contains (trimmedLine , "=" ) {
92
+ // No '=' found, so we don't need to modify this line
93
+ return false
94
+ }
95
+
96
+ // Split the line by '=' into key and value pair
97
+ parts := strings .SplitN (trimmedLine , "=" , 2 )
98
+ key := strings .TrimSpace (parts [0 ])
99
+
100
+ // Check if the key matches the target field
101
+ if key != field {
102
+ return false
91
103
}
92
104
93
- // If we are in the target section and the line starts with the field, modify it
94
- if inTargetSection && strings . HasPrefix ( strings . TrimSpace ( line ), field ) {
105
+ // If we are at the top-level or in the target section, return true
106
+ if currentSection == "" || inTargetSection {
95
107
return true
96
108
}
97
109
0 commit comments