This commit is contained in:
abs3nt 2024-02-17 21:55:06 -08:00
commit d411deaa02
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
4 changed files with 114 additions and 0 deletions

11
go.mod Normal file
View File

@ -0,0 +1,11 @@
module git.asdf.cafe/abs3nt/gunner
go 1.22.0
require (
github.com/cristalhq/aconfig v0.18.5
github.com/cristalhq/aconfig/aconfigdotenv v0.17.1
sigs.k8s.io/yaml v1.4.0
)
require github.com/joho/godotenv v1.4.0 // indirect

13
go.sum Normal file
View File

@ -0,0 +1,13 @@
github.com/cristalhq/aconfig v0.17.0/go.mod h1:NXaRp+1e6bkO4dJn+wZ71xyaihMDYPtCSvEhMTm/H3E=
github.com/cristalhq/aconfig v0.18.5 h1:QqXH/Gy2c4QUQJTV2BN8UAuL/rqZ3IwhvxeC8OgzquA=
github.com/cristalhq/aconfig v0.18.5/go.mod h1:NXaRp+1e6bkO4dJn+wZ71xyaihMDYPtCSvEhMTm/H3E=
github.com/cristalhq/aconfig/aconfigdotenv v0.17.1 h1:HG2ql5fGe4FLL2fUv6o+o0YRyF1mWEcYkNfWGWD82k4=
github.com/cristalhq/aconfig/aconfigdotenv v0.17.1/go.mod h1:gQIKkh+HkVcODvMNz/cLbH65Pk9b0r4tfolCOsI8G9I=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=

52
gun.go Normal file
View File

@ -0,0 +1,52 @@
package gun
import (
"os"
"path"
"github.com/cristalhq/aconfig"
"github.com/cristalhq/aconfig/aconfigdotenv"
"git.asdf.cafe/abs3nt/gunner/src/yaml"
)
func LoadApp(i any, appName string) {
yamlDecoder := yaml.New()
dotenvDecoder := aconfigdotenv.New()
configDir, err := os.UserConfigDir()
if err != nil {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
configDir = path.Join(homeDir, ".config")
}
filePath := path.Join(configDir, appName)
loader := aconfig.LoaderFor(i, aconfig.Config{
AllowUnknownFields: true,
AllowUnknownEnvs: true,
AllowUnknownFlags: true,
SkipFlags: true,
DontGenerateTags: true,
MergeFiles: true,
EnvPrefix: appName,
FlagPrefix: appName,
Files: []string{
path.Join(filePath, appName+".yml"),
path.Join(filePath, appName+".yaml"),
path.Join(filePath, appName+".json"),
path.Join(filePath, ".env"),
},
FileDecoders: map[string]aconfig.FileDecoder{
".yaml": yamlDecoder,
".yml": yamlDecoder,
".json": yamlDecoder,
".env": dotenvDecoder,
},
})
if err := loader.Load(); err != nil {
panic(err)
}
}

38
src/yaml/yaml.go Normal file
View File

@ -0,0 +1,38 @@
package yaml
import (
"io/fs"
"sigs.k8s.io/yaml"
)
// Decoder of YAML files for aconfig.
type Decoder struct {
fsys fs.FS
}
// New YAML decoder for aconfig.
func New() *Decoder { return &Decoder{} }
// Format of the decoder.
func (d *Decoder) Format() string {
return "yaml"
}
// DecodeFile implements aconfig.FileDecoder.
func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) {
b, err := fs.ReadFile(d.fsys, filename)
if err != nil {
return nil, err
}
var raw map[string]interface{}
if err := yaml.Unmarshal(b, &raw); err != nil {
return nil, err
}
return raw, nil
}
// DecodeFile implements aconfig.FileDecoder.
func (d *Decoder) Init(fsys fs.FS) {
d.fsys = fsys
}