Skip to content

Commit

Permalink
commands: Add --all flag to hugo mod clean
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Mar 3, 2020
1 parent 3d3fa5c commit 760a87a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions commands/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"

"github.com/gohugoio/hugo/modules"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,8 +50,11 @@ func (c *modCmd) newVerifyCmd() *cobra.Command {
return verifyCmd
}

var moduleNotFoundRe = regexp.MustCompile("module.*not found")

func (c *modCmd) newCleanCmd() *cobra.Command {
var pattern string
var all bool
cmd := &cobra.Command{
Use: "clean",
Short: "Delete the Hugo Module cache for the current project.",
Expand All @@ -62,13 +66,24 @@ Also note that if you configure a positive maxAge for the "modules" file cache,
`,
RunE: func(cmd *cobra.Command, args []string) error {
if all {
com, err := c.initConfig(false)

if err != nil && !moduleNotFoundRe.MatchString(err.Error()) {
return err
}

_, err = com.hugo().FileCaches.ModulesCache().Prune(true)
return err
}
return c.withModsClient(true, func(c *modules.Client) error {
return c.Clean(pattern)
})
},
}

cmd.Flags().StringVarP(&pattern, "pattern", "", "", `pattern matching module paths to clean (all if not set), e.g. "**hugo*"`)
cmd.Flags().BoolVarP(&all, "all", "", false, "clean entire module cache")

return cmd
}
Expand Down

0 comments on commit 760a87a

Please sign in to comment.