Skip to content

Commit adda496

Browse files
authored
fix: squash base config with mapstructure (#2777)
* fix: squash base config with mapstructure * fix: move env load settings to one place * chore: remove unused string replacer
1 parent 9d30b96 commit adda496

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

cmd/root.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/getsentry/sentry-go"
1414
"github.com/go-errors/errors"
15-
"github.com/mitchellh/mapstructure"
1615
"github.com/spf13/afero"
1716
"github.com/spf13/cobra"
1817
"github.com/spf13/viper"
@@ -223,18 +222,8 @@ func recoverAndExit() {
223222

224223
func init() {
225224
cobra.OnInitialize(func() {
226-
// Allow overriding config object with automatic env
227-
// Ref: https://github.com/spf13/viper/issues/761
228-
envKeysMap := map[string]interface{}{}
229-
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
230-
Result: &envKeysMap,
231-
IgnoreUntaggedFields: true,
232-
})
233-
cobra.CheckErr(err)
234-
cobra.CheckErr(dec.Decode(utils.Config))
235-
cobra.CheckErr(viper.MergeConfigMap(envKeysMap))
236225
viper.SetEnvPrefix("SUPABASE")
237-
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
226+
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
238227
viper.AutomaticEnv()
239228
})
240229

pkg/config/config.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/go-errors/errors"
2929
"github.com/golang-jwt/jwt/v5"
3030
"github.com/joho/godotenv"
31+
"github.com/mitchellh/mapstructure"
3132
"github.com/spf13/viper"
3233
"golang.org/x/mod/semver"
3334

@@ -136,13 +137,13 @@ type (
136137
EdgeRuntime edgeRuntime `toml:"edge_runtime"`
137138
Functions FunctionConfig `toml:"functions"`
138139
Analytics analytics `toml:"analytics"`
139-
Experimental experimental `toml:"experimental" mapstructure:"-"`
140+
Experimental experimental `toml:"experimental"`
140141
}
141142

142143
config struct {
143-
baseConfig
144-
Overrides map[string]interface{} `toml:"remotes"`
145-
Remotes map[string]baseConfig `toml:"-"`
144+
baseConfig `mapstructure:",squash"`
145+
Overrides map[string]interface{} `toml:"remotes"`
146+
Remotes map[string]baseConfig `toml:"-"`
146147
}
147148

148149
db struct {
@@ -587,6 +588,29 @@ func (c *config) Eject(w io.Writer) error {
587588
return nil
588589
}
589590

591+
func (c *config) loadFromEnv() error {
592+
// Allow overriding base config object with automatic env
593+
// Ref: https://github.com/spf13/viper/issues/761
594+
envKeysMap := map[string]interface{}{}
595+
if dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
596+
Result: &envKeysMap,
597+
IgnoreUntaggedFields: true,
598+
}); err != nil {
599+
return errors.Errorf("failed to create decoder: %w", err)
600+
} else if err := dec.Decode(c.baseConfig); err != nil {
601+
return errors.Errorf("failed to decode env: %w", err)
602+
}
603+
v := viper.New()
604+
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
605+
v.AutomaticEnv()
606+
if err := v.MergeConfigMap(envKeysMap); err != nil {
607+
return errors.Errorf("failed to merge config: %w", err)
608+
} else if err := v.Unmarshal(c); err != nil {
609+
return errors.Errorf("failed to parse env to config: %w", err)
610+
}
611+
return nil
612+
}
613+
590614
func (c *config) Load(path string, fsys fs.FS) error {
591615
builder := NewPathBuilder(path)
592616
// Load default values
@@ -614,9 +638,8 @@ func (c *config) Load(path string, fsys fs.FS) error {
614638
// Load secrets from .env file
615639
if err := loadDefaultEnv(); err != nil {
616640
return err
617-
}
618-
if err := viper.Unmarshal(c); err != nil {
619-
return errors.Errorf("failed to parse env to config: %w", err)
641+
} else if err := c.loadFromEnv(); err != nil {
642+
return err
620643
}
621644
// Generate JWT tokens
622645
if len(c.Auth.AnonKey) == 0 {

pkg/config/config_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,15 @@ func TestLoadSeedPaths(t *testing.T) {
341341
assert.Empty(t, config.SqlPaths)
342342
})
343343
}
344+
345+
func TestLoadEnv(t *testing.T) {
346+
t.Setenv("AUTH_JWT_SECRET", "test-secret")
347+
t.Setenv("DB_ROOT_KEY", "test-root-key")
348+
config := NewConfig()
349+
// Run test
350+
err := config.loadFromEnv()
351+
// Check error
352+
assert.NoError(t, err)
353+
assert.Equal(t, "test-secret", config.Auth.JwtSecret)
354+
assert.Equal(t, "test-root-key", config.Db.RootKey)
355+
}

0 commit comments

Comments
 (0)