chore: pre-process toml

This commit is contained in:
andig 2023-06-09 17:41:06 +02:00
parent c0226637f8
commit 422e6c72f9

View file

@ -2,6 +2,7 @@ package main
import (
"bufio"
"bytes"
"fmt"
"io/fs"
"os"
@ -11,44 +12,80 @@ import (
"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)
}
func process(filepath string) error {
in, err := os.ReadFile(filepath)
if err != nil {
return 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()))
pre := new(strings.Builder)
{
sc := bufio.NewScanner(bytes.NewReader(in))
for sc.Scan() {
if strings.HasPrefix(sc.Text(), "[") && strings.Contains(sc.Text(), ".") && out.Len() > 0 {
fmt.Fprintln(out, "")
col := strings.SplitN(sc.Text(), "=", 2)
if len(col) < 2 {
fmt.Fprintln(pre, sc.Text())
continue
}
fmt.Fprintln(out, sc.Text())
}
if err := os.WriteFile(filepath, []byte(out.String()), 0644); err != nil {
return fmt.Errorf("%s: %v", filepath, err)
}
key := strings.TrimSpace(col[0])
val := strings.TrimSpace(col[1])
return nil
})
if strings.HasPrefix(val, "\"") {
fmt.Fprintln(pre, sc.Text())
continue
}
fmt.Fprintf(pre, "%s = \"%s\"\n", key, val)
}
}
var config map[string]any
if _, err := toml.Decode(pre.String(), &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
}
func main() {
var err error
if len(os.Args) > 1 {
err = process(os.Args[1])
} else {
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
}
return process(filepath)
})
}
if err != nil {
panic(err)