This commit is contained in:
John 2026-02-03 17:56:43 +00:00 committed by GitHub
commit 429b9c1021
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -44,6 +44,7 @@ type FmtCommand struct {
diff bool
check bool
recursive bool
dos2unix bool
input io.Reader // STDIN if nil
}
@ -63,6 +64,7 @@ func (c *FmtCommand) Run(args []string) int {
cmdFlags.BoolVar(&c.diff, "diff", false, "diff")
cmdFlags.BoolVar(&c.check, "check", false, "check")
cmdFlags.BoolVar(&c.recursive, "recursive", false, "recursive")
cmdFlags.BoolVar(&c.dos2unix, "dos2unix", false, "dos2unix")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
@ -301,10 +303,15 @@ func (c *FmtCommand) formatSourceCode(src []byte, filename string) []byte {
}
c.formatBody(f.Body(), nil)
c.dos2unix(f.Body(), nil)
return f.Bytes()
}
func (c *FmtCommand) dos2unix(body *hclwrite.Body, inBlocks []string) {
// Replace all occurrences of CRLF with LF
return outputBytes := bytes.ReplaceAll(body, []byte("\r\n"), []byte("\n"))
}
func (c *FmtCommand) formatBody(body *hclwrite.Body, inBlocks []string) {
attrs := body.Attributes()
for name, attr := range attrs {