| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Lorenz Brun | 547b33f | 2020-04-23 15:27:06 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Lorenz Brun | 547b33f | 2020-04-23 15:27:06 +0200 | [diff] [blame] | 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "bufio" |
| 8 | "encoding/json" |
| 9 | "flag" |
| 10 | "fmt" |
| 11 | "io" |
| 12 | "os" |
| 13 | "strings" |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | inPath = flag.String("in", "", "Path to input Kconfig") |
| 18 | outPath = flag.String("out", "", "Path to output Kconfig") |
| 19 | ) |
| 20 | |
| 21 | func main() { |
| 22 | flag.Parse() |
| 23 | if *inPath == "" || *outPath == "" { |
| 24 | flag.PrintDefaults() |
| 25 | os.Exit(2) |
| 26 | } |
| 27 | inFile, err := os.Open(*inPath) |
| 28 | if err != nil { |
| 29 | fmt.Fprintf(os.Stderr, "Failed to open input Kconfig: %v\n", err) |
| 30 | os.Exit(1) |
| 31 | } |
| 32 | outFile, err := os.Create(*outPath) |
| 33 | if err != nil { |
| 34 | fmt.Fprintf(os.Stderr, "Failed to create output Kconfig: %v\n", err) |
| 35 | os.Exit(1) |
| 36 | } |
| 37 | var config struct { |
| 38 | Overrides map[string]string `json:"overrides"` |
| 39 | } |
| 40 | if err := json.Unmarshal([]byte(flag.Arg(0)), &config); err != nil { |
| 41 | fmt.Fprintf(os.Stderr, "Failed to parse overrides: %v\n", err) |
| 42 | os.Exit(1) |
| 43 | } |
| 44 | err = patchKconfig(inFile, outFile, config.Overrides) |
| 45 | if err != nil { |
| 46 | fmt.Fprintf(os.Stderr, "Failed to patch: %v\n", err) |
| 47 | os.Exit(1) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func patchKconfig(inFile io.Reader, outFile io.Writer, overrides map[string]string) error { |
| 52 | scanner := bufio.NewScanner(inFile) |
| 53 | for scanner.Scan() { |
| Tim Windelschmidt | 783d078 | 2024-04-17 17:38:32 +0200 | [diff] [blame] | 54 | if scanner.Err() != nil { |
| 55 | return scanner.Err() |
| 56 | } |
| Lorenz Brun | 547b33f | 2020-04-23 15:27:06 +0200 | [diff] [blame] | 57 | line := scanner.Text() |
| 58 | cleanLine := strings.TrimSpace(line) |
| 59 | if strings.HasPrefix(cleanLine, "#") || cleanLine == "" { |
| 60 | // Pass through comments and empty lines |
| 61 | fmt.Fprintln(outFile, line) |
| 62 | } else { |
| 63 | // Line contains a configuration option |
| 64 | parts := strings.SplitN(line, "=", 2) |
| 65 | keyName := parts[0] |
| 66 | if overrideVal, ok := overrides[strings.TrimSpace(keyName)]; ok { |
| 67 | // Override it |
| 68 | if overrideVal == "" { |
| 69 | fmt.Fprintf(outFile, "# %v is not set\n", keyName) |
| 70 | } else { |
| 71 | fmt.Fprintf(outFile, "%v=%v\n", keyName, overrideVal) |
| 72 | } |
| 73 | delete(overrides, keyName) |
| 74 | } else { |
| 75 | // Pass through unchanged |
| 76 | fmt.Fprintln(outFile, line) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | // Process left over overrides |
| 81 | for key, val := range overrides { |
| 82 | fmt.Fprintf(outFile, "%v=%v\n", key, val) |
| 83 | } |
| 84 | return nil |
| 85 | } |