github.com/octohelm/cuekit@v0.0.0-20240424021256-e7df8d743066/internal/gomod/util.go (about)

     1  package gomod
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/pkg/errors"
     9  	"golang.org/x/mod/module"
    10  
    11  	"github.com/octohelm/cuekit/internal/gomod/internal/cmd/go/internals/cfg"
    12  	"github.com/octohelm/cuekit/internal/gomod/internal/cmd/go/internals/modfetch"
    13  	"github.com/octohelm/cuekit/internal/gomod/internal/cmd/go/internals/modload"
    14  	"github.com/octohelm/cuekit/internal/gomod/internal/cmd/go/internals/vcs"
    15  	"github.com/octohelm/cuekit/internal/gomod/internal/cmd/go/internals/web"
    16  )
    17  
    18  type Module struct {
    19  	Path    string
    20  	Version string
    21  	Error   string
    22  	Dir     string
    23  	Sum     string
    24  }
    25  
    26  func envOr(key, def string) string {
    27  	val := cfg.Getenv(key)
    28  	if val == "" {
    29  		val = def
    30  	}
    31  	return val
    32  }
    33  
    34  func init() {
    35  	cfg.GOPROXY = envOr("GOPROXY", "https://proxy.golang.org,direct")
    36  	cfg.GOSUMDB = envOr("GOSUMDB", "sum.golang.org")
    37  
    38  	cfg.CmdName = "get"
    39  
    40  	userCacheDir, err := os.UserCacheDir()
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	cfg.GOMODCACHE = filepath.Join(userCacheDir, "cue/modcache")
    45  }
    46  
    47  func RepoRootForImportPath(importPath string) (string, error) {
    48  	r, err := vcs.RepoRootForImportPath(importPath, vcs.IgnoreMod, web.DefaultSecurity)
    49  	if err != nil {
    50  		return "", err
    51  	}
    52  	return r.Root, nil
    53  }
    54  
    55  // Get Module
    56  func Get(ctx context.Context, path string, version string, verbose bool) *Module {
    57  	mv := module.Version{Path: path, Version: version}
    58  	p, err := modfetch.DownloadDir(ctx, mv)
    59  	if err == nil {
    60  		// found in cache
    61  		return &Module{
    62  			Path:    mv.Path,
    63  			Version: mv.Version,
    64  			Dir:     p,
    65  			Sum:     modfetch.Sum(ctx, mv),
    66  		}
    67  	}
    68  
    69  	modload.ForceUseModules = true
    70  
    71  	cfg.BuildX = verbose
    72  
    73  	if version == "" {
    74  		version = "latest"
    75  	}
    76  
    77  	requestPath := path + "@" + version
    78  
    79  	found, err := modload.ListModulesOnly(ctx, []string{requestPath}, modload.ListVersions)
    80  	if err != nil {
    81  		panic(errors.Wrapf(err, "list %s failed", requestPath))
    82  	}
    83  	if len(found) > 0 {
    84  		info := found[0]
    85  
    86  		m := &Module{
    87  			Path:    info.Path,
    88  			Version: info.Version,
    89  		}
    90  
    91  		if info.Error != nil {
    92  			m.Error = info.Error.Err
    93  		} else {
    94  			m.Dir = info.Dir
    95  			m.Sum = modfetch.Sum(ctx, module.Version{Path: m.Path, Version: m.Version})
    96  		}
    97  		return m
    98  	}
    99  	return nil
   100  }
   101  
   102  // Download Module
   103  func Download(ctx context.Context, m *Module) {
   104  	mv := module.Version{Path: m.Path, Version: m.Version}
   105  	dir, err := modfetch.DownloadDir(ctx, mv)
   106  	if err == nil {
   107  		// found in cache
   108  		m.Dir = dir
   109  		m.Sum = modfetch.Sum(ctx, module.Version{Path: m.Path, Version: m.Version})
   110  		return
   111  	}
   112  
   113  	dir, err = modfetch.Download(ctx, mv)
   114  	if err != nil {
   115  		m.Error = err.Error()
   116  		return
   117  	}
   118  	m.Dir = dir
   119  	m.Sum = modfetch.Sum(ctx, module.Version{Path: m.Path, Version: m.Version})
   120  }
   121  
   122  func ListVersion(ctx context.Context, path string) ([]string, error) {
   123  	found, err := modload.ListModulesOnly(ctx, []string{path}, modload.ListVersions)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	if len(found) > 0 {
   128  		info := found[0]
   129  		if len(info.Versions) > 0 {
   130  			return info.Versions, nil
   131  		}
   132  
   133  		m := Get(ctx, info.Path, "latest", false)
   134  		if m.Error != "" {
   135  			return nil, errors.New(m.Error)
   136  		}
   137  		return []string{m.Version}, nil
   138  	}
   139  	return nil, errors.New("no versions")
   140  }