Search for HCL(v2) using syntax tree

Overview

hclgrep

Search for HCL(v2) using syntax tree.

The idea is heavily inspired by https://github.com/mvdan/gogrep.

Install

go install github.com/magodo/[email protected]

Usage

usage: hclgrep pattern [files]

A pattern is a piece of HCL code which may include wildcards. It can be:

  • A body (zero or more attributes, and zero or more blocks)
  • An expression

There are two types of wildcards, depending on the scope it resides in:

  • Attribute wildcard ("@"): represents an attribute, a block, or an object element
  • Expression wildcard ("$"): represents an expression or a place that a string is accepted (i.e. as a block type, block label)

The wildcards are followed by a name. Each wildcard with the same name must match the same node/string, excluding "_". Example:

$x.$_ = $x # assignment of self to a field in self

If "*" is before the name, it will match any number of nodes. Example:

[$*_] # any number of elements in a tuple

resource foo "name" {
    @*_  # any number of attributes/blocks inside the resource block body
}

Example

$ hclgrep 'dynamic $_ {@*_}' main.tf            # Grep dynamic blocks used in Terraform config
$ hclgrep 'var.$_[count.index]' main.tf         # Grep potential mis-used "count" in Terraform config

Limitation

  • The any expression wildcard ($*) doesn't work inside a traversal.
  • The expression wildcard doesn't work in place of traverse index. E.g. a[$_] doesn't match a[1], as the index is a cty.Value.
  • The expression wildcard doesn't work in place of unary/binary expression, as it compares by implementation and type (cty.Type).
You might also like...
Easysearch - Easy Full-Text Search Engine in golang
Easysearch - Easy Full-Text Search Engine in golang

Easy Full-Text Search Engine Overview EasySearch是一个分布式的全文检索搜索引擎,同时支持内存检索与磁盘检索,并针

This script search print debug from PHP code.

go-php-print-debug This script search print debug from PHP code. Checking "print", "print_r", "var_dump", "var_export", "echo" as print debug. Exclude

Gh-s - Search github repositories interactively
Gh-s - Search github repositories interactively

search github repositories interactively Installation • Usage • Feedback Search

Gh-i - Search your github issues interactively
Gh-i - Search your github issues interactively

search your github issues interactively Installation • Usage • Feedback Search G

A go (golang) library to search videos in YouTube.

YT Search A go (golang) library to search videos in YouTube. Installation go get github.com/AnjanaMadu/YTSearch Usage package main import ( "fmt"

How to get a Go / Golang app using the Gin web framework running natively on Windows Azure App Service WITHOUT using a Docker container

Go on Azure App Service View the running app - https://go-azure-appservice.azurewebsites.net 😎 This is an example repo of how to get a Go / Golang a

Stop using install.sh! Start using install.yml! DAJE...

Daje - A general purpose Ansible dotfiles installer Configure one time and run everywhere. What is this I've never liked using a big bash script to in

Supporting your devops by shortening your strings using common abbreviations and clever guesswork

abbreviate Shorten your strings using common abbreviations. Supported by Tidelift Motivation This tool comes out of a frustration of the name of resou

Copy files and artifacts via SSH using a binary, docker or Drone CI.

drone-scp Copy files and artifacts via SSH using a binary, docker or Drone CI. Feature Support routines. Support wildcard pattern on source list. Supp

Owner
magodo
magodo
Syntax-aware grep for PHP code.

phpgrep Syntax-aware grep for PHP code. This repository is used for the library and command-line tool development. A good source for additional utilit

Iskander (Alex) Sharipov 216 Dec 30, 2022
Fast, Docker-ready image processing server written in Go and libvips, with Thumbor URL syntax

Imagor Imagor is a fast, Docker-ready image processing server written in Go. Imagor uses one of the most efficient image processing library libvips (w

Adrian Shum 2.7k Dec 30, 2022
Tree style (files) explorer for p9p acme.

xplor, a tree-style (file) explorer for (plan9port) Acme screenshot, regular screenshot, monospaced Xplor is written for Acme, the Plan 9 text editing

Shingirai Chanakira 0 Nov 25, 2021
Beagle is a CLI written in Go to search for an specific username across the Internet.

Beagle __ \,--------/_/'--o Use beagle with /_ ___ /~" responsibility. /_/_/ /_/_/ ^^^^^^^^^^^^^^^^^^ Beagle is a CLI w

Daniel 28 Nov 16, 2022
A kubectl plugin for finding decoded secret data with productive search flags.

kubectl-secret-data What is it? This is a kubectl plugin for finding decoded secret data. Since kubectl only outputs base64-encoded secrets, it makes

Keisuke Umegaki 39 Dec 2, 2022
Mattermost outline plugin allows you to search your teams documents.

mattermost-plugin-outline Mattermost Outline plugin allows you to search your teams documents. Installation In Mattermost 5.16 and later, this plugin

Lujeni 11 Dec 7, 2022
A kubectl plugin for finding decoded secret data with productive search flags.

kubectl-secret-data What is it? This is a kubectl plugin for finding decoded secret data. Since kubectl outputs base64-encoded secrets basically, it m

Keisuke Umegaki 39 Dec 2, 2022
Search Kubernetes Ingress resources.

kubectl-ingress-search Search Ingress resources. Installation Download from Releases page. cp kubectl-ingress-search /usr/local/bin/ use kubectl-ingre

Huang Huang 0 Nov 7, 2021
Turn a heterogeneous pile of text docs into a single web page with good search.

Codex Codex turns an unstructured pile of heterogeneous documents into a single interactive web document. Your input documents maybe in markdown, TeX,

Amir Kadivar 4 Jan 2, 2022