1
0
Fork 0
hivemind/internal/config/node.go
2026-01-18 19:38:54 +03:00

85 lines
1.6 KiB
Go

package config
import (
"errors"
"fmt"
"strings"
)
type LogLevel string
const (
LogLevelDebug LogLevel = "DEBUG"
LogLevelInfo LogLevel = "INFO"
LogLevelWarn LogLevel = "WARN"
LogLevelError LogLevel = "ERROR"
)
func (l *LogLevel) UnmarshalText(data []byte) error {
raw := strings.ToUpper(string(data))
switch LogLevel(raw) {
case LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError:
*l = LogLevel(raw)
return nil
default:
return fmt.Errorf("invalid log level: %q", data)
}
}
type NodeConfig struct {
Hostname string `toml:"hostname"`
Endpoint string `toml:"endpoint"`
KeepaliveInterval int `toml:"keepalive_interval"`
LogLevel LogLevel `toml:"log_level"`
BootstrapMaster string `toml:"bootstrap_master"`
ListenOn string `toml:"listen_on"`
Port int `toml:"port"`
}
func (c NodeConfig) Validate() error {
if c.Hostname == "" {
return errors.New("missing hostname")
}
if c.Endpoint == "" {
return errors.New("missing endpoint")
}
if c.KeepaliveInterval < 1 && c.KeepaliveInterval != -1 {
return errors.New("invalid keepalive_interval")
}
return nil
}
func (c *NodeConfig) Merge(other NodeConfig) {
if other.Hostname != "" {
c.Hostname = other.Hostname
}
if other.Endpoint != "" {
c.Endpoint = other.Endpoint
}
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
}
}