Browse Source

reflective [WIP]

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

59
pkg/ceph/config_parser.go

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

98
pkg/wireguard/config_parser.go

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

Loading…
Cancel
Save