aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/commandfuncs.go
diff options
context:
space:
mode:
authorFrancis Lavoie <[email protected]>2022-04-12 14:49:19 -0400
committerGitHub <[email protected]>2022-04-12 14:49:19 -0400
commit6512832f9f3904209ccb3c305e38ebde108083fb (patch)
treeaf0b674126e80469bd5baf8a485e73062074bb86 /cmd/commandfuncs.go
parent3e3bb00265f8fa0dd8ff3d33ce901776fd689f32 (diff)
downloadcaddy-6512832f9f3904209ccb3c305e38ebde108083fb.tar.gz
caddy-6512832f9f3904209ccb3c305e38ebde108083fb.zip
cmd: Add `--diff` option for `caddy fmt` (#4695)
Diffstat (limited to 'cmd/commandfuncs.go')
-rw-r--r--cmd/commandfuncs.go15
1 files changed, 15 insertions, 0 deletions
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go
index 3cbe85b90..79604d936 100644
--- a/cmd/commandfuncs.go
+++ b/cmd/commandfuncs.go
@@ -32,6 +32,7 @@ import (
"sort"
"strings"
+ "github.com/aryann/difflib"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
@@ -569,6 +570,20 @@ func cmdFmt(fl Flags) (int, error) {
if err := os.WriteFile(formatCmdConfigFile, output, 0600); err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("overwriting formatted file: %v", err)
}
+ } else if fl.Bool("diff") {
+ diff := difflib.Diff(
+ strings.Split(string(input), "\n"),
+ strings.Split(string(output), "\n"))
+ for _, d := range diff {
+ switch d.Delta {
+ case difflib.Common:
+ fmt.Printf(" %s\n", d.Payload)
+ case difflib.LeftOnly:
+ fmt.Printf("- %s\n", d.Payload)
+ case difflib.RightOnly:
+ fmt.Printf("+ %s\n", d.Payload)
+ }
+ }
} else {
fmt.Print(string(output))
}