1
0
Fork 0

chore: use go generate to create Validate and Merge funcs on RoleConfigs

This commit is contained in:
Arthur K. 2026-02-01 13:14:34 +03:00
parent 28b7993be4
commit c9e93802eb
Signed by: wzray
GPG key ID: B97F30FDC4636357
10 changed files with 236 additions and 200 deletions

View file

@ -1,9 +1,7 @@
package config
import (
"errors"
"fmt"
"net"
"strings"
)
@ -28,68 +26,16 @@ func (l *LogLevel) UnmarshalText(data []byte) error {
}
}
//go:generate -command roleconfig go run git.wzray.com/homelab/hivemind/internal/codegen/roleconfig
//go:generate roleconfig -name NodeConfig
type NodeConfig struct {
Hostname string `toml:"hostname"`
Address string `toml:"address"`
Port int `toml:"port"`
KeepaliveInterval int `toml:"keepalive_interval"`
KeepaliveInterval int `toml:"keepalive_interval" gen:"positive"`
LogLevel LogLevel `toml:"log_level"`
BootstrapMaster string `toml:"bootstrap_master"`
ListenOn string `toml:"listen_on"`
}
func (c NodeConfig) Validate() error {
if c.Address == "" {
return errors.New("missing address")
}
if c.Hostname == "" {
return errors.New("missing hostname")
}
if c.KeepaliveInterval < 1 {
return errors.New("invalid keepalive_interval")
}
if c.ListenOn == "" {
return errors.New("missing listen_on")
}
if net.ParseIP(c.ListenOn) == nil {
return fmt.Errorf("invalid listen_on: %v", c.ListenOn)
}
return nil
}
func (c *NodeConfig) Merge(other NodeConfig) {
if other.Hostname != "" {
c.Hostname = other.Hostname
}
if other.Address != "" {
c.Address = other.Address
}
if other.BootstrapMaster != "" {
c.BootstrapMaster = other.BootstrapMaster
}
if other.ListenOn != "" {
c.ListenOn = other.ListenOn
}
if other.Port != 0 {
c.Port = other.Port
}
if other.KeepaliveInterval != 0 {
c.KeepaliveInterval = other.KeepaliveInterval
}
if other.LogLevel != "" {
c.LogLevel = other.LogLevel
}
BootstrapMaster string `toml:"bootstrap_master" gen:"no"`
ListenOn string `toml:"listen_on" gen:"ip"`
}