github.com/octohelm/cuekit@v0.0.0-20240424021256-e7df8d743066/pkg/mod/modmem/module.go (about)

     1  package modmem
     2  
     3  import (
     4  	"context"
     5  	"cuelang.org/go/mod/module"
     6  	"github.com/octohelm/cuekit/pkg/mod/modfile"
     7  	"github.com/octohelm/unifs/pkg/filesystem"
     8  )
     9  
    10  func NewModule(path string, version string, commitSource func(ctx context.Context, fsys filesystem.FileSystem) error) (*Module, error) {
    11  	v, err := module.NewVersion(path, version)
    12  	if err != nil {
    13  		return nil, err
    14  	}
    15  
    16  	m := &Module{}
    17  	m.Module = v.Path()
    18  	m.Version = v.Version()
    19  	m.Dir = "."
    20  
    21  	fsys := filesystem.NewMemFS()
    22  
    23  	ctx := context.Background()
    24  
    25  	if err := commitSource(ctx, fsys); err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	if err := filesystem.MkdirAll(ctx, fsys, "cue.mod"); err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	data, err := m.File.Format()
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	if err := filesystem.Write(context.Background(), fsys, "cue.mod/module.cue", data); err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	m.FS = filesystem.AsReadDirFS(fsys)
    43  
    44  	return m, nil
    45  }
    46  
    47  type Module struct {
    48  	modfile.File
    49  	Version string
    50  	module.SourceLoc
    51  }