cmd: Allow caddy adapt to read from stdin (#7163)
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.24.1, ubuntu-latest, 0, 1.24, linux) (push) Failing after 2m55s
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Has been skipped
Cross-Build / build (~1.24.1, 1.24, aix) (push) Successful in 1m31s
Cross-Build / build (~1.24.1, 1.24, darwin) (push) Successful in 1m31s
Cross-Build / build (~1.24.1, 1.24, dragonfly) (push) Successful in 1m33s
Cross-Build / build (~1.24.1, 1.24, freebsd) (push) Successful in 1m34s
Cross-Build / build (~1.24.1, 1.24, illumos) (push) Successful in 1m34s
Cross-Build / build (~1.24.1, 1.24, linux) (push) Successful in 1m39s
Cross-Build / build (~1.24.1, 1.24, netbsd) (push) Successful in 1m36s
Cross-Build / build (~1.24.1, 1.24, openbsd) (push) Successful in 1m34s
Cross-Build / build (~1.24.1, 1.24, solaris) (push) Successful in 1m35s
Cross-Build / build (~1.24.1, 1.24, windows) (push) Successful in 1m33s
Lint / lint (ubuntu-latest, linux) (push) Successful in 2m29s
Lint / govulncheck (push) Successful in 1m48s
Lint / dependency-review (push) Failing after 59s
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Has started running
Tests / test (./cmd/caddy/caddy, ~1.24.1, macos-14, 0, 1.24, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.24.1, windows-latest, True, 1.24, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled

This commit is contained in:
Bobby Dhillon 2025-08-06 17:04:28 -07:00 committed by GitHub
parent 007f4066f6
commit 19ff47a63b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 5 deletions

View File

@ -441,16 +441,20 @@ func cmdEnviron(fl Flags) (int, error) {
} }
func cmdAdaptConfig(fl Flags) (int, error) { func cmdAdaptConfig(fl Flags) (int, error) {
inputFlag := fl.String("config") configFlag := fl.String("config")
adapterFlag := fl.String("adapter") adapterFlag := fl.String("adapter")
prettyFlag := fl.Bool("pretty") prettyFlag := fl.Bool("pretty")
validateFlag := fl.Bool("validate") validateFlag := fl.Bool("validate")
var err error var err error
inputFlag, err = configFileWithRespectToDefault(caddy.Log(), inputFlag) configFlag, err = configFileWithRespectToDefault(caddy.Log(), configFlag)
if err != nil { if err != nil {
return caddy.ExitCodeFailedStartup, err return caddy.ExitCodeFailedStartup, err
} }
if configFlag == "" {
return caddy.ExitCodeFailedStartup,
fmt.Errorf("input file required when there is no Caddyfile in current directory (use --config flag)")
}
// load all additional envs as soon as possible // load all additional envs as soon as possible
err = handleEnvFileFlag(fl) err = handleEnvFileFlag(fl)
@ -469,13 +473,19 @@ func cmdAdaptConfig(fl Flags) (int, error) {
fmt.Errorf("unrecognized config adapter: %s", adapterFlag) fmt.Errorf("unrecognized config adapter: %s", adapterFlag)
} }
input, err := os.ReadFile(inputFlag) var input []byte
// read from stdin if the file name is "-"
if configFlag == "-" {
input, err = io.ReadAll(os.Stdin)
} else {
input, err = os.ReadFile(configFlag)
}
if err != nil { if err != nil {
return caddy.ExitCodeFailedStartup, return caddy.ExitCodeFailedStartup,
fmt.Errorf("reading input file: %v", err) fmt.Errorf("reading input file: %v", err)
} }
opts := map[string]any{"filename": inputFlag} opts := map[string]any{"filename": configFlag}
adaptedConfig, warnings, err := cfgAdapter.Adapt(input, opts) adaptedConfig, warnings, err := cfgAdapter.Adapt(input, opts)
if err != nil { if err != nil {

View File

@ -293,6 +293,8 @@ zero exit status will be returned.
If --envfile is specified, an environment file with environment variables If --envfile is specified, an environment file with environment variables
in the KEY=VALUE format will be loaded into the Caddy process. in the KEY=VALUE format will be loaded into the Caddy process.
If you wish to use stdin instead of a regular file, use - as the path.
`, `,
CobraFunc: func(cmd *cobra.Command) { CobraFunc: func(cmd *cobra.Command) {
cmd.Flags().StringP("config", "c", "", "Configuration file to adapt (required)") 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
unchanged lines are prefixed with two spaces for alignment, and that this unchanged lines are prefixed with two spaces for alignment, and that this
is not a valid patch format. is not a valid patch format.
If you wish you use stdin instead of a regular file, use - as the path. If you wish to use stdin instead of a regular file, use - as the path.
When reading from stdin, the --overwrite flag has no effect: the result When reading from stdin, the --overwrite flag has no effect: the result
is always printed to stdout. is always printed to stdout.
`, `,