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,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
}
}