forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoml.go
56 lines (45 loc) · 1.06 KB
/
toml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"bufio"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
)
func main() {
err := filepath.WalkDir("./i18n", func(filepath string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("%s: %v", filepath, err)
}
if d.IsDir() {
return nil
}
var config map[string]any
if _, err := toml.DecodeFile(filepath, &config); err != nil {
return fmt.Errorf("%s: %v", filepath, err)
}
s := new(strings.Builder)
enc := toml.NewEncoder(s)
enc.Indent = ""
if err := enc.Encode(config); err != nil {
return fmt.Errorf("%s: %v", filepath, err)
}
out := new(strings.Builder)
sc := bufio.NewScanner(strings.NewReader(s.String()))
for sc.Scan() {
if strings.HasPrefix(sc.Text(), "[") && strings.Contains(sc.Text(), ".") && out.Len() > 0 {
fmt.Fprintln(out, "")
}
fmt.Fprintln(out, sc.Text())
}
if err := os.WriteFile(filepath, []byte(out.String()), 0644); err != nil {
return fmt.Errorf("%s: %v", filepath, err)
}
return nil
})
if err != nil {
panic(err)
}
}