modules: Improve "hugo mod clean"

* Only clean project modules
* Optional glob pattern of module paths to clean

Closes #6907
This commit is contained in:
Bjørn Erik Pedersen 2020-02-19 16:59:54 +01:00
parent 0b96aba022
commit dce210ab56
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
3 changed files with 62 additions and 22 deletions

View file

@ -26,6 +26,9 @@ import (
"path/filepath"
"regexp"
"github.com/gobwas/glob"
hglob "github.com/gohugoio/hugo/hugofs/glob"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/hugofs/files"
@ -339,6 +342,38 @@ func (c *Client) Verify(clean bool) error {
return err
}
func (c *Client) Clean(pattern string) error {
mods, err := c.listGoMods()
if err != nil {
return err
}
var g glob.Glob
if pattern != "" {
var err error
g, err = hglob.GetGlob(pattern)
if err != nil {
return err
}
}
for _, m := range mods {
if m.Replace != nil || m.Main {
continue
}
if g != nil && !g.Match(m.Path) {
continue
}
_, err = hugofs.MakeReadableAndRemoveAllModulePkgDir(c.fs, m.Dir)
if err == nil {
c.logger.FEEDBACK.Printf("hugo: cleaned module cache for %q", m.Path)
}
}
return err
}
func (c *Client) runVerify() error {
return c.runGo(context.Background(), ioutil.Discard, "mod", "verify")
}