Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
cmd: Allow `caddy fmt` to read from stdin (#3680)
* Allow 'caddy fmt' to read from stdin * fmt: use '-' as the file name for reading from stdin * Minor adjustments Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
Matthew Penner committed
Sep 14, 2020 at 12:30 UTC
b88e2b6a497789307b0a12f9867cf2ac8088d73a
2 files changed
+19
-6
cmd/commandfuncs.go
+13
-4
@@ -533,7 +533,17 @@ func cmdFmt(fl Flags) (int, error) {
533
if formatCmdConfigFile == "" {
534
formatCmdConfigFile = "Caddyfile"
535
}
536
- overwrite := fl.Bool("overwrite")
536
+
537
+ // as a special case, read from stdin if the file name is "-"
538
+ if formatCmdConfigFile == "-" {
539
+ input, err := ioutil.ReadAll(os.Stdin)
540
+ if err != nil {
541
+ return caddy.ExitCodeFailedStartup,
542
+ fmt.Errorf("reading stdin: %v", err)
543
+ }
544
+ fmt.Print(string(caddyfile.Format(input)))
545
+ return caddy.ExitCodeSuccess, nil
546
+ }
547
548
input, err := ioutil.ReadFile(formatCmdConfigFile)
549
if err != nil {
@@ -543,9 +553,8 @@ func cmdFmt(fl Flags) (int, error) {
553
554
output := caddyfile.Format(input)
555
546
- if overwrite {
547
- err = ioutil.WriteFile(formatCmdConfigFile, output, 0644)
548
- if err != nil {
556
+ if fl.Bool("overwrite") {
557
+ if err := ioutil.WriteFile(formatCmdConfigFile, output, 0600); err != nil {
558
return caddy.ExitCodeFailedStartup, nil
559
}
560
} else {
cmd/commands.go
+6
-2
@@ -263,8 +263,12 @@ provisioning stages.`,
263
Formats the Caddyfile by adding proper indentation and spaces to improve
264
human readability. It prints the result to stdout.
265
266
-If --write is specified, the output will be written to the config file
267
-directly instead of printing it.`,
266
+If --overwrite is specified, the output will be written to the config file
267
+directly instead of printing it.
268
+
269
+If you wish you use stdin instead of a regular file, use - as the path.
270
+When reading from stdin, the --overwrite flag has no effect: the result
271
+is always printed to stdout.`,
272
Flags: func() *flag.FlagSet {
273
fs := flag.NewFlagSet("format", flag.ExitOnError)
274
fs.Bool("overwrite", false, "Overwrite the input file with the results")