{"id":15640364,"url":"https://github.com/katbyte/terrafmt","last_synced_at":"2025-10-04T21:09:21.252Z","repository":{"id":37850651,"uuid":"155151741","full_name":"katbyte/terrafmt","owner":"katbyte","description":"Format terraform blocks embedded in files","archived":false,"fork":false,"pushed_at":"2024-12-12T17:59:33.000Z","size":32048,"stargazers_count":80,"open_issues_count":7,"forks_count":15,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-28T15:08:43.329Z","etag":null,"topics":["formatter","go","golang","terraform","tool"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/katbyte.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-29T04:24:26.000Z","updated_at":"2024-12-12T17:59:37.000Z","dependencies_parsed_at":"2024-06-18T15:26:33.627Z","dependency_job_id":"b4c6571a-3402-4f4f-bf6f-42c229a197d3","html_url":"https://github.com/katbyte/terrafmt","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katbyte%2Fterrafmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katbyte%2Fterrafmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katbyte%2Fterrafmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katbyte%2Fterrafmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/katbyte","download_url":"https://codeload.github.com/katbyte/terrafmt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247208153,"owners_count":20901570,"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":["formatter","go","golang","terraform","tool"],"created_at":"2024-10-03T11:34:54.836Z","updated_at":"2025-10-04T21:09:16.215Z","avatar_url":"https://github.com/katbyte.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# terrafmt\n\n![build](https://github.com/katbyte/terrafmt/actions/workflows/build.yaml/badge.svg)\n![tests](https://github.com/katbyte/terrafmt/actions/workflows/test.yaml/badge.svg)\n![lint](https://github.com/katbyte/terrafmt/actions/workflows/lint.yaml/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/katbyte/terrafmt)](https://goreportcard.com/report/github.com/katbyte/terrafmt)\n\nA tool for extracting or formatting [Terraform](https://www.terraform.io/docs/) configuration embedded in [provider](https://www.terraform.io/docs/providers/index.html) code \n\n## Install\n\n### Local Install\n\nUse Go to install directly into your `$GOBIN` directory (e.g. `$GOPATH/bin`):\n\n```console\ngo get github.com/katbyte/terrafmt\n```\n\n## Usage\n\nInformation about usage and options can be found by using the `help` command:\n\n```console\nterrafmt help\n```\n\nThis tool can extract terraform blocks, run `terraform fmt` on the blocks and display the difference or update them in place.\n\nThe tool currently supports blocks with the following start and end lines:\n\n|start               |end |\n|--------------------|----|\n|```hcl              |``` |\n|```tf               |`,  |\n|```terraform        |`,  |\n|return fmt.Sprintf(`|`,  |\n|return fmt.Sprintf(`|`)  |\n|return `            |`   |\n\n### Extract Terraform Blocks\n\nUse the `blocks` command to extract blocks from a file:\n\n![blocks](.github/images/blocks.png)\n\nTo output only the block content, separated by the null character, use the flags `--zero-terminated` or `z`.\n\nTo output the blocks using a JSON structure, use the flags `--json` or `-j`. The format is\n\n```json\n{\n    \"block_count\": 1,\n    \"blocks\": [\n        {\n            \"block_number\": 1,\n            \"start_line\": 4,\n            \"end_line\": 9,\n            \"text\": \"...\"\n        }\n    ]\n}\n```\n\nGo [format verbs](https://golang.org/pkg/fmt/) can be escaped in the output blocks by using the flags `--fmtcompat` or `-f`.\n\nTo output the blocks using a JSON structure, use the flags `--json` or `-j`. The format is\n\n```json\n{\n    \"block_count\": 1,\n    \"blocks\": [\n        {\n            \"block_number\": 1,\n            \"start_line\": 4,\n            \"end_line\": 9,\n            \"text\": \"...\"\n        }\n    ]\n}\n```\n\n### Show What Format Would Do\n\nUse the `diff` command to see what would be formatted (files can also be piped in on stdin) :\n\n![diff](.github/images/diff.png) \n\nFor code files with printf verb formatting use the `-f` switch :\n\n![diff -f](.github/images/diff-f.png)\n\n### Format File\n\nUse the `fmt` command to format the blocks:\n\n![fmt](.github/images/fmt.png)\n\n### Format Multiple\n\nOne can use find and egrep to format multiple files:\n```shell\nfind . | egrep \"html.markdown\" | sort | while read f; do terrafmt fmt -f $f; done\n./website/docs/d/api_management.html.markdown: 136 lines \u0026 formatted 0/1 blocks!\n./website/docs/d/api_management_api.html.markdown: 79 lines \u0026 formatted 0/1 blocks!\n./website/docs/d/api_management_group.html.markdown: 46 lines \u0026 formatted 0/1 blocks!\n./website/docs/d/api_management_product.html.markdown: 52 lines \u0026 formatted 0/1 blocks!\n./website/docs/d/api_management_user.html.markdown: 48 lines \u0026 formatted 0/1 blocks!\n./website/docs/d/app_service.html.markdown: 139 lines \u0026 formatted 0/1 blocks!\n./website/docs/d/app_service_certificate.html.markdown: 54 lines \u0026 formatted 0/1 blocks!\n./website/docs/d/app_service_certificate_order.html.markdown: 79 lines \u0026 formatted 0/1 blocks!\n```\n\n### Upgrade Terraform in a File to 0.12\n\nUse the `upgrade012` command to upgrade the blocks to 0.12:\n\n![fmt](.github/images/upgrade.png)\n\n### Exit codes\n\nTo help usage of `terrafmt` in workflows, some commands will return actionable exit codes.\n\nIf a Terraform parsing error is encountered in a block, the exit code is `2`.\n\nIf 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.\n\nOtherwise, `terrafmt` will return `1` on an error.\n\n## Development and Testing\n\nThis project uses [Go Modules](https://github.com/golang/go/wiki/Modules) for dependency management.\n\n### Updating Dependencies\n\n```console\n$ go get URL\n$ go mod tidy\n$ go mod vendor\n```\n\n### Unit Testing\n\n```console\n$ go test ./...\n```\n\n### Local Install\n\n```console\n$ go install .\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkatbyte%2Fterrafmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkatbyte%2Fterrafmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkatbyte%2Fterrafmt/lists"}