URI: 
       commands: Add "hugo config mounts" command - hugo - [fork] hugo port for 9front
  HTML git clone git@git.drkhsh.at/hugo.git
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
   DIR commit d7c233afee6a16b1947f60b7e5450e40612997bb
   DIR parent 45ee8a7a52213bf394c7f41a72be78084ddc789a
  HTML Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
       Date:   Wed, 31 Jul 2019 10:31:26 +0200
       
       commands: Add "hugo config mounts" command
       
       This prints the effective file mounts in a project.
       
       Fixes #6144
       
       Diffstat:
         M commands/config.go                  |      66 ++++++++++++++++++++++++++++++-
         M hugolib/config.go                   |       2 +-
         M hugolib/filesystems/basefs.go       |      13 ++++++-------
         M hugolib/filesystems/basefs_test.go  |       2 +-
         M modules/collect.go                  |       4 ++--
       
       5 files changed, 74 insertions(+), 13 deletions(-)
       ---
   DIR diff --git a/commands/config.go b/commands/config.go
       @@ -14,11 +14,18 @@
        package commands
        
        import (
       +        "encoding/json"
       +        "os"
                "reflect"
                "regexp"
                "sort"
                "strings"
        
       +        "github.com/gohugoio/hugo/parser"
       +        "github.com/gohugoio/hugo/parser/metadecoders"
       +
       +        "github.com/gohugoio/hugo/modules"
       +
                "github.com/spf13/cobra"
                jww "github.com/spf13/jwalterweatherman"
                "github.com/spf13/viper"
       @@ -40,14 +47,37 @@ func newConfigCmd() *configCmd {
                        RunE:  cc.printConfig,
                })
        
       -        cc.cmd.Flags().StringVarP(&cc.source, "source", "s", "", "filesystem path to read files relative from")
       +        cc.cmd.PersistentFlags().StringVarP(&cc.source, "source", "s", "", "filesystem path to read files relative from")
       +
       +        printMountsCmd := &cobra.Command{
       +                Use:   "mounts",
       +                Short: "Print the configured file mounts",
       +                RunE:  cc.printMounts,
       +        }
       +
       +        cc.cmd.AddCommand(printMountsCmd)
        
                return cc
        }
        
       -func (c *configCmd) printConfig(cmd *cobra.Command, args []string) error {
       +func (c *configCmd) printMounts(cmd *cobra.Command, args []string) error {
                cfg, err := initializeConfig(true, false, &c.hugoBuilderCommon, c, nil)
       +        if err != nil {
       +                return err
       +        }
        
       +        allModules := cfg.Cfg.Get("allmodules").(modules.Modules)
       +
       +        for _, m := range allModules {
       +                if err := parser.InterfaceToConfig(&modMounts{m: m}, metadecoders.JSON, os.Stdout); err != nil {
       +                        return err
       +                }
       +        }
       +        return nil
       +}
       +
       +func (c *configCmd) printConfig(cmd *cobra.Command, args []string) error {
       +        cfg, err := initializeConfig(true, false, &c.hugoBuilderCommon, c, nil)
                if err != nil {
                        return err
                }
       @@ -83,3 +113,35 @@ func (c *configCmd) printConfig(cmd *cobra.Command, args []string) error {
        
                return nil
        }
       +
       +type modMounts struct {
       +        m modules.Module
       +}
       +
       +type modMount struct {
       +        Source string `json:"source"`
       +        Target string `json:"target"`
       +        Lang   string `json:"lang,omitempty"`
       +}
       +
       +func (m *modMounts) MarshalJSON() ([]byte, error) {
       +        var mounts []modMount
       +
       +        for _, mount := range m.m.Mounts() {
       +                mounts = append(mounts, modMount{
       +                        Source: mount.Source,
       +                        Target: mount.Target,
       +                        Lang:   mount.Lang,
       +                })
       +        }
       +
       +        return json.Marshal(&struct {
       +                Path   string     `json:"path"`
       +                Dir    string     `json:"dir"`
       +                Mounts []modMount `json:"mounts"`
       +        }{
       +                Path:   m.m.Path(),
       +                Dir:    m.m.Dir(),
       +                Mounts: mounts,
       +        })
       +}
   DIR diff --git a/hugolib/config.go b/hugolib/config.go
       @@ -217,7 +217,7 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
                        mods := m.ActiveModules
        
                        // Apply default project mounts.
       -                if err := modules.ApplyProjectConfigDefaults(v, mods[len(mods)-1]); err != nil {
       +                if err := modules.ApplyProjectConfigDefaults(v, mods[0]); err != nil {
                                return err
                        }
        
   DIR diff --git a/hugolib/filesystems/basefs.go b/hugolib/filesystems/basefs.go
       @@ -452,20 +452,19 @@ func (b *sourceFilesystemsBuilder) createMainOverlayFs(p *paths.Paths) (*filesys
        
                // The theme components are ordered from left to right.
                // We need to revert it to get the
       -        // overlay logic below working as expected, with the project on top (last).
       -        for i, mod := range mods {
       +        // overlay logic below working as expected, with the project on top.
       +        j := 0
       +        for i := len(mods) - 1; i >= 0; i-- {
       +                mod := mods[i]
                        dir := mod.Dir()
        
       -                if i < len(mods)-1 {
       -                        i = len(mods) - 2 - i
       -                }
       -
                        isMainProject := mod.Owner() == nil
       -                modsReversed[i] = mountsDescriptor{
       +                modsReversed[j] = mountsDescriptor{
                                Module:        mod,
                                dir:           dir,
                                isMainProject: isMainProject,
                        }
       +                j++
                }
        
                err := b.createOverlayFs(collector, modsReversed)
   DIR diff --git a/hugolib/filesystems/basefs_test.go b/hugolib/filesystems/basefs_test.go
       @@ -62,7 +62,7 @@ func initConfig(fs afero.Fs, cfg config.Provider) error {
                        return err
                }
        
       -        if err := modules.ApplyProjectConfigDefaults(cfg, moduleConfig.ActiveModules[len(moduleConfig.ActiveModules)-1]); err != nil {
       +        if err := modules.ApplyProjectConfigDefaults(cfg, moduleConfig.ActiveModules[0]); err != nil {
                        return err
                }
        
   DIR diff --git a/modules/collect.go b/modules/collect.go
       @@ -478,8 +478,8 @@ func (c *collector) collect() {
                        return
                }
        
       -        // Append the project module at the tail.
       -        c.modules = append(c.modules, projectMod)
       +        // Add the project mod on top.
       +        c.modules = append(Modules{projectMod}, c.modules...)
        
        }