summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Penner <me@matthewp.io>2020-09-14 12:30:12 -0600
committerGitHub <noreply@github.com>2020-09-14 12:30:12 -0600
commitb88e2b6a497789307b0a12f9867cf2ac8088d73a (patch)
tree60c795e4c95ab5058fc1ec1c3aa027c378fb26af
parent4217217badf220d7d2c25f43f955fdc8454f2c64 (diff)
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>
-rw-r--r--cmd/commandfuncs.go17
-rw-r--r--cmd/commands.go8
2 files changed, 19 insertions, 6 deletions
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go
index a10bc19..772fe01 100644
--- a/cmd/commandfuncs.go
+++ b/cmd/commandfuncs.go
@@ -533,7 +533,17 @@ func cmdFmt(fl Flags) (int, error) {
if formatCmdConfigFile == "" {
formatCmdConfigFile = "Caddyfile"
}
- overwrite := fl.Bool("overwrite")
+
+ // as a special case, read from stdin if the file name is "-"
+ if formatCmdConfigFile == "-" {
+ input, err := ioutil.ReadAll(os.Stdin)
+ if err != nil {
+ return caddy.ExitCodeFailedStartup,
+ fmt.Errorf("reading stdin: %v", err)
+ }
+ fmt.Print(string(caddyfile.Format(input)))
+ return caddy.ExitCodeSuccess, nil
+ }
input, err := ioutil.ReadFile(formatCmdConfigFile)
if err != nil {
@@ -543,9 +553,8 @@ func cmdFmt(fl Flags) (int, error) {
output := caddyfile.Format(input)
- if overwrite {
- err = ioutil.WriteFile(formatCmdConfigFile, output, 0644)
- if err != nil {
+ if fl.Bool("overwrite") {
+ if err := ioutil.WriteFile(formatCmdConfigFile, output, 0600); err != nil {
return caddy.ExitCodeFailedStartup, nil
}
} else {
diff --git a/cmd/commands.go b/cmd/commands.go
index 99d5245..ea8402d 100644
--- a/cmd/commands.go
+++ b/cmd/commands.go
@@ -263,8 +263,12 @@ provisioning stages.`,
Formats the Caddyfile by adding proper indentation and spaces to improve
human readability. It prints the result to stdout.
-If --write is specified, the output will be written to the config file
-directly instead of printing it.`,
+If --overwrite is specified, the output will be written to the config file
+directly instead of printing it.
+
+If you wish you use stdin instead of a regular file, use - as the path.
+When reading from stdin, the --overwrite flag has no effect: the result
+is always printed to stdout.`,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("format", flag.ExitOnError)
fs.Bool("overwrite", false, "Overwrite the input file with the results")