chore: use go generate to create Validate and Merge funcs on RoleConfigs
This commit is contained in:
parent
28b7993be4
commit
c9e93802eb
10 changed files with 236 additions and 200 deletions
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue