Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module.IsValid => HasModfile #40

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions gopmod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ type Module struct {
depmods []depmodInfo
}

// IsValid returns if this module exists or not.
func (p *Module) IsValid() bool {
return p != nil && p.File != nil
}

// PkgType specifies a package type.
type PkgType int

Expand Down Expand Up @@ -83,7 +78,7 @@ func (p *Module) PkgType(pkgPath string) PkgType {
}

func isPkgInMod(pkgPath, modPath string) bool {
if strings.HasPrefix(pkgPath, modPath) {
if modPath != "" && strings.HasPrefix(pkgPath, modPath) {
suffix := pkgPath[len(modPath):]
return suffix == "" || suffix[0] == '/'
}
Expand Down
20 changes: 13 additions & 7 deletions modload/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,25 @@ type Module struct {
Opt *modfile.File
}

func (p Module) IsDefault() bool {
return p.Syntax == nil
// HasModfile returns if this module exists or not.
func (p Module) HasModfile() bool {
return p.Syntax != nil
}

// Modfile returns absolute path of the module file.
func (p Module) Modfile() string {
return p.Syntax.Name
if syn := p.Syntax; syn != nil {
return syn.Name
}
return ""
}

// Root returns absolute root path of this module.
func (p Module) Root() string {
return filepath.Dir(p.Syntax.Name)
if syn := p.Syntax; syn != nil {
return filepath.Dir(syn.Name)
}
return ""
}

// Path returns the module path.
Expand Down Expand Up @@ -207,11 +214,10 @@ func hasGopExtended(opt *modfile.File) bool {

// Save saves all changes of this module.
func (p Module) Save() (err error) {
if p.IsDefault() {
modfile := p.Modfile()
if modfile == "" {
return ErrSaveDefault
}

modfile := p.Modfile()
data, err := p.Format()
if err != nil {
return
Expand Down