aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/commandfuncs.go
diff options
context:
space:
mode:
authorMatthew Penner <[email protected]>2020-09-14 12:30:12 -0600
committerGitHub <[email protected]>2020-09-14 12:30:12 -0600
commitb88e2b6a497789307b0a12f9867cf2ac8088d73a (patch)
tree60c795e4c95ab5058fc1ec1c3aa027c378fb26af /cmd/commandfuncs.go
parent4217217badf220d7d2c25f43f955fdc8454f2c64 (diff)
downloadcaddy-b88e2b6a497789307b0a12f9867cf2ac8088d73a.tar.gz
caddy-b88e2b6a497789307b0a12f9867cf2ac8088d73a.zip
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 <[email protected]>
Diffstat (limited to 'cmd/commandfuncs.go')
-rw-r--r--cmd/commandfuncs.go17
1 files changed, 13 insertions, 4 deletions
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go
index a10bc1919..772fe0129 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 {