91 lines
1.7 KiB
Go
91 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"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"`
|
|
Address string `toml:"address"`
|
|
Port int `toml:"port"`
|
|
|
|
KeepaliveInterval int `toml:"keepalive_interval"`
|
|
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 && c.KeepaliveInterval != -1 {
|
|
return errors.New("invalid keepalive_interval")
|
|
}
|
|
|
|
if net.ParseIP(c.ListenOn) == nil {
|
|
return errors.New("invalid listen_on")
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|