42 lines
705 B
Go
42 lines
705 B
Go
|
// Copyright (C) 2024 Umorpha Systems
|
||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||
|
|
||
|
package common
|
||
|
|
||
|
import (
|
||
|
"encoding"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"sigs.k8s.io/yaml"
|
||
|
)
|
||
|
|
||
|
func LoadYAML[T any](filename string) (T, error) {
|
||
|
bs, err := os.ReadFile(filename)
|
||
|
if err != nil {
|
||
|
var zero T
|
||
|
return zero, err
|
||
|
}
|
||
|
|
||
|
var ret T
|
||
|
if err := yaml.UnmarshalStrict(bs, &ret); err != nil {
|
||
|
var zero T
|
||
|
return zero, err
|
||
|
}
|
||
|
|
||
|
return ret, nil
|
||
|
}
|
||
|
|
||
|
type Duration struct {
|
||
|
time.Duration
|
||
|
}
|
||
|
|
||
|
var _ encoding.TextUnmarshaler = (*Duration)(nil)
|
||
|
|
||
|
// UnmarshalText implements [encoding.TextUnmarshaler].
|
||
|
func (d *Duration) UnmarshalText(text []byte) error {
|
||
|
var err error
|
||
|
d.Duration, err = time.ParseDuration(string(text))
|
||
|
return err
|
||
|
}
|