From 775c7c2474d8797c96c9ac529a3cd93c0c2d3514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 18 Feb 2020 12:17:16 +0100 Subject: [PATCH] commands: Support "hugo mod get -u ./..." Fixes #6828 --- commands/mod.go | 61 +++++++++++++++++++-- docs/content/en/hugo-modules/use-modules.md | 9 +++ 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/commands/mod.go b/commands/mod.go index 0b3e193b9..582ebfda7 100644 --- a/commands/mod.go +++ b/commands/mod.go @@ -14,7 +14,10 @@ package commands import ( + "errors" + "fmt" "os" + "path/filepath" "github.com/gohugoio/hugo/modules" "github.com/spf13/cobra" @@ -72,17 +75,65 @@ Install a specific version: Install the latest versions of all module dependencies: hugo mod get -u + hugo mod get -u ./... (recursive) Run "go help get" for more information. All flags available for "go get" is also relevant here. ` + commonUsage, RunE: func(cmd *cobra.Command, args []string) error { - return c.withModsClient(false, func(c *modules.Client) error { + // We currently just pass on the flags we get to Go and + // need to do the flag handling manually. + if len(args) == 1 && args[0] == "-h" { + return cmd.Help() + } - // We currently just pass on the flags we get to Go and - // need to do the flag handling manually. - if len(args) == 1 && args[0] == "-h" { - return cmd.Help() + var lastArg string + if len(args) != 0 { + lastArg = args[len(args)-1] + } + + if lastArg == "./..." { + args = args[:len(args)-1] + // Do a recursive update. + dirname, err := os.Getwd() + if err != nil { + return err } + + // Sanity check. We do recursive walking and want to avoid + // accidents. + if len(dirname) < 5 { + return errors.New("must not be run from the file system root") + } + + filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error { + if info.IsDir() { + return nil + } + + if info.Name() == "go.mod" { + // Found a module. + dir := filepath.Dir(path) + fmt.Println("Update module in", dir) + c.source = dir + err := c.withModsClient(false, func(c *modules.Client) error { + if len(args) == 1 && args[0] == "-h" { + return cmd.Help() + } + return c.Get(args...) + }) + if err != nil { + return err + } + + } + + return nil + }) + + return nil + } + + return c.withModsClient(false, func(c *modules.Client) error { return c.Get(args...) }) }, diff --git a/docs/content/en/hugo-modules/use-modules.md b/docs/content/en/hugo-modules/use-modules.md index 71430700a..b5f9deadd 100644 --- a/docs/content/en/hugo-modules/use-modules.md +++ b/docs/content/en/hugo-modules/use-modules.md @@ -46,6 +46,15 @@ Some examples: ```bash hugo mod get -u ``` + +### Update All Modules Recursively + +{{< new-in "0.65.0" >}} + +```bash +hugo mod get -u ./... +``` + ### Update One Module ```bash