feat: initial release
This commit is contained in:
parent
a3cf21f5bd
commit
1e0ee5bffe
40 changed files with 2007 additions and 217 deletions
85
internal/config/node.go
Normal file
85
internal/config/node.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue