0

I'm going to set HCL fields in golang. Here the example.

package main

import (
    "log"
    "os"

    "github.com/hashicorp/hcl"
)

const (
    EXAMPLE_HCL = `config = "/etc/test.conf"`
)

type HCLConfig struct {
    ConfigFile          string     `hcl:"config"`
}

func main() {
    cfg := &HCLConfig{}

    hclTree, err := hcl.Parse(EXAMPLE_HCL)
    if err != nil {
        os.Exit(1)
    }

    // how to modify tree to override config field

    if err := hcl.DecodeObject(&cfg, hclTree); err != nil {
        os.Exit(1)
    }

    log.Printf("%+v\n", cfg)
}

It's possible to override config field after called a function hcl.DecodeObject.

But I'm going to override that field before calling hcl.DecodeObject.

Is there some way to modify HCL tree?

cdevman
  • 119
  • 2
  • 14
  • Any particular reason to not just set the field after decoding? – Peter Apr 23 '18 at 10:55
  • Yes, I have some reason about that. For example, I'm going to check validation of HCL options before passing them to real structure fields. – cdevman Apr 23 '18 at 12:22
  • I can implement it using the [following link](https://github.com/kvz/json2hcl/blob/master/main.go) – cdevman Apr 24 '18 at 07:32

0 Answers0