Compare commits

...

3 Commits

Author SHA1 Message Date
Alejandro Mery 4ab2c9d068 reflective [WIP] 10 months ago
Alejandro Mery 098482a890 wireguard: ini.Unmarshal() [WIP] 10 months ago
Alejandro Mery 0a48a40796 build-sys: use local asciigoat.org/ini 10 months ago
  1. 2
      go.mod
  2. 2
      go.sum
  3. 57
      pkg/ceph/config_parser.go
  4. 2
      pkg/wireguard/config.go
  5. 98
      pkg/wireguard/config_parser.go

2
go.mod

@ -2,6 +2,8 @@ module git.jpi.io/amery/jpictl
go 1.19 go 1.19
replace asciigoat.org/ini => ../../../asciigoat.org/ini
require ( require (
asciigoat.org/ini v0.2.5 asciigoat.org/ini v0.2.5
darvaza.org/core v0.12.0 darvaza.org/core v0.12.0

2
go.sum

@ -1,7 +1,5 @@
asciigoat.org/core v0.3.9 h1:hgDDz4ecm3ZvehX++m8A/IzAt+B5oDPiRtxatzfUHPQ= asciigoat.org/core v0.3.9 h1:hgDDz4ecm3ZvehX++m8A/IzAt+B5oDPiRtxatzfUHPQ=
asciigoat.org/core v0.3.9/go.mod h1:CAaHwyw8MpAq4a1MYtN2dxJrsK+hmIdW50OndaQZYPI= asciigoat.org/core v0.3.9/go.mod h1:CAaHwyw8MpAq4a1MYtN2dxJrsK+hmIdW50OndaQZYPI=
asciigoat.org/ini v0.2.5 h1:4gRIp9rU+XQt8+HMqZO5R7GavMv9Yl2+N+je6djDIAE=
asciigoat.org/ini v0.2.5/go.mod h1:gmXzJ9XFqf1NLk5nQkj04USQ4tMtdRJHNQX6vp3DzjU=
darvaza.org/cache/x/simplelru v0.1.8 h1:rvFucut4wKYbsYc994yR3P0M08NqlsvZxr5G4QK82tw= darvaza.org/cache/x/simplelru v0.1.8 h1:rvFucut4wKYbsYc994yR3P0M08NqlsvZxr5G4QK82tw=
darvaza.org/cache/x/simplelru v0.1.8/go.mod h1:Mv1isOJTcXYK+aK0AvUe+/3KpRTXDsYga6rdTS/upNs= darvaza.org/cache/x/simplelru v0.1.8/go.mod h1:Mv1isOJTcXYK+aK0AvUe+/3KpRTXDsYga6rdTS/upNs=
darvaza.org/core v0.12.0 h1:LLtYh9RZJSd0sgPTDocofCc4H5jbutSUQgPbKt+8gcI= darvaza.org/core v0.12.0 h1:LLtYh9RZJSd0sgPTDocofCc4H5jbutSUQgPbKt+8gcI=

57
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")
return err
}
err := cfg.ClusterNetwork.UnmarshalText([]byte(field.Value))
switch {
case err != nil:
return core.Wrap(err, field.Key)
default: 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) { func newConfigFromDocument(doc *basic.Document) (*Config, error) {
var out Config var out Config

2
pkg/wireguard/config.go

@ -78,7 +78,7 @@ type PeerConfig struct {
Name string Name string
PublicKey PublicKey PublicKey PublicKey
Endpoint EndpointAddress Endpoint EndpointAddress
AllowedIPs []netip.Prefix AllowedIPs []netip.Prefix `ini:",comma"`
} }
// EndpointAddress is a host:port pair to reach the Peer // EndpointAddress is a host:port pair to reach the Peer

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