Skip to content

Commit dcbaa2a

Browse files
feat: load config from env
1 parent ef62f56 commit dcbaa2a

File tree

2 files changed

+66
-32
lines changed

2 files changed

+66
-32
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,20 @@ services:
162162
smtpbridge:
163163
image: ghcr.io/itsnotgoodname/smtpbridge:latest
164164
container_name: smtpbridge
165+
environment:
166+
- SMTPBRIDGE_CONFIG_YAML: | # Config option 1
167+
endpoints:
168+
hello_world:
169+
kind: console
170+
171+
rules:
172+
hello_world:
165173
ports:
166174
- 1025:1025
167175
- 8080:8080
168176
volumes:
177+
- /path/to/config:/config # Config option 2
169178
- /path/to/data:/data
170-
- /path/to/config:/config
171179
- /etc/timezone:/etc/timezone:ro
172180
- /etc/localtime:/etc/localtime:ro
173181
restart: unless-stopped

config/config.go

+57-31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"path"
66
"strconv"
7+
"strings"
78
"time"
89

910
"github.com/ItsNotGoodName/smtpbridge/internal/endpoints"
@@ -180,44 +181,69 @@ type RawRule struct {
180181
Endpoints []string
181182
}
182183

184+
const envConfigYamlKey = "SMTPBRIDGE_CONFIG_YAML"
185+
183186
func Read(cli CLI) (Raw, error) {
184-
var configFiles []string
185-
if cli.Config == nil {
186-
// DEPS: ../README.md
187-
configFile, err := resolve([]string{
188-
"config.yaml",
189-
"config.yml",
190-
".smtpbridge.yaml",
191-
".smtpbridge.yml",
192-
"~/.smtpbridge.yaml",
193-
"~/.smtpbridge.yml",
194-
"/etc/smtpbridge.yaml",
195-
"/etc/smtpbridge.yml",
196-
})
197-
if err != nil {
198-
return Raw{}, err
187+
var raw Raw
188+
envConfigYaml := os.Getenv(envConfigYamlKey)
189+
if envConfigYaml == "" {
190+
// Resolve config file
191+
var configFiles []string
192+
if cli.Config == nil {
193+
// DEPS: ../README.md
194+
configFile, err := resolve([]string{
195+
"config.yaml",
196+
"config.yml",
197+
".smtpbridge.yaml",
198+
".smtpbridge.yml",
199+
"~/.smtpbridge.yaml",
200+
"~/.smtpbridge.yml",
201+
"/etc/smtpbridge.yaml",
202+
"/etc/smtpbridge.yml",
203+
})
204+
if err != nil {
205+
return Raw{}, err
206+
}
207+
208+
if configFile != "" {
209+
configFiles = []string{configFile}
210+
}
211+
} else if *cli.Config != "" {
212+
configFiles = []string{*cli.Config}
199213
}
200214

201-
if configFile != "" {
202-
configFiles = []string{configFile}
215+
if len(configFiles) != 0 {
216+
// Load config file
217+
log.Info().Str("path", configFiles[0]).Msg("Reading config file")
218+
219+
parser, err := kong.New(&raw, kong.Configuration(kongyaml.Loader, configFiles...))
220+
if err != nil {
221+
return Raw{}, err
222+
}
223+
224+
_, err = parser.Parse([]string{})
225+
if err != nil {
226+
return Raw{}, err
227+
}
203228
}
204-
} else if *cli.Config != "" {
205-
configFiles = []string{*cli.Config}
206-
}
229+
} else {
230+
// Load config file from ENV
231+
log.Info().Msgf("Reading config from environment variable %s", envConfigYamlKey)
207232

208-
if len(configFiles) != 0 {
209-
log.Info().Str("path", configFiles[0]).Msg("Reading config file")
210-
}
233+
resolver, err := kongyaml.Loader(strings.NewReader(envConfigYaml))
234+
if err != nil {
235+
return Raw{}, err
236+
}
211237

212-
var raw Raw
213-
parser, err := kong.New(&raw, kong.Configuration(kongyaml.Loader, configFiles...))
214-
if err != nil {
215-
return Raw{}, err
216-
}
238+
parser, err := kong.New(&raw, kong.Resolvers(resolver))
239+
if err != nil {
240+
return Raw{}, err
241+
}
217242

218-
_, err = parser.Parse([]string{})
219-
if err != nil {
220-
return Raw{}, err
243+
_, err = parser.Parse([]string{})
244+
if err != nil {
245+
return Raw{}, err
246+
}
221247
}
222248

223249
for endKey := range raw.Endpoints {

0 commit comments

Comments
 (0)