-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.go
56 lines (47 loc) · 1.04 KB
/
configuration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"errors"
"time"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"go.uber.org/fx"
"go.uber.org/zap"
)
type Configuration struct {
Address string
Scripts []string
Claims map[string]interface{}
ValidFor time.Duration
}
func provideConfiguration() fx.Option {
return fx.Options(
fx.Provide(
func(l *zap.Logger) (v *viper.Viper, err error) {
v = viper.New()
v.SetConfigName("claimy")
v.AddConfigPath(".")
v.AddConfigPath("$HOME/.claimy")
v.AddConfigPath("/etc/claimy")
err = v.ReadInConfig()
var notFoundErr viper.ConfigFileNotFoundError
switch {
case err == nil:
l.Info("configuration file found", zap.String("file", v.ConfigFileUsed()))
case errors.As(err, ¬FoundErr):
err = nil // ignore
l.Info("no configuration file")
}
return
},
func(v *viper.Viper) (cfg Configuration, err error) {
err = v.Unmarshal(
&cfg,
func(dco *mapstructure.DecoderConfig) {
dco.ErrorUnused = true
},
)
return
},
),
)
}