|
1 | 1 | package utils
|
2 | 2 |
|
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// Utility function to clean the string by trimming spaces and removing ^M characters |
| 11 | +func cleanString(input string) string { |
| 12 | + return strings.TrimSpace(strings.ReplaceAll(input, "\r", "")) |
| 13 | +} |
| 14 | + |
| 15 | +// UpdateTomlValue updates a TOML file based on the provided key and value. |
| 16 | +// The key can be a field in a section (e.g., "api.enable") or a top-level field (e.g., "minimum-gas-prices"). |
3 | 17 | func UpdateTomlValue(filePath, key, value string) error {
|
4 |
| - // TODO: Implement this function |
| 18 | + value = cleanString(value) |
| 19 | + // Open the TOML file for reading |
| 20 | + file, err := os.Open(filePath) |
| 21 | + if err != nil { |
| 22 | + return fmt.Errorf("error opening file: %w", err) |
| 23 | + } |
| 24 | + defer file.Close() |
| 25 | + |
| 26 | + // Determine if the key has a section (e.g., "api.enable") or is a top-level field (e.g., "minimum-gas-prices") |
| 27 | + var section, field string |
| 28 | + parts := strings.SplitN(key, ".", 2) |
| 29 | + if len(parts) == 2 { |
| 30 | + section = parts[0] // e.g., "api" |
| 31 | + field = parts[1] // e.g., "enable" |
| 32 | + } else { |
| 33 | + field = key // e.g., "minimum-gas-prices" |
| 34 | + } |
| 35 | + |
| 36 | + // Slice to store updated file lines |
| 37 | + var updatedLines []string |
| 38 | + var currentSection string |
| 39 | + inTargetSection := false |
| 40 | + |
| 41 | + // Read the file line by line |
| 42 | + scanner := bufio.NewScanner(file) |
| 43 | + for scanner.Scan() { |
| 44 | + line := scanner.Text() |
| 45 | + trimmedLine := strings.TrimSpace(line) |
| 46 | + |
| 47 | + // Check if the line is a section header (e.g., [api]) |
| 48 | + if isSectionHeader(trimmedLine) { |
| 49 | + currentSection = getSectionName(trimmedLine) |
| 50 | + inTargetSection = (currentSection == section) |
| 51 | + } |
| 52 | + |
| 53 | + // Modify the field if it's in the correct section or at the top-level |
| 54 | + if shouldModifyField(inTargetSection, currentSection, field, trimmedLine) { |
| 55 | + line = fmt.Sprintf(`%s = "%s"`, field, value) |
| 56 | + } |
| 57 | + |
| 58 | + // Add the line to the updated content |
| 59 | + updatedLines = append(updatedLines, line) |
| 60 | + } |
| 61 | + |
| 62 | + // Check for any scanner errors |
| 63 | + if err := scanner.Err(); err != nil { |
| 64 | + return fmt.Errorf("error reading file: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Write the modified lines back to the file |
| 68 | + err = os.WriteFile(filePath, []byte(strings.Join(updatedLines, "\n")), 0644) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("error writing to file: %w", err) |
| 71 | + } |
5 | 72 |
|
6 | 73 | return nil
|
7 | 74 | }
|
| 75 | + |
| 76 | +// isSectionHeader checks if a line is a section header (e.g., [api]). |
| 77 | +func isSectionHeader(line string) bool { |
| 78 | + return strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") |
| 79 | +} |
| 80 | + |
| 81 | +// getSectionName extracts the section name from a section header (e.g., [api] -> api). |
| 82 | +func getSectionName(header string) string { |
| 83 | + return strings.Trim(header, "[]") |
| 84 | +} |
| 85 | + |
| 86 | +// shouldModifyField checks if the current line should be modified. |
| 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 |
| 91 | + } |
| 92 | + |
| 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) { |
| 95 | + return true |
| 96 | + } |
| 97 | + |
| 98 | + return false |
| 99 | +} |
0 commit comments