From 07cf008a3a661b4e59c7b67441f42fa436ca34b5 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 07:34:06 +0000 Subject: [PATCH] feat(config): expose service env prefix option Co-Authored-By: Virgil --- config_test.go | 22 ++++++++++++++++++++++ service.go | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/config_test.go b/config_test.go index 4ed23be..2e408f4 100644 --- a/config_test.go +++ b/config_test.go @@ -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" diff --git a/service.go b/service.go index 83f1d07..de1280c 100644 --- a/service.go +++ b/service.go @@ -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)) }