mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 22:23:48 +03:00
cmd: Add --diff
option for caddy fmt
(#4695)
This commit is contained in:
parent
3e3bb00265
commit
6512832f9f
2 changed files with 21 additions and 0 deletions
|
@ -32,6 +32,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/aryann/difflib"
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
"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 {
|
if err := os.WriteFile(formatCmdConfigFile, output, 0600); err != nil {
|
||||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("overwriting formatted file: %v", err)
|
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 {
|
} else {
|
||||||
fmt.Print(string(output))
|
fmt.Print(string(output))
|
||||||
}
|
}
|
||||||
|
|
|
@ -282,12 +282,18 @@ human readability. It prints the result to stdout.
|
||||||
If --overwrite is specified, the output will be written to the config file
|
If --overwrite is specified, the output will be written to the config file
|
||||||
directly instead of printing it.
|
directly instead of printing it.
|
||||||
|
|
||||||
|
If --diff is specified, the output will be compared against the input, and
|
||||||
|
lines will be prefixed with '-' and '+' where they differ. Note that
|
||||||
|
unchanged lines are prefixed with two spaces for alignment, and that this
|
||||||
|
is not a valid patch format.
|
||||||
|
|
||||||
If you wish you use stdin instead of a regular file, use - as the path.
|
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
|
When reading from stdin, the --overwrite flag has no effect: the result
|
||||||
is always printed to stdout.`,
|
is always printed to stdout.`,
|
||||||
Flags: func() *flag.FlagSet {
|
Flags: func() *flag.FlagSet {
|
||||||
fs := flag.NewFlagSet("fmt", flag.ExitOnError)
|
fs := flag.NewFlagSet("fmt", flag.ExitOnError)
|
||||||
fs.Bool("overwrite", false, "Overwrite the input file with the results")
|
fs.Bool("overwrite", false, "Overwrite the input file with the results")
|
||||||
|
fs.Bool("diff", false, "Print the differences between the input file and the formatted output")
|
||||||
return fs
|
return fs
|
||||||
}(),
|
}(),
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue