Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/katbyte/terrafmt
Format terraform blocks embedded in files
https://github.com/katbyte/terrafmt
formatter go golang terraform tool
Last synced: 4 days ago
JSON representation
Format terraform blocks embedded in files
- Host: GitHub
- URL: https://github.com/katbyte/terrafmt
- Owner: katbyte
- License: gpl-3.0
- Created: 2018-10-29T04:24:26.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-12-12T17:59:33.000Z (12 days ago)
- Last Synced: 2024-12-14T00:09:14.201Z (11 days ago)
- Topics: formatter, go, golang, terraform, tool
- Language: Go
- Homepage:
- Size: 30.6 MB
- Stars: 80
- Watchers: 4
- Forks: 16
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# terrafmt
![build](https://github.com/katbyte/terrafmt/actions/workflows/build.yaml/badge.svg)
![tests](https://github.com/katbyte/terrafmt/actions/workflows/test.yaml/badge.svg)
![lint](https://github.com/katbyte/terrafmt/actions/workflows/lint.yaml/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/katbyte/terrafmt)](https://goreportcard.com/report/github.com/katbyte/terrafmt)A tool for extracting or formatting [Terraform](https://www.terraform.io/docs/) configuration embedded in [provider](https://www.terraform.io/docs/providers/index.html) code
## Install
### Local Install
Use Go to install directly into your `$GOBIN` directory (e.g. `$GOPATH/bin`):
```console
go get github.com/katbyte/terrafmt
```## Usage
Information about usage and options can be found by using the `help` command:
```console
terrafmt help
```This tool can extract terraform blocks, run `terraform fmt` on the blocks and display the difference or update them in place.
The tool currently supports blocks with the following start and end lines:
|start |end |
|--------------------|----|
|```hcl |``` |
|```tf |`, |
|```terraform |`, |
|return fmt.Sprintf(`|`, |
|return fmt.Sprintf(`|`) |
|return ` |` |### Extract Terraform Blocks
Use the `blocks` command to extract blocks from a file:
![blocks](.github/images/blocks.png)
To output only the block content, separated by the null character, use the flags `--zero-terminated` or `z`.
To output the blocks using a JSON structure, use the flags `--json` or `-j`. The format is
```json
{
"block_count": 1,
"blocks": [
{
"block_number": 1,
"start_line": 4,
"end_line": 9,
"text": "..."
}
]
}
```Go [format verbs](https://golang.org/pkg/fmt/) can be escaped in the output blocks by using the flags `--fmtcompat` or `-f`.
To output the blocks using a JSON structure, use the flags `--json` or `-j`. The format is
```json
{
"block_count": 1,
"blocks": [
{
"block_number": 1,
"start_line": 4,
"end_line": 9,
"text": "..."
}
]
}
```### Show What Format Would Do
Use the `diff` command to see what would be formatted (files can also be piped in on stdin) :
![diff](.github/images/diff.png)
For code files with printf verb formatting use the `-f` switch :
![diff -f](.github/images/diff-f.png)
### Format File
Use the `fmt` command to format the blocks:
![fmt](.github/images/fmt.png)
### Format Multiple
One can use find and egrep to format multiple files:
```shell
find . | egrep "html.markdown" | sort | while read f; do terrafmt fmt -f $f; done
./website/docs/d/api_management.html.markdown: 136 lines & formatted 0/1 blocks!
./website/docs/d/api_management_api.html.markdown: 79 lines & formatted 0/1 blocks!
./website/docs/d/api_management_group.html.markdown: 46 lines & formatted 0/1 blocks!
./website/docs/d/api_management_product.html.markdown: 52 lines & formatted 0/1 blocks!
./website/docs/d/api_management_user.html.markdown: 48 lines & formatted 0/1 blocks!
./website/docs/d/app_service.html.markdown: 139 lines & formatted 0/1 blocks!
./website/docs/d/app_service_certificate.html.markdown: 54 lines & formatted 0/1 blocks!
./website/docs/d/app_service_certificate_order.html.markdown: 79 lines & formatted 0/1 blocks!
```### Upgrade Terraform in a File to 0.12
Use the `upgrade012` command to upgrade the blocks to 0.12:
![fmt](.github/images/upgrade.png)
### Exit codes
To help usage of `terrafmt` in workflows, some commands will return actionable exit codes.
If a Terraform parsing error is encountered in a block, the exit code is `2`.
If the command `diff` with the `--check` flag enabled encounters a formatting difference, it will return `4`. If a file contains both blocks with parsing errors and a formatting difference, it will combine the exit codes to return `6`. These codes can be tested using bitwise checks.
Otherwise, `terrafmt` will return `1` on an error.
## Development and Testing
This project uses [Go Modules](https://github.com/golang/go/wiki/Modules) for dependency management.
### Updating Dependencies
```console
$ go get URL
$ go mod tidy
$ go mod vendor
```### Unit Testing
```console
$ go test ./...
```### Local Install
```console
$ go install .
```