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
|
package main
import (
"context"
"fmt"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"go.uber.org/zap"
)
type configKey string
var key = configKey("mageconfigkey")
func GetConfig(ctx context.Context) (config Config, err error) {
config, ok := ctx.Value(key).(Config)
if !ok {
return config, errors.Errorf("config not found in mage context")
}
return config, nil
}
func WithConfig(ctx context.Context, args ...string) context.Context {
var config Config
err := envconfig.Process("", &config)
if err != nil {
zap.S().Fatalf("unable to parse environment config: %v", err)
}
godotenv.Load(fmt.Sprintf("%s.env", config.Env))
err = envconfig.Process("", &config)
if err != nil {
zap.S().Fatalf("unable to parse environment config: %v", err)
}
config.Args = args
return context.WithValue(ctx, key, config)
}
|