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,20 +1,8 @@
package config
//go:generate -command roleconfig go run git.wzray.com/homelab/hivemind/internal/codegen/roleconfig
//go:generate roleconfig -name DnsConfig
type DnsConfig struct {
UseSystemd bool `toml:"use_systemd"`
UseSystemd bool `toml:"use_systemd" gen:"no"`
baseRoleConfig
}
func (c DnsConfig) Validate() error {
return nil
}
func (c *DnsConfig) Merge(other DnsConfig) {
if other.set {
c.set = other.set
}
if other.UseSystemd {
c.UseSystemd = other.UseSystemd
}
}

View file

@ -1,79 +1,13 @@
package config
import (
"errors"
"fmt"
"net"
)
//go:generate -command roleconfig go run git.wzray.com/homelab/hivemind/internal/codegen/roleconfig
//go:generate roleconfig -name HostConfig
type HostConfig struct {
Domain string `toml:"domain"`
IpAddress string `toml:"ip"`
IpAddress string `toml:"ip" gen:"ip"`
LocalAddress string `toml:"local_address"`
InternalEntrypoint string `toml:"internal_entrypoint"`
ExternalEntrypoint string `toml:"external_entrypoint"`
ListenAddress string `toml:"listen_address"`
baseRoleConfig
}
func (c HostConfig) Validate() error {
if c.Domain == "" {
return errors.New("missing domain")
}
if c.IpAddress == "" {
return errors.New("missing ip")
}
if net.ParseIP(c.IpAddress) == nil {
return fmt.Errorf("invalid ip: %q", c.IpAddress)
}
if c.LocalAddress == "" {
return errors.New("missing local_address")
}
if c.InternalEntrypoint == "" {
return errors.New("missing internal_entrypoint")
}
if c.ExternalEntrypoint == "" {
return errors.New("missing external_entrypoint")
}
if c.ListenAddress == "" {
return errors.New("missing listen_address")
}
return nil
}
func (c *HostConfig) Merge(other HostConfig) {
if other.set {
c.set = other.set
}
if other.Domain != "" {
c.Domain = other.Domain
}
if other.IpAddress != "" {
c.IpAddress = other.IpAddress
}
if other.LocalAddress != "" {
c.LocalAddress = other.LocalAddress
}
if other.InternalEntrypoint != "" {
c.InternalEntrypoint = other.InternalEntrypoint
}
if other.ExternalEntrypoint != "" {
c.ExternalEntrypoint = other.ExternalEntrypoint
}
if other.ListenAddress != "" {
c.ListenAddress = other.ListenAddress
}
}

View file

@ -1,54 +1,12 @@
package config
import "errors"
//go:generate -command roleconfig go run git.wzray.com/homelab/hivemind/internal/codegen/roleconfig
//go:generate roleconfig -name MasterConfig
type MasterConfig struct {
ObserverInterval int `toml:"observer_interval"`
BackoffSeconds int `toml:"backoff_seconds"`
BackoffCount int `toml:"backoff_count"`
NodeTimeout int `toml:"node_timeout"`
ObserverInterval int `toml:"observer_interval" gen:"positive"`
BackoffSeconds int `toml:"backoff_seconds" gen:"positive"`
BackoffCount int `toml:"backoff_count" gen:"positive"`
NodeTimeout int `toml:"node_timeout" gen:"positive"`
baseRoleConfig
}
func (c MasterConfig) Validate() error {
if c.ObserverInterval < 1 {
return errors.New("invalid observer_interval")
}
if c.BackoffSeconds < 1 {
return errors.New("invalid backoff_seconds")
}
if c.BackoffCount < 1 {
return errors.New("invalid backoff_count")
}
if c.NodeTimeout < 1 {
return errors.New("invalid node_timeout")
}
return nil
}
func (c *MasterConfig) Merge(other MasterConfig) {
if other.set {
c.set = true
}
if other.ObserverInterval != 0 {
c.ObserverInterval = other.ObserverInterval
}
if other.BackoffSeconds != 0 {
c.BackoffSeconds = other.BackoffSeconds
}
if other.BackoffCount != 0 {
c.BackoffCount = other.BackoffCount
}
if other.NodeTimeout != 0 {
c.NodeTimeout = other.NodeTimeout
}
}

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"`
}

View file

@ -1,5 +1,5 @@
package config
type baseRoleConfig struct {
set bool
set bool `gen:"no"`
}