{"id":13505806,"url":"https://github.com/magodo/hclgrep","last_synced_at":"2025-04-10T16:34:27.457Z","repository":{"id":46888127,"uuid":"440847257","full_name":"magodo/hclgrep","owner":"magodo","description":"Syntax based grep for HCL(v2)","archived":false,"fork":false,"pushed_at":"2024-03-07T08:21:19.000Z","size":92,"stargazers_count":96,"open_issues_count":1,"forks_count":7,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-06T07:39:11.959Z","etag":null,"topics":["cli","grep-like","hcl2","syntax","terraform"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magodo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-12-22T12:15:33.000Z","updated_at":"2025-03-16T21:08:33.000Z","dependencies_parsed_at":"2024-04-10T21:42:33.116Z","dependency_job_id":null,"html_url":"https://github.com/magodo/hclgrep","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magodo%2Fhclgrep","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magodo%2Fhclgrep/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magodo%2Fhclgrep/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magodo%2Fhclgrep/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magodo","download_url":"https://codeload.github.com/magodo/hclgrep/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248252724,"owners_count":21072701,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","grep-like","hcl2","syntax","terraform"],"created_at":"2024-08-01T00:01:14.254Z","updated_at":"2025-04-10T16:34:27.418Z","avatar_url":"https://github.com/magodo.png","language":"Go","funding_links":[],"categories":["Tools","Go"],"sub_categories":["Community providers"],"readme":"# hclgrep\n\n![workflow](https://github.com/magodo/hclgrep/actions/workflows/go.yml/badge.svg)\n\nSearch for HCL(v2) using syntax tree.\n\nThe idea is heavily inspired by https://github.com/mvdan/gogrep.\n\n## Install\n\n    go install github.com/magodo/hclgrep@latest\n\n## Usage\n\n    usage: hclgrep [options] commands [FILE...]\n\nAn option is one of the following:\n\n    -H                  prefix the filename and byte offset of a match\n\nA command is one of the following:\n\n    -x  pattern         find all nodes matching a pattern\n    -g  pattern         discard nodes not matching a pattern\n    -v  pattern         discard nodes matching a pattern\n    -p  number          navigate up a number of node parents\n    -rx name=\"regexp\"   filter nodes by regexp against wildcard value of \"name\"\n    -w  name            print the wildcard node only (must be the last command)\n\nA pattern is a piece of HCL code which may include wildcards. It can be:\n\n- A [body](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#bodies) (zero or more attributes, and zero or more blocks)\n- An [expression](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#expressions)\n\nThere are two types of wildcards can be used in a pattern, depending on the scope it resides in:\n\n- Attribute wildcard (\"@\"): represents an [attribute](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#attribute-definitions), a [block](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#blocks), or an [object element](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#collection-values)\n- Expression wildcard (\"$\"): represents an [expression](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#expressions) or a place that a string is accepted (i.e. as a block type, block label)\n\nThe wildcards are followed by a name. Each wildcard with the same name must match the same node/string, excluding \"\\_\". Example:\n\n    $x.$_ = $x # assignment of self to a field in self\n\nThe wildcard name is only recorded for \"-x\" command or \"-g\" command (the first match in DFS).\n\nIf \"\\*\" is before the name, it will match **any** number of nodes. Example:\n\n    [$*_] # any number of elements in a tuple\n\n    resource foo \"name\" {\n        @*_  # any number of attributes/blocks inside the resource block body\n    }\n\n## Example\n\n- Grep dynamic blocks used in Terraform config\n\n        $ hclgrep -x 'dynamic $_ {@*_}' main.tf\n\n- Grep potential mis-used \"count\" in Terraform config\n\n        $ hclgrep -x 'var.$_[count.index]' main.tf\n\n- Grep module source addresses in Terraform config\n\n        $ hclgrep -x 'module $_ {@*_}' \\\n            -x 'source = $addr' \\\n            -w addr main.tf\n\n- Grep AzureRM Terraform network security rule resource which allows 22 port for inbound traffic\n\n        $ hclgrep -x 'resource azurerm_network_security_rule $_ {@*_}' \\\n        -g 'direction = \"Inbound\"' \\\n        -g 'access = \"Allow\"' \\\n        -g 'destination_port_range = $port' \\\n        -rx 'port=\"22|\\*\"' \\\n        main.tf\n\n- Grep for the evaluated Terraform configurations, run following command in the root module (given there is no output variables defined)\n\n        $ terraform show -no-color | sed --expression 's;(sensitive value);\"\";' | hclgrep -x '\u003cpattern\u003e'\n\n## Limitation\n\n- The **any** expression wildcard (`$*`) doesn't work inside a traversal.\n- The **any** wildcard doesn't remember the matched wildcard name.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagodo%2Fhclgrep","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagodo%2Fhclgrep","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagodo%2Fhclgrep/lists"}