Skip to content

Commit

Permalink
fix: move env load settings to one place
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Oct 17, 2024
1 parent 04c22bd commit 2630826
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
11 changes: 0 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/getsentry/sentry-go"
"github.com/go-errors/errors"
"github.com/mitchellh/mapstructure"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -223,16 +222,6 @@ func recoverAndExit() {

func init() {
cobra.OnInitialize(func() {
// Allow overriding config object with automatic env
// Ref: https://github.com/spf13/viper/issues/761
envKeysMap := map[string]interface{}{}
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &envKeysMap,
IgnoreUntaggedFields: true,
})
cobra.CheckErr(err)
cobra.CheckErr(dec.Decode(utils.Config))
cobra.CheckErr(viper.MergeConfigMap(envKeysMap))
viper.SetEnvPrefix("SUPABASE")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
viper.AutomaticEnv()
Expand Down
31 changes: 27 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/go-errors/errors"
"github.com/golang-jwt/jwt/v5"
"github.com/joho/godotenv"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"golang.org/x/mod/semver"

Expand Down Expand Up @@ -136,7 +137,7 @@ type (
EdgeRuntime edgeRuntime `toml:"edge_runtime"`
Functions FunctionConfig `toml:"functions"`
Analytics analytics `toml:"analytics"`
Experimental experimental `toml:"experimental" mapstructure:"-"`
Experimental experimental `toml:"experimental"`
}

config struct {
Expand Down Expand Up @@ -587,6 +588,29 @@ func (c *config) Eject(w io.Writer) error {
return nil
}

func (c *config) loadFromEnv() error {
// Allow overriding base config object with automatic env
// Ref: https://github.com/spf13/viper/issues/761
envKeysMap := map[string]interface{}{}
if dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &envKeysMap,
IgnoreUntaggedFields: true,
}); err != nil {
return errors.Errorf("failed to create decoder: %w", err)
} else if err := dec.Decode(c.baseConfig); err != nil {
return errors.Errorf("failed to decode env: %w", err)
}
v := viper.New()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
if err := v.MergeConfigMap(envKeysMap); err != nil {
return errors.Errorf("failed to merge config: %w", err)
} else if err := v.Unmarshal(c); err != nil {
return errors.Errorf("failed to parse env to config: %w", err)
}
return nil
}

func (c *config) Load(path string, fsys fs.FS) error {
builder := NewPathBuilder(path)
// Load default values
Expand Down Expand Up @@ -614,9 +638,8 @@ func (c *config) Load(path string, fsys fs.FS) error {
// Load secrets from .env file
if err := loadDefaultEnv(); err != nil {
return err
}
if err := viper.Unmarshal(c); err != nil {
return errors.Errorf("failed to parse env to config: %w", err)
} else if err := c.loadFromEnv(); err != nil {
return err
}
// Generate JWT tokens
if len(c.Auth.AnonKey) == 0 {
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,15 @@ func TestLoadSeedPaths(t *testing.T) {
assert.Empty(t, config.SqlPaths)
})
}

func TestLoadEnv(t *testing.T) {
t.Setenv("AUTH_JWT_SECRET", "test-secret")
t.Setenv("DB_ROOT_KEY", "test-root-key")
config := NewConfig()
// Run test
err := config.loadFromEnv()
// Check error
assert.NoError(t, err)
assert.Equal(t, "test-secret", config.Auth.JwtSecret)
assert.Equal(t, "test-root-key", config.Db.RootKey)
}

0 comments on commit 2630826

Please sign in to comment.