Browse Source

reflective [WIP]

Signed-off-by: Alejandro Mery <amery@jpi.io>
dev-amery-reflective
Alejandro Mery 1 year ago
parent
commit
4ab2c9d068
  1. 57
      pkg/ceph/config_parser.go
  2. 98
      pkg/wireguard/config_parser.go

57
pkg/ceph/config_parser.go

@ -2,10 +2,9 @@ package ceph
import (
"io/fs"
"net/netip"
"log"
"asciigoat.org/ini/basic"
"asciigoat.org/ini/parser"
"darvaza.org/core"
)
@ -36,61 +35,27 @@ func loadGlobalConfSection(out *Config, src *basic.Section) error {
return nil
}
// revive:disable:cyclomatic
// revive:disable:cognitive-complexity
func loadGlobalConfField(cfg *GlobalConfig, field basic.Field) error {
// revive:enable:cyclomatic
// revive:enable:cognitive-complexity
log.Printf("%s[%q] = %q", "global", field.Key, field.Value)
// TODO: refactor when asciigoat's ini parser learns to do reflection
key, value := field.Key, field.Value
switch field.Key {
switch key {
case "fsid":
if !core.IsZero(cfg.FSID) {
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
}
err := cfg.FSID.UnmarshalText([]byte(field.Value))
switch {
case err != nil:
return core.Wrap(err, field.Key)
default:
return nil
}
return configFieldHandler(&cfg.FSID, key, value)
case "mon_host":
entries, _ := parser.SplitCommaArray(field.Value)
for _, s := range entries {
var addr netip.Addr
if err := addr.UnmarshalText([]byte(s)); err != nil {
return core.Wrap(err, field.Key)
}
cfg.MonitorsAddr = append(cfg.MonitorsAddr, addr)
}
return nil
return configFieldHandler(&cfg.MonitorsAddr, key, value)
case "mon_initial_members":
entries, _ := parser.SplitCommaArray(field.Value)
cfg.Monitors = append(cfg.Monitors, entries...)
return nil
return configFieldHandler(&cfg.Monitors, key, value)
case "cluster_network":
if !core.IsZero(cfg.ClusterNetwork) {
err := core.Wrap(fs.ErrInvalid, "fields before the first section")
return err
}
err := cfg.ClusterNetwork.UnmarshalText([]byte(field.Value))
switch {
case err != nil:
return core.Wrap(err, field.Key)
return configFieldHandler(&cfg.ClusterNetwork, key, value)
default:
return nil
}
return core.Wrap(fs.ErrNotExist, "field %q unknown", key)
}
return nil
}
func configFieldHandler(vi any, key, value string) error
func newConfigFromDocument(doc *basic.Document) (*Config, error) {
var out Config

98
pkg/wireguard/config_parser.go

@ -2,7 +2,7 @@ package wireguard
import (
"io/fs"
"strconv"
"log"
"asciigoat.org/ini/basic"
"darvaza.org/core"
@ -50,104 +50,40 @@ func loadPeerConfSection(out *Config, src *basic.Section) error {
return nil
}
// revive:disable:cyclomatic
// revive:disable:cognitive-complexity
func configFieldHandler(vi any, key, value string) error
func loadInterfaceConfField(cfg *InterfaceConfig, field basic.Field) error {
// revive:enable:cyclomatic
// revive:enable:cognitive-complexity
log.Printf("%s[%q] = %q", "Interface", field.Key, field.Value)
// TODO: refactor when asciigoat's ini parser learns to do reflection
switch field.Key {
case "Address":
if !core.IsZero(cfg.Address) {
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
}
key, value := field.Key, field.Value
err := cfg.Address.UnmarshalText([]byte(field.Value))
switch {
case err != nil:
return core.Wrap(err, field.Key)
default:
return nil
}
switch key {
case "Address":
return configFieldHandler(&cfg.Address, key, value)
case "PrivateKey":
if !core.IsZero(cfg.PrivateKey) {
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
}
err := cfg.PrivateKey.UnmarshalText([]byte(field.Value))
switch {
case err != nil:
return core.Wrap(err, field.Key)
default:
return nil
}
return configFieldHandler(&cfg.PrivateKey, key, value)
case "ListenPort":
if cfg.ListenPort > 0 {
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
}
u64, err := strconv.ParseUint(field.Value, 10, 16)
switch {
case err != nil:
return core.Wrap(err, field.Key)
case u64 == 0:
return core.Wrap(fs.ErrInvalid, "invalid %q value", field.Key)
default:
cfg.ListenPort = uint16(u64)
return nil
}
return configFieldHandler(&cfg.ListenPort, key, value)
default:
return core.Wrap(fs.ErrInvalid, "unknown field %q", field.Key)
return core.Wrap(fs.ErrInvalid, "unknown field %q", key)
}
}
// revive:disable:cyclomatic
// revive:disable:cognitive-complexity
func loadPeerConfField(cfg *PeerConfig, field basic.Field) error {
// revive:enable:cyclomatic
// revive:enable:cognitive-complexity
log.Printf("%s[%q] = %q", "Peer", field.Key, field.Value)
switch field.Key {
case "PublicKey":
if !core.IsZero(cfg.PublicKey) {
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
}
key, value := field.Key, field.Value
err := cfg.PublicKey.UnmarshalText([]byte(field.Value))
switch {
case err != nil:
return core.Wrap(err, field.Key)
default:
return nil
}
switch key {
case "PublicKey":
return configFieldHandler(&cfg.PublicKey, key, value)
case "Endpoint":
if cfg.Endpoint.String() != "" {
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
}
err := cfg.Endpoint.UnmarshalText([]byte(field.Value))
switch {
case err != nil:
return core.Wrap(err, field.Key)
default:
return nil
}
return configFieldHandler(&cfg.Endpoint, key, value)
case "AllowedIPs":
s, err := parseAllowedIPs(field.Value)
switch {
case err != nil:
return core.Wrap(err, field.Key)
case len(s) > 0:
cfg.AllowedIPs = append(cfg.AllowedIPs, s...)
return nil
}
return configFieldHandler(&cfg.AllowedIPs, key, value)
default:
return core.Wrap(fs.ErrInvalid, "unknown field %q", field.Key)
}
return nil
}
func newConfigFromDocument(doc *basic.Document) (*Config, error) {

Loading…
Cancel
Save