nurse/config/app_config.go
Peter Kurfer 9791e9f282
All checks were successful
Renovate / renovate (push) Successful in 40s
Go build / build (push) Successful in 8m44s
feat: refactor to server and exec-check subcommands
- allow to interactively execute checks instead of server mode
- use urfave/cli for subcommands
2023-12-04 11:22:49 +01:00

107 lines
1.9 KiB
Go

package config
import (
"io"
"os"
"time"
)
type (
Option interface {
Extend(n *Nurse) error
}
OptionFunc func(n *Nurse) error
)
func (f OptionFunc) Extend(n *Nurse) error {
return f(n)
}
func New(opts ...Option) (*Nurse, error) {
var (
inst = new(Nurse)
err error
)
for i := range opts {
if err = opts[i].Extend(inst); err != nil {
return nil, err
}
}
return inst, nil
}
type Nurse struct {
Servers map[string]Server
Endpoints map[Route]EndpointSpec
CheckTimeout time.Duration
CheckAttempts uint
Insecure bool
}
func (n *Nurse) ServerLookup() (*ServerRegister, error) {
register := NewServerRegister()
for name, srv := range n.Servers {
if err := register.Register(name, srv); err != nil {
return nil, err
}
}
return register, nil
}
// Merge merges the current Nurse instance with another one
// giving the current instance precedence means no set value is overwritten
func (n *Nurse) Merge(other Nurse) {
if n.CheckTimeout == 0 {
n.CheckTimeout = other.CheckTimeout
}
if n.CheckAttempts == 0 {
n.CheckAttempts = other.CheckAttempts
}
for name, srv := range other.Servers {
if _, ok := n.Servers[name]; !ok {
n.Servers[name] = srv
}
}
}
func (n *Nurse) Unmarshal(reader io.ReadSeeker) error {
providers := []func(io.Reader) configDecoder{
newYAMLDecoder,
newJSONDecoder,
}
for i := range providers {
if err := n.tryUnmarshal(reader, providers[i]); err == nil {
return nil
}
}
return nil
}
func (n *Nurse) ReadFromFile(configFilePath string) error {
if file, err := os.Open(configFilePath); err != nil {
return err
} else {
defer func() {
_ = file.Close()
}()
return n.Unmarshal(file)
}
}
func (n *Nurse) tryUnmarshal(seeker io.ReadSeeker, prov func(reader io.Reader) configDecoder) error {
if _, err := seeker.Seek(0, 0); err != nil {
return err
}
decoder := prov(seeker)
return decoder.DecodeConfig(n)
}