@marcel91200 / caddy Mirror · read-only / commits / 19ff47a6
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 22:15 UTC

cmd: Allow `caddy adapt` to read from stdin (#7163)

Bobby Dhillon committed Aug 6, 2025 at 17:04 UTC 19ff47a63b9ff2ae424790b02548d9ba4afc56ba
2 files changed +17 -5
cmd/commandfuncs.go
+14 -4
@@ -441,16 +441,20 @@ func cmdEnviron(fl Flags) (int, error) {
441 }
442
443 func cmdAdaptConfig(fl Flags) (int, error) {
444 - inputFlag := fl.String("config")
444 + configFlag := fl.String("config")
445 adapterFlag := fl.String("adapter")
446 prettyFlag := fl.Bool("pretty")
447 validateFlag := fl.Bool("validate")
448
449 var err error
450 - inputFlag, err = configFileWithRespectToDefault(caddy.Log(), inputFlag)
450 + configFlag, err = configFileWithRespectToDefault(caddy.Log(), configFlag)
451 if err != nil {
452 return caddy.ExitCodeFailedStartup, err
453 }
454 + if configFlag == "" {
455 + return caddy.ExitCodeFailedStartup,
456 + fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)")
457 + }
458
459 // load all additional envs as soon as possible
460 err = handleEnvFileFlag(fl)
@@ -469,13 +473,19 @@ func cmdAdaptConfig(fl Flags) (int, error) {
473 fmt.Errorf("unrecognized config adapter: %s", adapterFlag)
474 }
475
472 - input, err := os.ReadFile(inputFlag)
476 + var input []byte
477 + // read from stdin if the file name is "-"
478 + if configFlag == "-" {
479 + input, err = io.ReadAll(os.Stdin)
480 + } else {
481 + input, err = os.ReadFile(configFlag)
482 + }
483 if err != nil {
484 return caddy.ExitCodeFailedStartup,
485 fmt.Errorf("reading input file: %v", err)
486 }
487
478 - opts := map[string]any{"filename": inputFlag}
488 + opts := map[string]any{"filename": configFlag}
489
490 adaptedConfig, warnings, err := cfgAdapter.Adapt(input, opts)
491 if err != nil {
cmd/commands.go
+3 -1
@@ -293,6 +293,8 @@ zero exit status will be returned.
293
294 If --envfile is specified, an environment file with environment variables
295 in the KEY=VALUE format will be loaded into the Caddy process.
296 +
297 +If you wish to use stdin instead of a regular file, use - as the path.
298 `,
299 CobraFunc: func(cmd *cobra.Command) {
300 cmd.Flags().StringP("config", "c", "", "Configuration file to adapt (required)")
@@ -390,7 +392,7 @@ lines will be prefixed with '-' and '+' where they differ. Note that
392 unchanged lines are prefixed with two spaces for alignment, and that this
393 is not a valid patch format.
394
393 -If you wish you use stdin instead of a regular file, use - as the path.
395 +If you wish to use stdin instead of a regular file, use - as the path.
396 When reading from stdin, the --overwrite flag has no effect: the result
397 is always printed to stdout.
398 `,