From dce210ab56fc885818fc5d1a084a1c3ba84e7929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 19 Feb 2020 16:59:54 +0100 Subject: modules: Improve "hugo mod clean" * Only clean project modules * Optional glob pattern of module paths to clean Closes #6907 --- modules/client.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'modules/client.go') diff --git a/modules/client.go b/modules/client.go index 601b5e109..a82300b37 100644 --- a/modules/client.go +++ b/modules/client.go @@ -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") } -- cgit v1.2.3