github.com/octohelm/cuemod@v0.9.4/pkg/cuex/build.go (about)

     1  package cuex
     2  
     3  import (
     4  	"cuelang.org/go/cue/build"
     5  	cueparser "cuelang.org/go/cue/parser"
     6  )
     7  
     8  func InstanceFromRaw(src []byte) (*build.Instance, error) {
     9  	inst := build.NewContext().NewInstance("", nil)
    10  	f, err := cueparser.ParseFile("main.cue", src)
    11  	if err != nil {
    12  		return nil, err
    13  	}
    14  	if err := inst.AddSyntax(f); err != nil {
    15  		return nil, err
    16  	}
    17  	return inst, nil
    18  }
    19  
    20  func InstanceFromTemplateAndOverwrites(template []byte, overwrites []byte) (*build.Instance, error) {
    21  	t, err := InstanceFromRaw(template)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	if overwrites == nil {
    27  		overwrites = []byte(`
    28  import t "t"
    29  t & {}
    30  `)
    31  	}
    32  
    33  	m, err := InstanceFromRaw(overwrites)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	t.ImportPath = "t"
    39  
    40  	m.Imports = append(m.Imports, t)
    41  
    42  	return m, nil
    43  }