44 lines
896 B
Go
44 lines
896 B
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
type HealthchecksConfig struct {
|
|
Enabled bool `json:"enabled"`
|
|
HostPingAPI string `json:"host_ping_api"`
|
|
PingKey string `json:"ping_key"`
|
|
}
|
|
|
|
type Config struct {
|
|
HealthchecksConfig `json:"healthchecks"`
|
|
LeasesPath string `json:"leases_path"`
|
|
}
|
|
|
|
func defaultConfig() *Config {
|
|
return &Config{
|
|
HealthchecksConfig: HealthchecksConfig{
|
|
Enabled: false,
|
|
},
|
|
LeasesPath: "/var/lib/misc/dnsmasq.leases",
|
|
}
|
|
}
|
|
|
|
func New(path string) (*Config, error) {
|
|
fileContent, err := os.ReadFile(path)
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return defaultConfig(), nil
|
|
}
|
|
return nil, fmt.Errorf("failed to read config file %w", err)
|
|
}
|
|
|
|
var c Config
|
|
if err := json.Unmarshal(fileContent, &c); err != nil {
|
|
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
|
}
|
|
return &c, nil
|
|
}
|