Skip to content

Commit 19ff47a

Browse files
authored
cmd: Allow caddy adapt to read from stdin (#7163)
1 parent 007f406 commit 19ff47a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

cmd/commandfuncs.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,16 +441,20 @@ func cmdEnviron(fl Flags) (int, error) {
441441
}
442442

443443
func cmdAdaptConfig(fl Flags) (int, error) {
444-
inputFlag := fl.String("config")
444+
configFlag := fl.String("config")
445445
adapterFlag := fl.String("adapter")
446446
prettyFlag := fl.Bool("pretty")
447447
validateFlag := fl.Bool("validate")
448448

449449
var err error
450-
inputFlag, err = configFileWithRespectToDefault(caddy.Log(), inputFlag)
450+
configFlag, err = configFileWithRespectToDefault(caddy.Log(), configFlag)
451451
if err != nil {
452452
return caddy.ExitCodeFailedStartup, err
453453
}
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+
}
454458

455459
// load all additional envs as soon as possible
456460
err = handleEnvFileFlag(fl)
@@ -469,13 +473,19 @@ func cmdAdaptConfig(fl Flags) (int, error) {
469473
fmt.Errorf("unrecognized config adapter: %s", adapterFlag)
470474
}
471475

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+
}
473483
if err != nil {
474484
return caddy.ExitCodeFailedStartup,
475485
fmt.Errorf("reading input file: %v", err)
476486
}
477487

478-
opts := map[string]any{"filename": inputFlag}
488+
opts := map[string]any{"filename": configFlag}
479489

480490
adaptedConfig, warnings, err := cfgAdapter.Adapt(input, opts)
481491
if err != nil {

cmd/commands.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ zero exit status will be returned.
293293
294294
If --envfile is specified, an environment file with environment variables
295295
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.
296298
`,
297299
CobraFunc: func(cmd *cobra.Command) {
298300
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
390392
unchanged lines are prefixed with two spaces for alignment, and that this
391393
is not a valid patch format.
392394
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.
394396
When reading from stdin, the --overwrite flag has no effect: the result
395397
is always printed to stdout.
396398
`,

0 commit comments

Comments
 (0)