Skip to content

Conversation

@gotwarlost
Copy link
Collaborator

@gotwarlost gotwarlost commented Nov 17, 2025

add a new fmt command that allows for formatting and checking the format of .hcl files. In the process convert any key value pairs inside object literals to always consistently use = instead of :.

It uses the std hclformat package for formatting.

A flag is present to turn off the key value normalizer. It may be removed once we have enough experience with the formatter in the wild.

@gotwarlost gotwarlost requested a review from Copilot November 17, 2025 19:34
add a new fmt command that allows for formatting and checking the format of .hcl files.
In the process convert any key value pairs inside object literals to always
consistently use = instead of :.

It uses the std hclformat package for formatting.

A flag is present to turn off the key value normalizer. It may be removed once we
have enough experience with the formatter in the wild.

Signed-off-by: gotwarlost <krishnan.anantheswaran@elastic.co>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements an opinionated format command for HCL files that standardizes object literal syntax from key: value to key = value while preserving the correct usage of colons in ternary expressions and for-expressions.

Key Changes

  • Added a new fmt command with check mode and recursive directory processing capabilities
  • Implemented token-based parsing to distinguish between object literal colons and expression colons
  • Created comprehensive test coverage for various HCL syntax patterns including nested objects, ternaries, and for-expressions

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/format/format.go Core formatting logic with stack-based token processing to normalize object literal syntax
internal/format/format_test.go Comprehensive test suite covering object literals, ternaries, for-expressions, and edge cases
internal/format/cmd.go CLI implementation with file collection, stdin support, and check mode
cmd/fn-hcl-tools/tools.go Command registration with flags for normalization, checking, and recursion
cmd/fn-hcl-tools/main.go Integration of format command into main CLI

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: gotwarlost <krishnan.anantheswaran@elastic.co>
Signed-off-by: gotwarlost <krishnan.anantheswaran@elastic.co>
Copy link
Collaborator

@ydubreuil ydubreuil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, very well done. I added a few nit comments for the tests.

Co-authored-by: Yoann Dubreuil <yoann.dubreuil@gmail.com>
Signed-off-by: gotwarlost <krishnan.anantheswaran@elastic.co>
@gotwarlost
Copy link
Collaborator Author

OK, I committed your suggestions but realized they were incorrect because the variables in the locals block are HCL attributes and cannot be assigned using : in the first place. I updated it to use a variant that assigns a local to an object that has a colon-delimited kv.

@gotwarlost gotwarlost merged commit a36b5e5 into main Nov 19, 2025
6 checks passed
@gotwarlost gotwarlost deleted the fmt branch November 19, 2025 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants