2022-05-08 09:00:22 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-12-04 10:22:49 +00:00
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
2022-05-08 09:00:22 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-12-04 10:22:49 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-05-08 09:00:22 +00:00
|
|
|
)
|
|
|
|
|
2023-12-04 10:22:49 +00:00
|
|
|
//nolint:ireturn // required for interface implementation
|
|
|
|
func WithCheckDuration(d time.Duration) Option {
|
|
|
|
return OptionFunc(func(n *Nurse) error {
|
|
|
|
n.CheckTimeout = d
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCheckAttempts(attempts uint) Option {
|
|
|
|
return OptionFunc(func(n *Nurse) error {
|
|
|
|
n.CheckAttempts = attempts
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:ireturn // required for interface implementation
|
|
|
|
func WithServersFromArgs(servers []string) Option {
|
|
|
|
return OptionFunc(func(n *Nurse) error {
|
|
|
|
if len(servers) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rawSrv := range servers {
|
|
|
|
name, rawURL, found := strings.Cut(rawSrv, "=")
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("couldn't parse %s as server, expected format: <server-name>=<url>", rawSrv)
|
|
|
|
}
|
|
|
|
|
|
|
|
var srv Server
|
|
|
|
if err := srv.UnmarshalURL(rawURL); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name = strings.ToLower(strings.TrimSpace(name))
|
|
|
|
|
|
|
|
n.Servers[name] = srv
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:40:32 +00:00
|
|
|
//nolint:ireturn // required for interface implementation
|
2022-05-08 09:00:22 +00:00
|
|
|
func WithServersFromEnv() Option {
|
2023-12-04 10:22:49 +00:00
|
|
|
return OptionFunc(func(n *Nurse) error {
|
2022-05-08 09:00:22 +00:00
|
|
|
envServers, err := ServersFromEnv()
|
|
|
|
if err != nil {
|
2023-12-04 10:22:49 +00:00
|
|
|
return err
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.Servers == nil || len(n.Servers) == 0 {
|
|
|
|
n.Servers = envServers
|
2023-12-04 10:22:49 +00:00
|
|
|
return nil
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, srv := range envServers {
|
|
|
|
if _, ok := n.Servers[name]; !ok {
|
|
|
|
n.Servers[name] = srv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 10:22:49 +00:00
|
|
|
return nil
|
2022-05-08 09:00:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithEndpointsFromEnv() Option {
|
2023-12-04 10:22:49 +00:00
|
|
|
return OptionFunc(func(n *Nurse) error {
|
2022-05-08 09:00:22 +00:00
|
|
|
envEndpoints, err := EndpointsFromEnv()
|
|
|
|
if err != nil {
|
2023-12-04 10:22:49 +00:00
|
|
|
return err
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.Endpoints == nil || len(n.Endpoints) == 0 {
|
|
|
|
n.Endpoints = envEndpoints
|
2023-12-04 10:22:49 +00:00
|
|
|
return nil
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for route, spec := range envEndpoints {
|
|
|
|
if _, ok := n.Endpoints[route]; !ok {
|
|
|
|
n.Endpoints[route] = spec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 10:22:49 +00:00
|
|
|
return nil
|
2022-05-08 09:00:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:40:32 +00:00
|
|
|
//nolint:ireturn // required to implement interface
|
2022-05-08 09:00:22 +00:00
|
|
|
func WithConfigFile(configFilePath string) Option {
|
2023-12-04 10:22:49 +00:00
|
|
|
return OptionFunc(func(n *Nurse) error {
|
2022-05-08 09:00:22 +00:00
|
|
|
var out Nurse
|
|
|
|
if configFilePath != "" {
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Debug("Attempt to load config file")
|
2022-05-08 09:00:22 +00:00
|
|
|
if err := out.ReadFromFile(configFilePath); err == nil {
|
2023-12-04 10:22:49 +00:00
|
|
|
return nil
|
2022-05-08 09:00:22 +00:00
|
|
|
} else {
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Warn(
|
2022-05-08 09:00:22 +00:00
|
|
|
"Failed to load config file",
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.String("config_file_path", configFilePath),
|
|
|
|
slog.String("err", err.Error()),
|
2022-05-08 09:00:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
|
|
configFilePath = filepath.Join(workingDir, "nurse.yaml")
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Debug("Attempt to load config file from current working directory")
|
2022-05-08 09:00:22 +00:00
|
|
|
if err = out.ReadFromFile(configFilePath); err == nil {
|
2023-12-04 10:22:49 +00:00
|
|
|
return nil
|
2022-05-08 09:00:22 +00:00
|
|
|
} else {
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Warn(
|
2022-05-08 09:00:22 +00:00
|
|
|
"Failed to load config file",
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.String("config_file_path", configFilePath),
|
|
|
|
slog.String("err", err.Error()),
|
2022-05-08 09:00:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if home, err := os.UserHomeDir(); err == nil {
|
|
|
|
configFilePath = filepath.Join(home, ".nurse.yaml")
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Debug("Attempt to load config file from user home directory")
|
2022-05-08 09:00:22 +00:00
|
|
|
if err = out.ReadFromFile(configFilePath); err == nil {
|
2023-12-04 10:22:49 +00:00
|
|
|
return nil
|
2022-05-08 09:00:22 +00:00
|
|
|
} else {
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Warn(
|
2022-05-08 09:00:22 +00:00
|
|
|
"Failed to load config file",
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.String("config_file_path", configFilePath),
|
|
|
|
slog.String("err", err.Error()),
|
2022-05-08 09:00:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
configFilePath = filepath.Join("", "etc", "nurse", "config.yaml")
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Debug("Attempt to load config file from global config directory")
|
2022-05-08 09:00:22 +00:00
|
|
|
if err := out.ReadFromFile(configFilePath); err == nil {
|
2023-12-04 10:22:49 +00:00
|
|
|
return nil
|
2022-05-08 09:00:22 +00:00
|
|
|
} else {
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Warn(
|
2022-05-08 09:00:22 +00:00
|
|
|
"Failed to load config file",
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.String("config_file_path", configFilePath),
|
|
|
|
slog.String("err", err.Error()),
|
2022-05-08 09:00:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-04 10:22:49 +00:00
|
|
|
return nil
|
2022-05-08 09:00:22 +00:00
|
|
|
})
|
|
|
|
}
|