google / keep-sorted

keep-sorted is a language-agnostic formatter that sorts lines between two markers in a larger file.
Apache License 2.0
132 stars 15 forks source link

Sort Terraform's data.tf #44

Closed ruzickap closed 3 weeks ago

ruzickap commented 1 month ago

I'm trying to sort the following Terraform's data.tf which looks like:

# keep-sorted start
# [CloudDNS-001] Cloud DNS public DNS managed zone should have DNSSEC enabled
data "wiz_cloud_configuration_rules" "clouddns_001" {
  search = "CloudDNS-001"
}

# [CloudSQL-027] SQL Database should be configured to send Threat Detection email alerts
data "wiz_cloud_configuration_rules" "cloudsql_027" {
  search = "CloudSQL-027"
}

# [CloudFront-010] CloudFront distribution logging should be enabled
data "wiz_cloud_configuration_rules" "cloudfront_010" {
  search = "CloudFront-010"
}

# [CloudFront-007] CloudFront distribution should be associated with a WAF web ACL
data "wiz_cloud_configuration_rules" "cloudfront_007" {
  search = "CloudFront-007"
}
# keep-sorted end

After using the keep-sorted I got:

# keep-sorted start

# [CloudDNS-001] Cloud DNS public DNS managed zone should have DNSSEC enabled
data "wiz_cloud_configuration_rules" "clouddns_001" {
  search = "CloudDNS-001"
# [CloudFront-007] CloudFront distribution should be associated with a WAF web ACL
data "wiz_cloud_configuration_rules" "cloudfront_007" {
  search = "CloudFront-007"
# [CloudFront-010] CloudFront distribution logging should be enabled
data "wiz_cloud_configuration_rules" "cloudfront_010" {
  search = "CloudFront-010"
# [CloudSQL-027] SQL Database should be configured to send Threat Detection email alerts
data "wiz_cloud_configuration_rules" "cloudsql_027" {
  search = "CloudSQL-027"
}
# keep-sorted end

As you can see the } and new lines are missing. The remove_duplicates=no doesn't help to sort it properly. (ChatGPT or Gemini can do it, but that is not what I'm looking for...)

Anyway - can keep-sorted work with HCL / Terraform syntax and sort such files?

Thank you

AlexanderGH commented 3 weeks ago

Have you tried enabling blocks? https://github.com/google/keep-sorted#blocks

ruzickap commented 3 weeks ago

Thank you.

It seems like block=yes "almost" did the job and I got following output:

# keep-sorted start block=yes

# [CloudDNS-001] Cloud DNS public DNS managed zone should have DNSSEC enabled
data "wiz_cloud_configuration_rules" "clouddns_001" {
  search = "CloudDNS-001"
}
# [CloudFront-007] CloudFront distribution should be associated with a WAF web ACL
data "wiz_cloud_configuration_rules" "cloudfront_007" {
  search = "CloudFront-007"
}
# [CloudFront-010] CloudFront distribution logging should be enabled
data "wiz_cloud_configuration_rules" "cloudfront_010" {
  search = "CloudFront-010"
}
# [CloudSQL-027] SQL Database should be configured to send Threat Detection email alerts
data "wiz_cloud_configuration_rules" "cloudsql_027" {
  search = "CloudSQL-027"
}
# keep-sorted end

^^^ It is correct now - thank you for the tip.

Is there any way how I can keep "empty line" between the data blocks? (It is more readable...)

Thanks...

JeffFaer commented 3 weeks ago

https://github.com/google/keep-sorted#newline-separated

Also add newline_separated=yes to your keep-sorted start line

ruzickap commented 3 weeks ago

Thanks a lot... Both of these parameters did the right job.

# keep-sorted start block=yes newline_separated=yes
....

Closing...