Skip to content

Commit

Permalink
Merge pull request #29 from xushiwei/q
Browse files Browse the repository at this point in the history
gopmod.LoadFrom; modload.LoadFrom
  • Loading branch information
xushiwei authored Nov 9, 2023
2 parents d89a318 + beba30a commit 10181f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
12 changes: 10 additions & 2 deletions gopmod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ func New(mod modload.Module) *Module {
return &Module{projects: projects, depmods: depmods, Module: mod}
}

// Load loads a module from a local dir.
// If we only want to load a Go modfile, pass env parameter as nil.
// Load loads a module from a local directory.
func Load(dir string, mode mod.Mode) (*Module, error) {
mod, err := modload.Load(dir, mode)
if err != nil {
Expand All @@ -190,6 +189,15 @@ func Load(dir string, mode mod.Mode) (*Module, error) {
return New(mod), nil
}

// LoadFrom loads a module from specified gop.mod or go.mod file.
func LoadFrom(file string) (*Module, error) {
mod, err := modload.LoadFrom(file)
if err != nil {
return nil, errors.NewWith(err, `modload.LoadFrom(file)`, -2, "modload.LoadFrom", file)
}
return New(mod), nil
}

// LoadMod loads a module from a versioned module path.
// If we only want to load a Go modfile, pass env parameter as nil.
func LoadMod(mod module.Version, mode mod.Mode) (p *Module, err error) {
Expand Down
14 changes: 9 additions & 5 deletions modload/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,29 @@ func fixVersion(fixed *bool) modfile.VersionFixer {
}
}

// Load loads a module from `dir`.
// Load loads a module from specified directory.
func Load(dir string, mode mod.Mode) (p Module, err error) {
gopmod, err := mod.GOPMOD(dir, mode)
if err != nil {
err = errors.NewWith(err, `mod.GOPMOD(dir, mode)`, -2, "mod.GOPMOD", dir, mode)
return
}
return LoadFrom(gopmod)
}

data, err := os.ReadFile(gopmod)
// LoadFrom loads a module from specified gop.mod or go.mod file.
func LoadFrom(file string) (p Module, err error) {
data, err := os.ReadFile(file)
if err != nil {
err = errors.NewWith(err, `os.ReadFile(gopmod)`, -2, "os.ReadFile", gopmod)
err = errors.NewWith(err, `os.ReadFile(gopmod)`, -2, "os.ReadFile", file)
return
}

var fixed bool
fix := fixVersion(&fixed)
f, err := modfile.Parse(gopmod, data, fix)
f, err := modfile.Parse(file, data, fix)
if err != nil {
err = errors.NewWith(err, `modfile.Parse(gopmod, data, fix)`, -2, "modfile.Parse", gopmod, data, fix)
err = errors.NewWith(err, `modfile.Parse(gopmod, data, fix)`, -2, "modfile.Parse", file, data, fix)
return
}
if f.Module == nil {
Expand Down

0 comments on commit 10181f6

Please sign in to comment.