mirror of
https://github.com/hrfee/jfa-go.git
synced 2026-01-18 16:47:42 +01:00
scripts: add "yaml" script
takes over the "Order" validation of scripts/ini, and also re-orders the "Sections" section according to "Order". Used instead of copying config-base.yaml into the executable's data.
This commit is contained in:
2
Makefile
2
Makefile
@@ -195,7 +195,7 @@ COPY_TARGET = $(DATA)/jfa-go.service
|
||||
# $(DATA)/LICENSE $(LANG_TARGET) $(STATIC_TARGET) $(DATA)/web/css/$(CSSVERSION)bundle.css
|
||||
$(COPY_TARGET): $(INLINE_TARGET) $(STATIC_SRC) $(LANG_SRC) $(CONFIG_BASE)
|
||||
$(info copying $(CONFIG_BASE))
|
||||
cp $(CONFIG_BASE) $(DATA)/
|
||||
go run scripts/yaml/main.go -in $(CONFIG_BASE) -out $(DATA)/$(shell basename $(CONFIG_BASE))
|
||||
$(info copying crash page)
|
||||
cp $(DATA)/crash.html $(DATA)/html/
|
||||
$(info copying static data)
|
||||
|
||||
18
scripts/yaml/go.mod
Normal file
18
scripts/yaml/go.mod
Normal file
@@ -0,0 +1,18 @@
|
||||
module github.com/hrfee/jfa-go/scripts/yaml
|
||||
|
||||
replace github.com/hrfee/jfa-go/common => ../../common
|
||||
|
||||
replace github.com/hrfee/jfa-go/logmessages => ../../logmessages
|
||||
|
||||
go 1.22.4
|
||||
|
||||
require (
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
github.com/goccy/go-yaml v1.18.0 // indirect
|
||||
github.com/hrfee/jfa-go/common v0.0.0-20251123201034-b1c578ccf49f // indirect
|
||||
github.com/hrfee/jfa-go/logmessages v0.0.0-20240806200606-6308db495a0a // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
golang.org/x/sys v0.25.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
16
scripts/yaml/go.sum
Normal file
16
scripts/yaml/go.sum
Normal file
@@ -0,0 +1,16 @@
|
||||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
|
||||
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
|
||||
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
121
scripts/yaml/main.go
Normal file
121
scripts/yaml/main.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/goccy/go-yaml"
|
||||
"github.com/hrfee/jfa-go/common"
|
||||
)
|
||||
|
||||
func flattenOrder(c common.Config) (sections []string) {
|
||||
var traverseGroup func(groupName string) []string
|
||||
traverseGroup = func(groupName string) []string {
|
||||
out := []string{}
|
||||
for _, group := range c.Groups {
|
||||
if group.Group == groupName {
|
||||
for _, groupMember := range group.Members {
|
||||
if groupMember.Group != "" {
|
||||
out = append(out, traverseGroup(groupMember.Group)...)
|
||||
} else if groupMember.Section != "" {
|
||||
out = append(out, groupMember.Section)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
sections = make([]string, 0, len(c.Sections))
|
||||
for _, member := range c.Order {
|
||||
if member.Group != "" {
|
||||
sections = append(sections, traverseGroup(member.Group)...)
|
||||
} else if member.Section != "" {
|
||||
sections = append(sections, member.Section)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func validateOrderCompleteness(c common.Config, sectOrder []string) (missing []string) {
|
||||
listedSects := map[string]bool{}
|
||||
for _, sect := range sectOrder {
|
||||
listedSects[sect] = true
|
||||
}
|
||||
|
||||
for _, section := range c.Sections {
|
||||
if _, ok := listedSects[section.Section]; !ok {
|
||||
missing = append(missing, section.Section)
|
||||
}
|
||||
}
|
||||
return missing
|
||||
}
|
||||
|
||||
func main() {
|
||||
var inPath string
|
||||
var outPath string
|
||||
flag.StringVar(&inPath, "in", "", "Input of the config base in yaml.")
|
||||
flag.StringVar(&outPath, "out", "", "Output of the checked and processed")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if inPath == "" {
|
||||
panic(errors.New("invalid input path"))
|
||||
}
|
||||
if outPath == "" {
|
||||
panic(errors.New("invalid output path"))
|
||||
}
|
||||
|
||||
yamlFile, err := os.ReadFile(inPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
info, err := os.Stat(inPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
configBase := common.Config{}
|
||||
err = yaml.Unmarshal(yamlFile, &configBase)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
red := color.New(color.FgRed)
|
||||
|
||||
if len(configBase.Order) > 0 {
|
||||
sectOrder := flattenOrder(configBase)
|
||||
missing := validateOrderCompleteness(configBase, sectOrder)
|
||||
if len(missing) > 0 {
|
||||
red.Fprintln(os.Stderr, "ERROR: Root order specified but the following sections were not listed, directly or indirectly:")
|
||||
for _, section := range missing {
|
||||
red.Fprintln(os.Stderr, "\t"+section)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
sectionMap := map[string]common.Section{}
|
||||
for _, sect := range configBase.Sections {
|
||||
sectionMap[sect.Section] = sect
|
||||
}
|
||||
|
||||
for i, sect := range sectOrder {
|
||||
configBase.Sections[i] = sectionMap[sect]
|
||||
}
|
||||
|
||||
fmt.Println("Re-ordered sections to follow root order.")
|
||||
}
|
||||
|
||||
bytes, err := yaml.Marshal(&configBase)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(outPath, bytes, info.Mode())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user