terrafmt icon indicating copy to clipboard operation
terrafmt copied to clipboard

New command: `terrafmt grep`

Open magodo opened this issue 2 years ago • 2 comments

This PR integrated hclgrep into the terraform.

Example

terraform-provider-azurerm on  revert_case_sensitivity_containers via 🐹 v1.17.7 took 3s 
💢  terrafmt grep -p "*_test.go" . -- -H -x 'resource azurerm_servicebus_namespace $_ {@*_}' -x 'sku = "Basic"'

####### B5 @ #657
internal/services/eventgrid/eventgrid_event_subscription_resource_test.go:643,3-32:
sku                 = "Basic"

####### B7 @ #715
internal/services/eventgrid/eventgrid_system_topic_event_subscription_resource_test.go:689,3-32:
sku                 = "Basic"

####### B4 @ #281
internal/services/servicebus/servicebus_namespace_resource_test.go:279,3-32:
sku                 = "Basic"

####### B6 @ #323
internal/services/servicebus/servicebus_namespace_resource_test.go:320,3-32:
sku                 = "Basic"

magodo avatar Mar 02 '22 09:03 magodo

@katbyte Thank you for looking into this!

The naming of the options and the wildcard pattern is inherited from https://github.com/mvdan/gogrep, where the spelling of the "-x", "-g" and "-v" verbs came from Rob Pike's Sam editor command language: https://en.wikipedia.org/wiki/Sam_(text_editor) (also used in the Acme editor).

And you are right, hclgrep/gogrep is not same as grep as they are grepping the AST, which makes their syntax a bit complex. I agree to rename the subcommand from grep to hclgrep, to make it clear for users. For the "address-like" syntax, there is an implemention in https://github.com/minamijoyo/hcledit.

magodo avatar Mar 17 '22 06:03 magodo

@magodo I'm not familiar with gogrep, but its popularity is dwarfed by the number of people who use grep/egrep, and we are not grepping go. we are grepping HCL/terraform blocks which is very different from go. Thus I'm not sure why we would be modelling a go tool. The concepts don't exactly match and its not clear how to do things as is evident by the complicated syntax we ended up with here.

katbyte avatar Mar 17 '22 20:03 katbyte