Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 15, 2023
1 parent ae08073 commit 424a86f
Show file tree
Hide file tree
Showing 21 changed files with 472 additions and 226 deletions.
4 changes: 3 additions & 1 deletion cache/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/config"

"github.com/gohugoio/hugo/helpers"

Expand Down Expand Up @@ -333,7 +334,8 @@ func NewCaches(p *helpers.PathSpec) (Caches, error) {
dcfg = c
} else {
var err error
dcfg, err = DecodeConfig(p.Fs.Source, p.Cfg)
// TODO1
dcfg, err = DecodeConfig(p.Fs.Source, config.BaseConfig{}, nil) // TODO1 fixme.
if err != nil {
return nil, err
}
Expand Down
21 changes: 6 additions & 15 deletions cache/filecache/filecache_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@ import (
"time"

"github.com/gohugoio/hugo/common/maps"

"github.com/gohugoio/hugo/config"

"github.com/gohugoio/hugo/helpers"

"errors"

"github.com/mitchellh/mapstructure"
"github.com/spf13/afero"
)

const (
cachesConfigKey = "caches"

resourcesGenDir = ":resourceDir/_gen"
cacheDirProject = ":cacheDir/:project"
)
Expand Down Expand Up @@ -131,7 +126,7 @@ func (f Caches) GetResourceCache() *Cache {
return f[cacheKeyGetResource]
}

func DecodeConfig(fs afero.Fs, cfg config.Provider) (Configs, error) {
func DecodeConfig(fs afero.Fs, bcfg config.BaseConfig, m map[string]any) (Configs, error) {
c := make(Configs)
valid := make(map[string]bool)
// Add defaults
Expand All @@ -140,8 +135,6 @@ func DecodeConfig(fs afero.Fs, cfg config.Provider) (Configs, error) {
valid[k] = true
}

m := cfg.GetStringMap(cachesConfigKey)

_, isOsFs := fs.(*afero.OsFs)

for k, v := range m {
Expand Down Expand Up @@ -178,7 +171,7 @@ func DecodeConfig(fs afero.Fs, cfg config.Provider) (Configs, error) {
}

// This is a very old flag in Hugo, but we need to respect it.
disabled := cfg.GetBool("ignoreCache")
disabled := false // TODO1 cfg.GetBool("ignoreCache")

for k, v := range c {
dir := filepath.ToSlash(filepath.Clean(v.Dir))
Expand All @@ -187,7 +180,7 @@ func DecodeConfig(fs afero.Fs, cfg config.Provider) (Configs, error) {

for i, part := range parts {
if strings.HasPrefix(part, ":") {
resolved, isResource, err := resolveDirPlaceholder(fs, cfg, part)
resolved, isResource, err := resolveDirPlaceholder(fs, bcfg, part)
if err != nil {
return c, err
}
Expand Down Expand Up @@ -238,17 +231,15 @@ func DecodeConfig(fs afero.Fs, cfg config.Provider) (Configs, error) {
}

// Resolves :resourceDir => /myproject/resources etc., :cacheDir => ...
func resolveDirPlaceholder(fs afero.Fs, cfg config.Provider, placeholder string) (cacheDir string, isResource bool, err error) {
workingDir := cfg.GetString("workingDir")
func resolveDirPlaceholder(fs afero.Fs, bcfg config.BaseConfig, placeholder string) (cacheDir string, isResource bool, err error) {

switch strings.ToLower(placeholder) {
case ":resourcedir":
return "", true, nil
case ":cachedir":
d, err := helpers.GetCacheDir(fs, cfg)
return d, false, err
return bcfg.CacheDir, false, nil
case ":project":
return filepath.Base(workingDir), false, nil
return filepath.Base(bcfg.WorkingDir), false, nil
}

return "", false, fmt.Errorf("%q is not a valid placeholder (valid values are :cacheDir or :resourceDir)", placeholder)
Expand Down
12 changes: 8 additions & 4 deletions cache/filecache/filecache_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ dir = "/path/to/c4"
cfg, err := config.FromConfigString(configStr, "toml")
c.Assert(err, qt.IsNil)
fs := afero.NewMemMapFs()
decoded, err := DecodeConfig(fs, cfg)
m := cfg.GetStringMap("caches")
decoded, err := DecodeConfig(fs, config.BaseConfig{}, m)
c.Assert(err, qt.IsNil)

c.Assert(len(decoded), qt.Equals, 6)
Expand Down Expand Up @@ -106,7 +107,8 @@ dir = "/path/to/c4"
cfg, err := config.FromConfigString(configStr, "toml")
c.Assert(err, qt.IsNil)
fs := afero.NewMemMapFs()
decoded, err := DecodeConfig(fs, cfg)
m := cfg.GetStringMap("caches")
decoded, err := DecodeConfig(fs, config.BaseConfig{}, m)
c.Assert(err, qt.IsNil)

c.Assert(len(decoded), qt.Equals, 6)
Expand All @@ -131,7 +133,8 @@ func TestDecodeConfigDefault(t *testing.T) {

fs := afero.NewMemMapFs()

decoded, err := DecodeConfig(fs, cfg)
m := cfg.GetStringMap("caches")
decoded, err := DecodeConfig(fs, config.BaseConfig{}, m)

c.Assert(err, qt.IsNil)

Expand Down Expand Up @@ -179,7 +182,8 @@ dir = "/"
c.Assert(err, qt.IsNil)
fs := afero.NewMemMapFs()

_, err = DecodeConfig(fs, cfg)
m := cfg.GetStringMap("caches")
_, err := DecodeConfig(fs, config.BaseConfig{}, m)
c.Assert(err, qt.Not(qt.IsNil))
}

Expand Down
10 changes: 6 additions & 4 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func (c *commandeer) loadConfig() error {
if configPath == "" {
configPath = dir
}
config, configFiles, err := hugolib.LoadConfig(
res, err := hugolib.LoadConfig(
hugolib.ConfigSourceDescriptor{
Fs: sourceFs,
Logger: c.logger,
Expand All @@ -364,11 +364,11 @@ func (c *commandeer) loadConfig() error {
// Just make it a warning.
c.logger.Warnln(err)
}
} else if c.mustHaveConfigFile && len(configFiles) == 0 {
} else if c.mustHaveConfigFile && len(res.ConfigFiles) == 0 {
return hugolib.ErrNoConfigFile
}

c.configFiles = configFiles
c.configFiles = res.ConfigFiles

var ok bool
loc := time.Local
Expand All @@ -395,6 +395,8 @@ func (c *commandeer) loadConfig() error {
}
}

config := res.Cfg

logger, err := c.createLogger(config)
if err != nil {
return err
Expand Down Expand Up @@ -516,7 +518,7 @@ func (c *commandeer) loadConfig() error {
return err
}

cacheDir, err := helpers.GetCacheDir(sourceFs, config)
cacheDir, err := helpers.GetCacheDir(sourceFs, config.GetString("cacheDir"))
if err != nil {
return err
}
Expand Down
108 changes: 100 additions & 8 deletions common/maps/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,104 @@ import (
// Params is a map where all keys are lower case.
type Params map[string]any

// Get does a lower case and nested search in this map.
// KeyParams is an utility struct for the WalkParams method.
type KeyParams struct {
Key string
Params Params
}

// GetNested does a lower case and nested search in this map.
// It will return nil if none found.
func (p Params) Get(indices ...string) any {
// Make all of these methods internal somehow.
func (p Params) GetNested(indices ...string) any {
v, _, _ := getNested(p, indices)
return v
}

func (p Params) GetString(key string) string {
v, _ := p.get(key)
return cast.ToString(v)
}

func (p Params) GetInt(key string) int {
v, _ := p.get(key)
return cast.ToInt(v)
}

func (p Params) GetBool(key string) bool {
v, _ := p.get(key)
return cast.ToBool(v)
}

func (p Params) GetParams(key string) Params {
v, found := p.get(key)
if !found {
return nil
}
return ToStringMap(v)
}

func (p Params) GetStringMap(key string) map[string]any {
v, found := p.get(key)
if !found {
return nil
}
return ToStringMap(v)
}

func (p Params) GetStringMapString(key string) map[string]string {
v, found := p.get(key)
if !found {
return nil
}
return ToStringMapString(v)
}

func (p Params) GetStringSlice(key string) []string {
v, found := p.get(key)
if !found {
return nil
}
return cast.ToStringSlice(v)
}

func (p Params) Get(key string) any {
v, _ := p.get(key)
return v
}

func (p Params) get(key string) (any, bool) {
v, found := p[p.cleanKey(key)]
return v, found
}

func (p Params) SetDefaults(params Params) {
panic("not supported")
}

func (p Params) WalkParams(walkFn func(params ...KeyParams) bool) {
panic("not supported")
}

func (p Params) IsSet(key string) bool {
_, found := p[p.cleanKey(key)]
return found
}

func (p Params) cleanKey(key string) string {
if strings.Contains(key, ".") {
panic(fmt.Sprintf("Invalid key: %q, dot nesting not supported", key))
}
return strings.ToLower(key)
}

func (p Params) Set(key string, value any) {
p[p.cleanKey(key)] = value
}

// Set overwrites values in p with values in pp for common or new keys.
// This is done recursively.
func (p Params) Set(pp Params) {
func (p Params) SetParams(pp Params) {
for k, v := range pp {
vv, found := p[k]
if !found {
Expand All @@ -41,7 +129,7 @@ func (p Params) Set(pp Params) {
switch vvv := vv.(type) {
case Params:
if pv, ok := v.(Params); ok {
vvv.Set(pv)
vvv.SetParams(pv)
} else {
p[k] = v
}
Expand Down Expand Up @@ -72,8 +160,8 @@ func (p Params) IsZero() bool {

// Merge transfers values from pp to p for new keys.
// This is done recursively.
func (p Params) Merge(pp Params) {
p.merge("", pp)
func (p Params) Merge(s string, pp any) {
p.merge(ParamsMergeStrategy(s), pp.(Params)) // TODO1
}

// MergeRoot transfers values from pp to p for new keys where p is the
Expand Down Expand Up @@ -133,7 +221,11 @@ func (p Params) DeleteMergeStrategy() bool {
return false
}

func (p Params) SetDefaultMergeStrategy(s ParamsMergeStrategy) {
func (p Params) SetDefaultMergeStrategy() {
panic("not supported")
}

func (p Params) SetMergeStrategy(s ParamsMergeStrategy) {
switch s {
case ParamsMergeStrategyDeep, ParamsMergeStrategyNone, ParamsMergeStrategyShallow:
default:
Expand Down Expand Up @@ -187,7 +279,7 @@ func GetNestedParam(keyStr, separator string, candidates ...Params) (any, error)

keySegments := strings.Split(keyStr, separator)
for _, m := range candidates {
if v := m.Get(keySegments...); v != nil {
if v := m.GetNested(keySegments...); v != nil {
return v, nil
}
}
Expand Down
16 changes: 8 additions & 8 deletions common/maps/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestParamsSetAndMerge(t *testing.T) {

p1, p2 := createParamsPair()

p1.Set(p2)
p1.SetParams(p2)

c.Assert(p1, qt.DeepEquals, Params{
"a": "abv",
Expand All @@ -97,7 +97,7 @@ func TestParamsSetAndMerge(t *testing.T) {

p1, p2 = createParamsPair()

p1.Merge(p2)
p1.Merge("", p2)

// Default is to do a shallow merge.
c.Assert(p1, qt.DeepEquals, Params{
Expand All @@ -111,8 +111,8 @@ func TestParamsSetAndMerge(t *testing.T) {
})

p1, p2 = createParamsPair()
p1.SetDefaultMergeStrategy(ParamsMergeStrategyNone)
p1.Merge(p2)
p1.SetMergeStrategy(ParamsMergeStrategyNone)
p1.Merge("", p2)
p1.DeleteMergeStrategy()

c.Assert(p1, qt.DeepEquals, Params{
Expand All @@ -125,8 +125,8 @@ func TestParamsSetAndMerge(t *testing.T) {
})

p1, p2 = createParamsPair()
p1.SetDefaultMergeStrategy(ParamsMergeStrategyShallow)
p1.Merge(p2)
p1.SetMergeStrategy(ParamsMergeStrategyShallow)
p1.Merge("", p2)
p1.DeleteMergeStrategy()

c.Assert(p1, qt.DeepEquals, Params{
Expand All @@ -140,8 +140,8 @@ func TestParamsSetAndMerge(t *testing.T) {
})

p1, p2 = createParamsPair()
p1.SetDefaultMergeStrategy(ParamsMergeStrategyDeep)
p1.Merge(p2)
p1.SetMergeStrategy(ParamsMergeStrategyDeep)
p1.Merge("", p2)
p1.DeleteMergeStrategy()

c.Assert(p1, qt.DeepEquals, Params{
Expand Down
Loading

0 comments on commit 424a86f

Please sign in to comment.