summaryrefslogtreecommitdiffstats
path: root/commands/mod.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-02-18 12:17:16 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-02-18 12:17:16 +0100
commit775c7c2474d8797c96c9ac529a3cd93c0c2d3514 (patch)
tree251cca5cde1de2facafa996920af646fdc92a7fd /commands/mod.go
parenteada236f87d9669885da1ff647672bb3dc6b4954 (diff)
commands: Support "hugo mod get -u ./..."
Fixes #6828
Diffstat (limited to 'commands/mod.go')
-rw-r--r--commands/mod.go61
1 files changed, 56 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()
+ }
+
+ 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
+ }
- // 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()
+ // 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...)
})
},