github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/types/hcl/hcl.go (about)

     1  package hcl
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/hashicorp/hcl"
     7  	"github.com/lmorg/murex/config"
     8  	"github.com/lmorg/murex/lang"
     9  	"github.com/lmorg/murex/lang/stdio"
    10  	"github.com/lmorg/murex/lang/types"
    11  	"github.com/lmorg/murex/utils/json"
    12  )
    13  
    14  const typeName = "hcl"
    15  
    16  func init() {
    17  	lang.ReadIndexes[typeName] = readIndex
    18  	lang.ReadNotIndexes[typeName] = readIndex
    19  
    20  	stdio.RegisterReadArray(typeName, readArray)
    21  	stdio.RegisterReadArrayWithType(typeName, readArrayWithType)
    22  	stdio.RegisterReadMap(typeName, readMap)
    23  	stdio.RegisterWriteArray(typeName, newArrayWriter)
    24  
    25  	lang.Marshallers[typeName] = marshal
    26  	lang.Unmarshallers[typeName] = unmarshal
    27  
    28  	// These are just guessed at as I couldn't find any formally named MIMEs
    29  	lang.SetMime(typeName,
    30  		"application/hcl",
    31  		"application/x-hcl",
    32  		"text/hcl",
    33  		"text/x-hcl",
    34  	)
    35  
    36  	lang.SetFileExtensions(typeName, "hcl", "tf", "tfvars")
    37  }
    38  
    39  func readArray(ctx context.Context, read stdio.Io, callback func([]byte)) error {
    40  	// Create a marshaller function to pass to ArrayTemplate
    41  	marshaller := func(v interface{}) ([]byte, error) {
    42  		return json.Marshal(v, read.IsTTY())
    43  	}
    44  
    45  	return lang.ArrayTemplate(ctx, marshaller, hcl.Unmarshal, read, callback)
    46  }
    47  
    48  func readArrayWithType(ctx context.Context, read stdio.Io, callback func(interface{}, string)) error {
    49  	// Create a marshaller function to pass to ArrayWithTypeTemplate
    50  	marshaller := func(v interface{}) ([]byte, error) {
    51  		return json.Marshal(v, read.IsTTY())
    52  	}
    53  
    54  	return lang.ArrayWithTypeTemplate(ctx, types.Json, marshaller, hcl.Unmarshal, read, callback)
    55  }
    56  
    57  func readMap(read stdio.Io, _ *config.Config, callback func(*stdio.Map)) error {
    58  	// Create a marshaller function to pass to ArrayWithTypeTemplate
    59  	marshaller := func(v interface{}) ([]byte, error) {
    60  		return json.Marshal(v, read.IsTTY())
    61  	}
    62  
    63  	return lang.MapTemplate(types.Json, marshaller, hcl.Unmarshal, read, callback)
    64  }
    65  
    66  func readIndex(p *lang.Process, params []string) error {
    67  	var jInterface interface{}
    68  
    69  	b, err := p.Stdin.ReadAll()
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	err = hcl.Unmarshal(b, &jInterface)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	marshaller := func(iface interface{}) ([]byte, error) {
    80  		return json.Marshal(iface, p.Stdout.IsTTY())
    81  	}
    82  
    83  	return lang.IndexTemplateObject(p, params, &jInterface, marshaller)
    84  }
    85  
    86  func marshal(p *lang.Process, v interface{}) ([]byte, error) {
    87  	return json.Marshal(v, p.Stdout.IsTTY())
    88  }
    89  
    90  func unmarshal(p *lang.Process) (v interface{}, err error) {
    91  	b, err := p.Stdin.ReadAll()
    92  	if err != nil {
    93  		return
    94  	}
    95  
    96  	err = hcl.Unmarshal(b, &v)
    97  	return
    98  }