generated from sendnrw/template_golang
init
Some checks failed
build-binaries / build (, amd64, linux) (push) Has been skipped
build-binaries / build (, arm, 7, linux) (push) Has been skipped
build-binaries / build (, arm64, linux) (push) Has been skipped
build-binaries / build (.exe, amd64, windows) (push) Has been skipped
build-binaries / release (push) Has been skipped
build-binaries / publish-agent (push) Has been skipped
release-tag / release-image (push) Has been cancelled
Some checks failed
build-binaries / build (, amd64, linux) (push) Has been skipped
build-binaries / build (, arm, 7, linux) (push) Has been skipped
build-binaries / build (, arm64, linux) (push) Has been skipped
build-binaries / build (.exe, amd64, windows) (push) Has been skipped
build-binaries / release (push) Has been skipped
build-binaries / publish-agent (push) Has been skipped
release-tag / release-image (push) Has been cancelled
This commit is contained in:
66
config.go
Normal file
66
config.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ListenAddr string `yaml:"listen_addr"`
|
||||
Defaults TargetSettings `yaml:"defaults"`
|
||||
Targets []TargetConfig `yaml:"targets"`
|
||||
}
|
||||
|
||||
type TargetSettings struct {
|
||||
Interval time.Duration `yaml:"interval"`
|
||||
Timeout time.Duration `yaml:"timeout"`
|
||||
Size int `yaml:"size"`
|
||||
HistorySize int `yaml:"history_size"`
|
||||
DNSServer string `yaml:"dns_server"`
|
||||
DisableIPv6 bool `yaml:"disable_ipv6"`
|
||||
}
|
||||
|
||||
type TargetConfig struct {
|
||||
Name string `yaml:"name"`
|
||||
Host string `yaml:"host"`
|
||||
|
||||
// optional overrides
|
||||
Interval *time.Duration `yaml:"interval"`
|
||||
Timeout *time.Duration `yaml:"timeout"`
|
||||
Size *int `yaml:"size"`
|
||||
HistorySize *int `yaml:"history_size"`
|
||||
DNSServer *string `yaml:"dns_server"`
|
||||
DisableIPv6 *bool `yaml:"disable_ipv6"`
|
||||
}
|
||||
|
||||
func LoadConfig(path string) (*Config, error) {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(b, &cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// defaults setzen, falls nicht da
|
||||
if cfg.ListenAddr == "" {
|
||||
cfg.ListenAddr = ":9101"
|
||||
}
|
||||
if cfg.Defaults.Interval == 0 {
|
||||
cfg.Defaults.Interval = 5 * time.Second
|
||||
}
|
||||
if cfg.Defaults.Timeout == 0 {
|
||||
cfg.Defaults.Timeout = 1 * time.Second
|
||||
}
|
||||
if cfg.Defaults.Size == 0 {
|
||||
cfg.Defaults.Size = 56
|
||||
}
|
||||
if cfg.Defaults.HistorySize == 0 {
|
||||
cfg.Defaults.HistorySize = 10
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user