github.com/octohelm/cuekit@v0.0.0-20240424021256-e7df8d743066/pkg/cuecontext/instance.go (about)

     1  package cuecontext
     2  
     3  import (
     4  	"github.com/octohelm/cuekit/pkg/mod/module"
     5  	"path/filepath"
     6  
     7  	"cuelang.org/go/cue/build"
     8  	"cuelang.org/go/cue/load"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  type Instance = build.Instance
    13  
    14  func BuildInstance(c *load.Config, inputs []string) (*Instance, error) {
    15  	if len(inputs) == 0 {
    16  		files, err := module.WalkCueFile(c.Dir, ".")
    17  		if err != nil {
    18  			return nil, err
    19  		}
    20  		inputs = files
    21  	}
    22  
    23  	files := make([]string, len(inputs))
    24  
    25  	for i, f := range inputs {
    26  		if filepath.IsAbs(f) {
    27  			rel, _ := filepath.Rel(c.Dir, f)
    28  			files[i] = "./" + rel
    29  		} else {
    30  			files[i] = f
    31  		}
    32  	}
    33  
    34  	insts := load.Instances(files, c)
    35  	if len(insts) != 1 {
    36  		return nil, errors.New("invalid instance")
    37  	}
    38  
    39  	if err := insts[0].Err; err != nil {
    40  		return nil, err
    41  	}
    42  	return insts[0], nil
    43  }