feat(config): expose service env prefix option
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
411c82a6d4
commit
07cf008a3a
2 changed files with 27 additions and 0 deletions
|
|
@ -1,12 +1,14 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
coreio "forge.lthn.ai/core/go-io"
|
||||
core "forge.lthn.ai/core/go/pkg/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
|
@ -409,6 +411,26 @@ func TestConfig_WithEnvPrefix_TrailingUnderscore_Good(t *testing.T) {
|
|||
assert.Equal(t, "secret", setting)
|
||||
}
|
||||
|
||||
func TestService_OnStartup_WithEnvPrefix_Good(t *testing.T) {
|
||||
t.Setenv("MYAPP_SETTING", "secret")
|
||||
|
||||
m := coreio.NewMockMedium()
|
||||
svc := &Service{
|
||||
ServiceRuntime: core.NewServiceRuntime(nil, ServiceOptions{
|
||||
EnvPrefix: "MYAPP",
|
||||
Medium: m,
|
||||
}),
|
||||
}
|
||||
|
||||
err := svc.OnStartup(context.Background())
|
||||
assert.NoError(t, err)
|
||||
|
||||
var setting string
|
||||
err = svc.Get("setting", &setting)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "secret", setting)
|
||||
}
|
||||
|
||||
func TestConfig_Get_EmptyKey(t *testing.T) {
|
||||
m := coreio.NewMockMedium()
|
||||
m.Files["/config.yaml"] = "app:\n name: test\nversion: 1"
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ type Service struct {
|
|||
type ServiceOptions struct {
|
||||
// Path overrides the default config file path.
|
||||
Path string
|
||||
// EnvPrefix overrides the default environment variable prefix.
|
||||
EnvPrefix string
|
||||
// Medium overrides the default storage medium.
|
||||
Medium coreio.Medium
|
||||
}
|
||||
|
|
@ -39,6 +41,9 @@ func (s *Service) OnStartup(_ context.Context) error {
|
|||
if opts.Path != "" {
|
||||
configOpts = append(configOpts, WithPath(opts.Path))
|
||||
}
|
||||
if opts.EnvPrefix != "" {
|
||||
configOpts = append(configOpts, WithEnvPrefix(opts.EnvPrefix))
|
||||
}
|
||||
if opts.Medium != nil {
|
||||
configOpts = append(configOpts, WithMedium(opts.Medium))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue