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

This commit is contained in:
Bobby Dhillon
2025-08-06 17:04:28 -07:00
committed by GitHub
parent 007f4066f6
commit 19ff47a63b
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.
`, `,