Skip to content

Commit

Permalink
Merge pull request #40 from xushiwei/q
Browse files Browse the repository at this point in the history
Module.IsValid => HasModfile
  • Loading branch information
xushiwei authored Jan 7, 2024
2 parents 4ba66b1 + 5ae616b commit 206ed31
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
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

0 comments on commit 206ed31

Please sign in to comment.