moonmath/config/config.go

109 lines
2.3 KiB
Go
Raw Normal View History

2024-03-20 04:44:11 +00:00
package config
import (
2024-03-22 00:39:10 +00:00
_ "embed"
2024-03-20 04:44:11 +00:00
"slices"
"time"
"code.humancabbage.net/sam/moonmath/coindesk"
"code.humancabbage.net/sam/moonmath/moon"
2024-03-22 00:39:10 +00:00
"github.com/knadh/koanf/parsers/yaml"
2024-03-20 04:44:11 +00:00
"github.com/knadh/koanf/providers/file"
2024-03-22 00:39:10 +00:00
"github.com/knadh/koanf/providers/rawbytes"
2024-03-20 04:44:11 +00:00
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
)
var k = koanf.New(".")
2024-03-29 08:15:59 +00:00
func Load(filePath string) (r Root, err error) {
2024-03-20 04:44:11 +00:00
err = k.Load(structs.Provider(Default, "koanf"), nil)
if err != nil {
return
}
2024-03-29 08:15:59 +00:00
if filePath != "" {
err = k.Load(file.Provider(filePath), yaml.Parser())
2024-03-20 04:44:11 +00:00
if err != nil {
return
}
}
2024-03-29 08:15:59 +00:00
err = k.Unmarshal("", &r)
2024-03-20 04:44:11 +00:00
return
}
2024-03-29 08:15:59 +00:00
var Default Root
2024-03-22 00:39:10 +00:00
2024-03-29 08:15:59 +00:00
type Root struct {
Defaults Asset `koanf:"defaults"`
Assets map[coindesk.Asset]Asset `koanf:"assets"`
2024-03-20 04:44:11 +00:00
}
2024-03-29 08:15:59 +00:00
type Asset struct {
2024-03-20 04:44:11 +00:00
Asset coindesk.Asset `koanf:"asset"`
Goals []moon.Goal `koanf:"goals"`
ConstantBases []moon.ConstantBase `koanf:"constantBases"`
RelativeBases []moon.RelativeBase `koanf:"relativeBases"`
}
2024-03-29 08:15:59 +00:00
func (r Root) ForAsset(asset coindesk.Asset) (data Asset) {
data, ok := r.Assets[asset]
2024-03-22 00:39:10 +00:00
if !ok {
2024-03-29 08:15:59 +00:00
data = r.Defaults
2024-03-22 00:39:10 +00:00
}
if data.Asset == "" {
data.Asset = asset
}
if data.Goals == nil || len(data.Goals) == 0 {
2024-03-29 08:15:59 +00:00
data.Goals = r.Defaults.Goals
2024-03-22 00:39:10 +00:00
}
if data.ConstantBases == nil || len(data.ConstantBases) == 0 {
2024-03-29 08:15:59 +00:00
data.ConstantBases = r.Defaults.ConstantBases
2024-03-22 00:39:10 +00:00
}
if data.RelativeBases == nil || len(data.RelativeBases) == 0 {
2024-03-29 08:15:59 +00:00
data.RelativeBases = r.Defaults.RelativeBases
2024-03-22 00:39:10 +00:00
}
return
2024-03-20 04:44:11 +00:00
}
// GetBases returns the concatenation of the constant and relative bases, sorted
// from most recent to least recent in time.
2024-03-29 08:15:59 +00:00
func GetBases(d *Asset) (bases []moon.Base) {
2024-03-20 04:44:11 +00:00
for _, b := range d.ConstantBases {
bases = append(bases, b)
}
for _, b := range d.RelativeBases {
bases = append(bases, b)
}
now := time.Now()
slices.SortFunc(bases, func(a, b moon.Base) int {
aTime := a.From(now)
bTime := b.From(now)
aFirst := aTime.Before(bTime)
bFirst := bTime.Before(aTime)
switch {
case aFirst:
return 1
case bFirst:
return -1
}
return 0
})
return
}
2024-03-22 00:39:10 +00:00
//go:embed default.yaml
var defaultYamlBytes []byte
func init() {
var k = koanf.New(".")
err := k.Load(rawbytes.Provider(defaultYamlBytes), yaml.Parser())
if err != nil {
panic(err)
}
err = k.Unmarshal("", &Default)
if err != nil {
panic(err)
}
}