diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2025-10-07 20:29:09 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2025-10-07 20:29:09 -0700 |
| commit | 40e6f248d4e302c8541d72778de64b5bdde2b69c (patch) | |
| tree | 7187d86f922af555c765059b36eacfa4e874213a /internal/ini/ini.go | |
| parent | Log (diff) | |
| download | roseh.moe-40e6f248d4e302c8541d72778de64b5bdde2b69c.tar.zst | |
Use ini for the secrets file as well
Diffstat (limited to 'internal/ini/ini.go')
| -rw-r--r-- | internal/ini/ini.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/internal/ini/ini.go b/internal/ini/ini.go new file mode 100644 index 0000000..c546a52 --- /dev/null +++ b/internal/ini/ini.go @@ -0,0 +1,51 @@ +package ini + +import ( + "bufio" + "errors" + "io" + "iter" + "strings" +) + +type Parser struct { + Section string + LineNumber int +} + +type KeyVal struct { + Key, Val string +} + +func (p *Parser) Parse(r io.Reader) iter.Seq2[KeyVal, error] { + return func(yield func(KeyVal, error) bool) { + *p = Parser{} + scanner := bufio.NewScanner(r) + for scanner.Scan() { + p.LineNumber++ + line := strings.TrimSpace(scanner.Text()) + if len(line) == 0 || strings.HasPrefix(line, "//") || strings.HasPrefix(line, ";") || strings.HasPrefix(line, "#") { + continue + } + if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { + p.Section = strings.TrimSpace(line[1 : len(line)-1]) + continue + } + key, val, ok := strings.Cut(line, "=") + if !ok { + if !yield(KeyVal{}, errors.New("invalid key-value pair")) { + return + } + continue + } + key, val = strings.TrimSpace(key), strings.TrimSpace(val) + if !yield(KeyVal{key, val}, nil) { + return + } + } + if err := scanner.Err(); err != nil { + yield(KeyVal{}, err) + return + } + } +} |
