github.com/octohelm/cuemod@v0.9.4/pkg/modutil/util.go (about)

     1  package modutil
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"golang.org/x/mod/module"
     8  
     9  	"github.com/octohelm/cuemod/internal/cmd/go/internals/cfg"
    10  	"github.com/octohelm/cuemod/internal/cmd/go/internals/modfetch"
    11  	"github.com/octohelm/cuemod/internal/cmd/go/internals/modload"
    12  	"github.com/octohelm/cuemod/internal/cmd/go/internals/vcs"
    13  	"github.com/octohelm/cuemod/internal/cmd/go/internals/web"
    14  )
    15  
    16  type Module struct {
    17  	Path    string
    18  	Version string
    19  	Error   string
    20  	Dir     string
    21  	Sum     string
    22  }
    23  
    24  func envOr(key, def string) string {
    25  	val := cfg.Getenv(key)
    26  	if val == "" {
    27  		val = def
    28  	}
    29  	return val
    30  }
    31  
    32  func init() {
    33  	cfg.GOPROXY = envOr("GOPROXY", "https://proxy.golang.org,direct")
    34  	cfg.GOSUMDB = envOr("GOSUMDB", "sum.golang.org")
    35  }
    36  
    37  func RepoRootForImportPath(importPath string) (string, error) {
    38  	r, err := vcs.RepoRootForImportPath(importPath, vcs.IgnoreMod, web.DefaultSecurity)
    39  	if err != nil {
    40  		return "", err
    41  	}
    42  	return r.Root, nil
    43  }
    44  
    45  // Get Module
    46  func Get(ctx context.Context, path string, version string, verbose bool) *Module {
    47  	mv := module.Version{Path: path, Version: version}
    48  	p, err := modfetch.DownloadDir(ctx, mv)
    49  	if err == nil {
    50  		// found in cache
    51  		return &Module{
    52  			Path:    mv.Path,
    53  			Version: mv.Version,
    54  			Dir:     p,
    55  			Sum:     modfetch.Sum(ctx, mv),
    56  		}
    57  	}
    58  
    59  	modload.ForceUseModules = true
    60  	cfg.BuildX = verbose
    61  
    62  	requestPath := path + "@" + version
    63  	if strings.HasSuffix(path, ".v3") {
    64  		requestPath = path
    65  	}
    66  
    67  	found, err := modload.ListModules(ctx, []string{requestPath}, modload.ListVersions, "")
    68  	if err != nil {
    69  		panic(err)
    70  	}
    71  	if len(found) > 0 {
    72  		info := found[0]
    73  
    74  		m := &Module{
    75  			Path:    info.Path,
    76  			Version: info.Version,
    77  		}
    78  
    79  		if info.Error != nil {
    80  			m.Error = info.Error.Err
    81  		} else {
    82  			m.Dir = info.Dir
    83  			m.Sum = modfetch.Sum(ctx, module.Version{Path: m.Path, Version: m.Version})
    84  		}
    85  		return m
    86  	}
    87  	return nil
    88  }
    89  
    90  // Download Module
    91  func Download(ctx context.Context, m *Module) {
    92  	mv := module.Version{Path: m.Path, Version: m.Version}
    93  	dir, err := modfetch.DownloadDir(ctx, mv)
    94  	if err == nil {
    95  		// found in cache
    96  		m.Dir = dir
    97  		m.Sum = modfetch.Sum(ctx, module.Version{Path: m.Path, Version: m.Version})
    98  		return
    99  	}
   100  
   101  	dir, err = modfetch.Download(ctx, mv)
   102  	if err != nil {
   103  		m.Error = err.Error()
   104  		return
   105  	}
   106  	m.Dir = dir
   107  	m.Sum = modfetch.Sum(ctx, module.Version{Path: m.Path, Version: m.Version})
   108  }