Documentation ¶
Overview ¶
Package hcledit is a Go package to edit HCL configurations. Basically, this is just a wrapper of hclwrite package which provides low-level features of generating HCL configurations. But hcledit allows you to access HCL attribute or block by jq-like query and do various manipulations.
Example ¶
package main import ( "fmt" "strings" "go.mercari.io/hcledit" ) func main() { src := ` resource "google_container_node_pool" "nodes1" { name = "nodes1" node_config { preemptible = false machine_type = "e2-medium" } timeouts { create = "30m" } } resource "google_container_node_pool" "nodes2" { name = "nodes2" node_config { preemptible = false machine_type = "e2-medium" } timeouts { create = "30m" } } ` // Read HCL contents. editor, _ := hcledit.Read(strings.NewReader(src), "") // Create new attribute on the existing block. editor.Create("resource.google_container_node_pool.*.node_config.disk_size_gb", "200") // Create new block and add some attributes. editor.Create("resource.google_container_node_pool.*.master_auth", hcledit.BlockVal()) editor.Create("resource.google_container_node_pool.*.master_auth.username", "") editor.Create("resource.google_container_node_pool.*.master_auth.password", "") // Update existing attributes. editor.Update("resource.google_container_node_pool.*.node_config.machine_type", "COS") editor.Update("resource.google_container_node_pool.*.node_config.preemptible", true) // Delete existing attribute and blocks editor.Delete("resource.google_container_node_pool.*.timeouts") fmt.Printf("%s", editor.Bytes()) }
Output: resource "google_container_node_pool" "nodes1" { name = "nodes1" node_config { preemptible = true machine_type = "COS" disk_size_gb = "200" } master_auth { username = "" password = "" } } resource "google_container_node_pool" "nodes2" { name = "nodes2" node_config { preemptible = true machine_type = "COS" disk_size_gb = "200" } master_auth { username = "" password = "" } }
Index ¶
- func BlockVal(labels ...string) *handler.BlockVal
- func RawVal(rawString string) *handler.RawVal
- type HCLEditor
- func (h *HCLEditor) Bytes() []byte
- func (h *HCLEditor) Create(queryStr string, value interface{}, opts ...Option) error
- func (h *HCLEditor) CustomEdit(fn func(*hclwrite.Body) error) error
- func (h *HCLEditor) Delete(queryStr string, opts ...Option) error
- func (h *HCLEditor) OverWriteFile() error
- func (h *HCLEditor) Read(queryStr string, opts ...Option) (map[string]interface{}, error)
- func (h *HCLEditor) Update(queryStr string, value interface{}, opts ...Option) error
- func (h *HCLEditor) Write(w io.Writer) error
- func (h *HCLEditor) WriteFile(path string) error
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type HCLEditor ¶
type HCLEditor struct {
// contains filtered or unexported fields
}
HCLEditor implements an editor of HCL configuration.
func (*HCLEditor) Bytes ¶ added in v0.0.9
Bytes returns a buffer containing the source code resulting from the tokens underlying the receiving file. If any updates have been made via the AST API, these will be reflected in the result.
func (*HCLEditor) Create ¶
Create creates attributes and blocks matched with the given query with the given value. The value can be any type and it's transformed into HCL type inside.
func (*HCLEditor) CustomEdit ¶ added in v0.0.9
CustomEdit executes a custom function on the underlying file
func (*HCLEditor) Delete ¶
Delete deletes attributes and blocks matched with the given query.
It returns error if it does not match any key.
func (*HCLEditor) OverWriteFile ¶ added in v0.0.9
OverWriteFile writes the new contents to the file that has first been read via ReadFile.
func (*HCLEditor) Read ¶
Read returns attributes and blocks matched with the given query. The results are map of mached key and its value.
It returns error if it does not match any key.
func (*HCLEditor) Update ¶
Update replaces attributes and blocks which matched with its key and given query with the given value. The value can be any type and it's transformed into HCL type inside.
By default, it returns error if the does not matched with any key. You must create value before update.
type Option ¶
type Option func(*option)
Option configures specific behavior for specific HCLEditor operations. TODO(slewiskelly): Not all options are applicable to all operations, maybe options should be specific to each kind of operation?
func WithComment ¶
WithComment provides comment to put together when creating.
func WithQuerySeparator ¶ added in v0.0.15
This sets a separator for queryStr in HCLEditor funcs. The default separator is ".".
func WithReadFallbackToRawString ¶ added in v0.0.10
func WithReadFallbackToRawString() Option
This provides a fallback to return the raw string of the value if we could not parse it. If this option is provided to HCLEditor.Read(), the error return value will signal fallback occurred.