gitstack

grindlemire/gothem-stack code browser

1.0 KB Go 44 lines 2024-07-19 · 7d26781 raw
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")

// GetConfig retrieves the mage config from the context
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
}

// WithConfig adds the mage config to the context
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))

	// we have to process the env config again to get the env vars loaded from the env file
	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)
}