emissary/internal/openwrt/uci/uci.go

76 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-02-02 10:55:24 +01:00
package uci
import (
"strings"
)
type UCI struct {
Packages []*Package `parser:"@@*" json:"packages"`
}
type Package struct {
Name string `parser:"'package' @Ident" json:"name"`
Configs []*Config `parser:"@@*" json:"configs"`
}
type Config struct {
Name string `parser:"'config' @Ident" json:"name"`
Section *string `parser:"@String?" json:"section,omitempty"`
Options []Option `parser:"@@*" json:"options"`
}
type Option struct {
Type string `parser:"@( 'list' | 'option' )" json:"type"`
Name string `parser:"@Ident" json:"name"`
Value string `parser:"@String" json:"value"`
}
func (u *UCI) Export() string {
var sb strings.Builder
for pkgIdx, pkg := range u.Packages {
if pkgIdx > 0 {
sb.WriteString("\n")
}
sb.WriteString("package ")
sb.WriteString(pkg.Name)
sb.WriteString("\n")
for _, cfg := range pkg.Configs {
sb.WriteString("\n")
sb.WriteString("config ")
sb.WriteString(cfg.Name)
if cfg.Section != nil {
sb.WriteString(" '")
sb.WriteString(*cfg.Section)
sb.WriteString("'")
}
for _, opt := range cfg.Options {
sb.WriteString("\n")
sb.WriteString("\t")
sb.WriteString(opt.Type)
sb.WriteString(" ")
sb.WriteString(opt.Name)
sb.WriteString(" '")
sb.WriteString(opt.Value)
sb.WriteString("'")
}
sb.WriteString("\n")
}
}
return sb.String()
}
func NewUCI() *UCI {
return &UCI{
Packages: make([]*Package, 0),
}
}