1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 17:10:10 +00:00
jfa-go/scripts/ini/main.go
Harvey Tindall f063b970b4
config: migrate to new yaml format
config-base.yaml is almost identical to json version, except there's no "order" field, as
"sections" and "settings" fields are now lists themselves and so Go can
parse the correct order. As such, removed enumerate_config.py. Also,
rewrote scripts/generate_ini.py in Go as scripts/ini/. Config structure
in Go form is now in common/config.go, and is used by jfa-go and the ini
script. app.configBase is now untouched once read from config-base.yaml,
and instead copied to and patched in app.patchedConfig. Patching occurs
at program start and config modification, so GetConfig is now just a
couple of lines. Discord role patching still occurs in GetConfig, as the
available roles can change regularly. Also added new "Disabled" field to
sections, to avoid the nightmare of deleting from an array.
2024-08-26 15:43:28 +01:00

131 lines
3.0 KiB
Go

package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/hrfee/jfa-go/common"
"gopkg.in/ini.v1"
"gopkg.in/yaml.v3"
)
func fixDescription(desc string) string {
return "; " + strings.ReplaceAll(desc, "\n", "\n; ")
}
func generateIni(yamlPath string, iniPath string) {
yamlFile, err := os.ReadFile(yamlPath)
if err != nil {
panic(err)
}
configBase := common.Config{}
err = yaml.Unmarshal(yamlFile, &configBase)
if err != nil {
panic(err)
}
conf := ini.Empty()
for _, section := range configBase.Sections {
cSection, err := conf.NewSection(section.Section)
if err != nil {
panic(err)
}
if section.Meta.Description != "" {
cSection.Comment = fixDescription(section.Meta.Description)
}
for _, setting := range section.Settings {
if setting.Type == common.NoteType {
continue
}
val := ""
if setting.Value != nil {
// Easy way to convert bools and numbers to strings,
// Instead of checking setting.Type
val = fmt.Sprintf("%v", setting.Value)
}
cKey, err := cSection.NewKey(setting.Setting, val)
if err != nil {
panic(err)
}
if setting.Description != "" {
cKey.Comment = fixDescription(setting.Description)
}
// Explain how to use list type
if setting.Type == common.ListType {
if cKey.Comment != "" {
cKey.Comment += "\n"
}
cKey.Comment += `List type: duplicate and edit the line to add more entries.`
}
}
}
err = conf.SaveTo(iniPath)
if err != nil {
panic(err)
}
}
// Compares two inis, used to check this script does the equivalent of the old generate_ini.py.
func compareInis(p1, p2 string) {
cA, err := ini.ShadowLoad(p1)
if err != nil {
panic(err)
}
cB, err := ini.ShadowLoad(p2)
if err != nil {
panic(err)
}
for _, pair := range [][2]*ini.File{{cA, cB}, {cB, cA}} {
s1 := pair[0].Sections()
s2 := pair[1].Sections()
for i := range s1 {
if s1[i].Name() != s2[i].Name() {
panic(fmt.Errorf("mismatching section order: s0[i]=%s, s1[i]=%s", s1[i].Name(), s2[i].Name()))
}
// fmt.Println("Section order matches")
st1 := s1[i].Keys()
st2 := s2[i].Keys()
for i := range st1 {
if st1[i].Name() != st2[i].Name() {
panic(fmt.Errorf("mismatching setting order: st1[i]=%s, st2[i]=%s", st1[i].Name(), st2[i].Name()))
}
if st1[i].Value() != st2[i].Value() {
panic(fmt.Errorf("mismatching setting values: st1[i]=%s, st2[i]=%s", st1[i].Value(), st2[i].Value()))
}
// fmt.Println("Setting matches")
}
}
}
}
func main() {
var yamlPath string
var iniPath string
var comparePath string
flag.StringVar(&yamlPath, "in", "", "Input of the config base in yaml.")
flag.StringVar(&iniPath, "out", "", "Output path of an ini file.")
flag.StringVar(&comparePath, "comp", "", "Path to ini file to compare against.")
flag.Parse()
if yamlPath == "" {
panic(errors.New("invalid yaml path"))
}
if iniPath == "" {
panic(errors.New("invalid ini path"))
}
generateIni(yamlPath, iniPath)
if comparePath != "" {
compareInis(iniPath, comparePath)
fmt.Println("Passed.")
}
}