-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WiP] fix #2072 make sure the generated
code is formatted before writing it to a file.
#3917
base: main
Are you sure you want to change the base?
[WiP] fix #2072 make sure the generated
code is formatted before writing it to a file.
#3917
Conversation
…ore writing it to a file.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThis pull request updates the file generation process. In Changes
Sequence Diagram(s)sequenceDiagram
participant T as Test
participant W as WriteToFile
participant H as hclwrite.Format
participant OS as os.WriteFile
T->>W: Call WriteToFile(contents)
W->>H: Format(contents)
H-->>W: Return formattedContents
W->>OS: Write formattedContents
OS-->>W: Write success/error
W->>T: Return result
Suggested reviewers
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
codegen/generate.go (1)
141-141
: Nice improvement! 👍Good call on using
hclwrite.Format
to ensure consistent formatting. The placement right before writing to file is perfect.Consider extracting the formatting into a separate function for better testability and reuse:
-if err := os.WriteFile(targetPath, hclwrite.Format([]byte(contentsToWrite)), ownerWriteGlobalReadPerms); err != nil { +formattedContent := hclwrite.Format([]byte(contentsToWrite)) +if err := os.WriteFile(targetPath, formattedContent, ownerWriteGlobalReadPerms); err != nil {codegen/generate_test.go (1)
148-201
: Great test structure! Let's add more test cases.The table-driven test pattern is perfect for this, and the first test case is a good start. However, we could strengthen the test coverage with more cases.
Consider adding these test cases:
tc := []struct { name string disabled bool path string contents string expected string ifExists codegen.GenerateConfigExists }{ { "generate-simple-example", false, fmt.Sprintf("%s/%s", testDir, "simple_example"), "variable \"msg\"{\ntype=string\n default=\"hello\"\n}\n", "variable \"msg\" {\n type = string\n default = \"hello\"\n}\n", codegen.ExistsError, }, + { + "generate-block-with-nested-blocks", + false, + fmt.Sprintf("%s/%s", testDir, "nested_blocks"), + "resource\"aws_instance\"\"example\"{\nami=\"ami-123\"\ntags={\nName=\"test\"\n}\n}\n", + "resource \"aws_instance\" \"example\" {\n ami = \"ami-123\"\n tags = {\n Name = \"test\"\n }\n}\n", + codegen.ExistsError, + }, + { + "generate-with-comments", + false, + fmt.Sprintf("%s/%s", testDir, "with_comments"), + "#test comment\nvariable\"foo\"{#another comment\ntype=string}\n", + "# test comment\nvariable \"foo\" { # another comment\n type = string\n}\n", + codegen.ExistsError, + }, },
@@ -138,7 +138,7 @@ func WriteToFile(terragruntOptions *options.TerragruntOptions, basePath string, | |||
contentsToWrite := fmt.Sprintf("%s%s", prefix, config.Contents) | |||
|
|||
const ownerWriteGlobalReadPerms = 0644 | |||
if err := os.WriteFile(targetPath, []byte(contentsToWrite), ownerWriteGlobalReadPerms); err != nil { | |||
if err := os.WriteFile(targetPath, hclwrite.Format([]byte(contentsToWrite)), ownerWriteGlobalReadPerms); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wakeful , this needs to be opt-in. The generate
block can write arbitrary content, not just HCL content.
We could have a fmt
attribute that determines whether to format the file, and users can set that to format the file.
We can also look to leverage the filename extension to have a smarter default for formatting (e.g. if the file has extension hcl
, tf
or tofu
, default to format the file if not defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood! 💪
I will mark this PR as WiP and come back with extended functionality.
generated
code is formatted before writing it to a file.generated
code is formatted before writing it to a file.
Description
Fixes #2072 make sure the
generated
code is formatted before writing it to a file.TODOs
Read the Gruntwork contribution guidelines.
Release Notes (draft)
Updated the
generated
block to format theHCL
syntax before writing it to a file, fix for #2072.Migration Guide
n/a
Summary by CodeRabbit
New Features
Tests