Skip to content

Commit a35c1d4

Browse files
committed
Add --envfile support for adapt command
1 parent 31790d3 commit a35c1d4

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Caddyfile.*
1212
cmd/caddy/caddy
1313
cmd/caddy/caddy.exe
1414
cmd/caddy/tmp/*.exe
15+
cmd/caddy/.env
1516

1617
# mac specific
1718
.DS_Store

cmd/commandfuncs.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,21 @@ func cmdAdaptConfig(fl Flags) (int, error) {
445445
return caddy.ExitCodeFailedStartup, err
446446
}
447447

448+
var envfileFlag []string
449+
envfileFlag, err = fl.GetStringSlice("envfile")
450+
if err != nil {
451+
return caddy.ExitCodeFailedStartup,
452+
fmt.Errorf("reading envfile flag: %v", err)
453+
}
454+
455+
// load all additional envs as soon as possible
456+
for _, envfile := range envfileFlag {
457+
if err := loadEnvFromFile(envfile); err != nil {
458+
return caddy.ExitCodeFailedStartup,
459+
fmt.Errorf("loading additional environment variables: %v", err)
460+
}
461+
}
462+
448463
if adapterFlag == "" {
449464
return caddy.ExitCodeFailedStartup,
450465
fmt.Errorf("adapter name is required (use --adapt flag or leave unspecified for default)")

cmd/commands.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Environments may contain sensitive data.
264264

265265
RegisterCommand(Command{
266266
Name: "adapt",
267-
Usage: "--config <path> [--adapter <name>] [--pretty] [--validate]",
267+
Usage: "--config <path> [--adapter <name>] [--pretty] [--validate] [--envfile <path>]",
268268
Short: "Adapts a configuration to Caddy's native JSON",
269269
Long: `
270270
Adapts a configuration to Caddy's native JSON format and writes the
@@ -276,12 +276,16 @@ for human readability.
276276
If --validate is used, the adapted config will be checked for validity.
277277
If the config is invalid, an error will be printed to stderr and a non-
278278
zero exit status will be returned.
279+
280+
If --envfile is specified, an environment file with environment variables in
281+
the KEY=VALUE format will be loaded into the Caddy process.
279282
`,
280283
CobraFunc: func(cmd *cobra.Command) {
281284
cmd.Flags().StringP("config", "c", "", "Configuration file to adapt (required)")
282285
cmd.Flags().StringP("adapter", "a", "caddyfile", "Name of config adapter")
283286
cmd.Flags().BoolP("pretty", "p", false, "Format the output for human readability")
284287
cmd.Flags().BoolP("validate", "", false, "Validate the output")
288+
cmd.Flags().StringSliceP("envfile", "", []string{}, "Environment file(s) to load")
285289
cmd.RunE = WrapCommandFuncForCobra(cmdAdaptConfig)
286290
},
287291
})

0 commit comments

Comments
 (0)