https://github.com/mercari/hcledit
Go package to edit HCL configuration
https://github.com/mercari/hcledit
Last synced: about 1 month ago
JSON representation
Go package to edit HCL configuration
- Host: GitHub
- URL: https://github.com/mercari/hcledit
- Owner: mercari
- License: mit
- Created: 2021-01-04T09:52:52.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-09-04T01:07:23.000Z (8 months ago)
- Last Synced: 2025-03-01T00:23:20.478Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 183 KB
- Stars: 53
- Watchers: 19
- Forks: 14
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- trackawesomelist - hcledit (⭐53) - Go package to edit HCL configuration (Recently Updated / [Feb 10, 2025](/content/2025/02/10/README.md))
README
# hcledit
[![workflow-test][workflow-test-badge]][workflow-test]
[![release][release-badge]][release]
[![pkg.go.dev][pkg.go.dev-badge]][pkg.go.dev]
[![license][license-badge]][license]`hcledit` is a wrapper around the [`hclwrite`](https://pkg.go.dev/github.com/hashicorp/hcl/v2/hclwrite) package that adds the ability to edit and manipulate [HCL](https://github.com/hashicorp/hcl) documents using a [`jq`](https://github.com/stedolan/jq)-like query/selector syntax.
We provide a Go package and a simple CLI application based on this package. See [`hcledit` command](cmd/hcledit/README.md).
*NOTE*: This is still under heavy development and we don't have enough documentation and we are planing to add breaking changes. Please be careful when using it.
## Install
Use `go install`:
```bash
$ go install go.mercari.io/hcledit/cmd/hcledit@latest
```## Usage
See [Go doc][pkg.go.dev].
## Examples
The following is an HCL configuration which we want to manipulate.
```hcl
resource "google_container_node_pool" "nodes1" {
name = "nodes1"node_config {
preemptible = false
machine_type = "e2-medium"
}
}
```To create a new attribute,
```go
editor, _ := hcledit.ReadFile(filename)
editor.Create("resource.google_container_node_pool.*.node_config.image_type", "COS")
editor.OverWriteFile()
``````diff
resource "google_container_node_pool" "nodes1" {
name = "nodes1"node_config {
preemptible = false
machine_type = "e2-medium"
+ image_type = "COS"
}
}
```To update the existing attribute,
```go
editor, _ := hcledit.ReadFile(filename)
editor.Update("resource.google_container_node_pool.*.node_config.machine_type", "e2-highmem-2")
editor.OverWriteFile()
``````diff
resource "google_container_node_pool" "nodes1" {
name = "nodes1"node_config {
preemptible = false
- machine_type = "e2-medium"
+ machine_type = "e2-highmem-2"
}
}
```To delete the existing attribute,
```go
editor, _ := hcledit.ReadFile(filename)
editor.Delete("resource.google_container_node_pool.*.node_config.machine_type")
editor.OverWriteFile()
``````diff
resource "google_container_node_pool" "nodes1" {
name = "nodes1"node_config {
preemptible = false
- machine_type = "e2-medium"
}
}
```## Contribution
During the active development, we unlikely accept PRs for new features but welcome bug fixes and documentation.
If you find issues, please submit an issue first.If you want to submit a PR for bug fixes or documentation, please read the [CONTRIBUTING.md](CONTRIBUTING.md) and follow the instruction beforehand.
## License
The hcledit is released under the [MIT License](LICENSE).
[workflow-test]: https://github.com/mercari/hcledit/actions?query=workflow%3ATest
[workflow-test-badge]: https://img.shields.io/github/workflow/status/mercari/hcledit/Test?label=Test&style=for-the-badge&logo=github[release]: https://github.com/mercari/hcledit/releases
[release-badge]: https://img.shields.io/github/v/release/mercari/hcledit?style=for-the-badge&logo=github[pkg.go.dev]: https://pkg.go.dev/go.mercari.io/hcledit
[pkg.go.dev-badge]: http://bit.ly/pkg-go-dev-badge[license]: LICENSE
[license-badge]: https://img.shields.io/github/license/mercari/hcledit?style=for-the-badge