Skip to content

Commit

Permalink
More work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 14, 2023
1 parent 8159464 commit 5693ed0
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 68 deletions.
6 changes: 3 additions & 3 deletions config/allconfig/allconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type Config struct {
// <docsmeta>{"refs": ["config:languages:menus"] }</docsmeta>
Menus *config.ConfigNamespace[map[string]navigation.MenuConfig, navigation.Menus] `mapstructure:"-"`

// The deployment configuration section contains for hugo deploy.
Deployment deploy.DeployConfig `mapstructure:"-"`

// Module configuration.
Module modules.Config `mapstructure:"-"`

Expand Down Expand Up @@ -142,9 +145,6 @@ type Config struct {
// Services configuration.
Services services.Config `mapstructure:"-"`

// The deployment configuration section contains for hugo deploy.
Deployment deploy.DeployConfig `mapstructure:"-"`

// User provided parameters.
// <docsmeta>{"refs": ["config:languages:params"] }</docsmeta>
Params maps.Params `mapstructure:"-"`
Expand Down
2 changes: 1 addition & 1 deletion config/allconfig/alldecoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ var allDecoderSetups = map[string]decodeWeight{
return err
},
},
"deploment": {
"deployment": {
key: "deployment",
decode: func(d decodeWeight, p decodeConfig) error {
var err error
Expand Down
12 changes: 6 additions & 6 deletions deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Deployer struct {

cfg DeployConfig

target *target // the target to deploy to
target *Target // the target to deploy to

// For tests...
summary deploySummary // summary of latest Deploy results
Expand All @@ -84,7 +84,7 @@ func New(cfg config.AllProvider, localFs afero.Fs) (*Deployer, error) {
mediaTypes := cfg.GetConfigSection("mediaTypes").(media.Types)

// Find the target to deploy to.
var tgt *target
var tgt *Target
if targetName == "" {
// Default to the first target.
tgt = dcfg.Targets[0]
Expand Down Expand Up @@ -340,14 +340,14 @@ type localFile struct {
UploadSize int64

fs afero.Fs
matcher *matcher
matcher *Matcher
md5 []byte // cache
gzipped bytes.Buffer // cached of gzipped contents if gzipping
mediaTypes media.Types
}

// newLocalFile initializes a *localFile.
func newLocalFile(fs afero.Fs, nativePath, slashpath string, m *matcher, mt media.Types) (*localFile, error) {
func newLocalFile(fs afero.Fs, nativePath, slashpath string, m *Matcher, mt media.Types) (*localFile, error) {
f, err := fs.Open(nativePath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -479,7 +479,7 @@ func knownHiddenDirectory(name string) bool {

// walkLocal walks the source directory and returns a flat list of files,
// using localFile.SlashPath as the map keys.
func walkLocal(fs afero.Fs, matchers []*matcher, include, exclude glob.Glob, mediaTypes media.Types) (map[string]*localFile, error) {
func walkLocal(fs afero.Fs, matchers []*Matcher, include, exclude glob.Glob, mediaTypes media.Types) (map[string]*localFile, error) {
retval := map[string]*localFile{}
err := afero.Walk(fs, "", func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -518,7 +518,7 @@ func walkLocal(fs afero.Fs, matchers []*matcher, include, exclude glob.Glob, med
}

// Find the first matching matcher (if any).
var m *matcher
var m *Matcher
for _, cur := range matchers {
if cur.Matches(slashpath) {
m = cur
Expand Down
18 changes: 9 additions & 9 deletions deploy/deployConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const deploymentConfigKey = "deployment"

// DeployConfig is the complete configuration for deployment.
type DeployConfig struct {
Targets []*target
Matchers []*matcher
Targets []*Target
Matchers []*Matcher
Order []string

// Usually set via flags.
Expand All @@ -55,7 +55,7 @@ type DeployConfig struct {
ordering []*regexp.Regexp // compiled Order
}

type target struct {
type Target struct {
Name string
URL string

Expand All @@ -75,7 +75,7 @@ type target struct {
excludeGlob glob.Glob
}

func (tgt *target) parseIncludeExclude() error {
func (tgt *Target) parseIncludeExclude() error {
var err error
if tgt.Include != "" {
tgt.includeGlob, err = hglob.GetGlob(tgt.Include)
Expand All @@ -92,9 +92,9 @@ func (tgt *target) parseIncludeExclude() error {
return nil
}

// matcher represents configuration to be applied to files whose paths match
// Matcher represents configuration to be applied to files whose paths match
// a specified pattern.
type matcher struct {
type Matcher struct {
// Pattern is the string pattern to match against paths.
// Matching is done against paths converted to use / as the path separator.
Pattern string
Expand Down Expand Up @@ -123,7 +123,7 @@ type matcher struct {
re *regexp.Regexp
}

func (m *matcher) Matches(path string) bool {
func (m *Matcher) Matches(path string) bool {
return m.re.MatchString(path)
}

Expand All @@ -145,7 +145,7 @@ func DecodeConfig(cfg config.Provider) (DeployConfig, error) {
}

for _, tgt := range dcfg.Targets {
if *tgt == (target{}) {
if *tgt == (Target{}) {
return dcfg, errors.New("empty deployment target")
}
if err := tgt.parseIncludeExclude(); err != nil {
Expand All @@ -154,7 +154,7 @@ func DecodeConfig(cfg config.Provider) (DeployConfig, error) {
}
var err error
for _, m := range dcfg.Matchers {
if *m == (matcher{}) {
if *m == (Matcher{}) {
return dcfg, errors.New("empty deployment matcher")
}
m.re, err = regexp.Compile(m.Pattern)
Expand Down
22 changes: 11 additions & 11 deletions deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestFindDiffs(t *testing.T) {
{
Description: "local == remote with route.Force true -> diffs",
Local: []*localFile{
{NativePath: "aaa", SlashPath: "aaa", UploadSize: 1, matcher: &matcher{Force: true}, md5: hash1},
{NativePath: "aaa", SlashPath: "aaa", UploadSize: 1, matcher: &Matcher{Force: true}, md5: hash1},
makeLocal("bbb", 2, hash1),
},
Remote: []*blob.ListObject{
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestLocalFile(t *testing.T) {
tests := []struct {
Description string
Path string
Matcher *matcher
Matcher *Matcher
MediaTypesConfig map[string]any
WantContent []byte
WantSize int64
Expand All @@ -315,7 +315,7 @@ func TestLocalFile(t *testing.T) {
{
Description: "CacheControl from matcher",
Path: "foo.txt",
Matcher: &matcher{CacheControl: "max-age=630720000"},
Matcher: &Matcher{CacheControl: "max-age=630720000"},
WantContent: contentBytes,
WantSize: contentLen,
WantMD5: contentMD5[:],
Expand All @@ -324,7 +324,7 @@ func TestLocalFile(t *testing.T) {
{
Description: "ContentEncoding from matcher",
Path: "foo.txt",
Matcher: &matcher{ContentEncoding: "foobar"},
Matcher: &Matcher{ContentEncoding: "foobar"},
WantContent: contentBytes,
WantSize: contentLen,
WantMD5: contentMD5[:],
Expand All @@ -333,7 +333,7 @@ func TestLocalFile(t *testing.T) {
{
Description: "ContentType from matcher",
Path: "foo.txt",
Matcher: &matcher{ContentType: "foo/bar"},
Matcher: &Matcher{ContentType: "foo/bar"},
WantContent: contentBytes,
WantSize: contentLen,
WantMD5: contentMD5[:],
Expand All @@ -342,7 +342,7 @@ func TestLocalFile(t *testing.T) {
{
Description: "gzipped content",
Path: "foo.txt",
Matcher: &matcher{Gzip: true},
Matcher: &Matcher{Gzip: true},
WantContent: gzBytes,
WantSize: gzLen,
WantMD5: gzMD5[:],
Expand Down Expand Up @@ -760,7 +760,7 @@ func TestIncludeExclude(t *testing.T) {
if err != nil {
t.Fatal(err)
}
tgt := &target{
tgt := &Target{
Include: test.Include,
Exclude: test.Exclude,
}
Expand Down Expand Up @@ -844,7 +844,7 @@ func TestIncludeExcludeRemoteDelete(t *testing.T) {
}

// Second sync
tgt := &target{
tgt := &Target{
Include: test.Include,
Exclude: test.Exclude,
}
Expand Down Expand Up @@ -878,7 +878,7 @@ func TestCompression(t *testing.T) {
deployer := &Deployer{
localFs: test.fs,
bucket: test.bucket,
cfg: DeployConfig{MaxDeletes: -1, Matchers: []*matcher{{Pattern: ".*", Gzip: true, re: regexp.MustCompile(".*")}}},
cfg: DeployConfig{MaxDeletes: -1, Matchers: []*Matcher{{Pattern: ".*", Gzip: true, re: regexp.MustCompile(".*")}}},
mediaTypes: media.DefaultTypes,
}

Expand Down Expand Up @@ -933,7 +933,7 @@ func TestMatching(t *testing.T) {
deployer := &Deployer{
localFs: test.fs,
bucket: test.bucket,
cfg: DeployConfig{MaxDeletes: -1, Matchers: []*matcher{{Pattern: "^subdir/aaa$", Force: true, re: regexp.MustCompile("^subdir/aaa$")}}},
cfg: DeployConfig{MaxDeletes: -1, Matchers: []*Matcher{{Pattern: "^subdir/aaa$", Force: true, re: regexp.MustCompile("^subdir/aaa$")}}},
mediaTypes: media.DefaultTypes,
}

Expand All @@ -958,7 +958,7 @@ func TestMatching(t *testing.T) {
}

// Repeat with a matcher that should now match 3 files.
deployer.cfg.Matchers = []*matcher{{Pattern: "aaa", Force: true, re: regexp.MustCompile("aaa")}}
deployer.cfg.Matchers = []*Matcher{{Pattern: "aaa", Force: true, re: regexp.MustCompile("aaa")}}
if err := deployer.Deploy(ctx); err != nil {
t.Errorf("no-op deploy with triple force matcher: %v", err)
}
Expand Down
22 changes: 22 additions & 0 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ name = 'Services'
pageRef = '/services'
weight = 30
[deployment]
order = [".jpg$", ".gif$"]
[[deployment.targets]]
name = "mydeployment"
url = "s3://mybucket?region=us-east-1"
cloudFrontDistributionID = "mydistributionid"
[[deployment.matchers]]
pattern = "^.+\\.(js|css|svg|ttf)$"
cacheControl = "max-age=31536000, no-transform, public"
gzip = true
[[deployment.matchers]]
pattern = "^.+\\.(png|jpg)$"
cacheControl = "max-age=31536000, no-transform, public"
gzip = false
[[deployment.matchers]]
pattern = "^sitemap\\.xml$"
contentType = "application/xml"
gzip = true
[[deployment.matchers]]
pattern = "^.+\\.(html|xml|json)$"
gzip = true
[permalinks]
posts = '/posts/:year/:month/:title/'
Expand Down
3 changes: 1 addition & 2 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ func (s *Site) withSiteTemplates(withTemplates ...func(templ tpl.TemplateManager
// Reset resets the sites and template caches etc., making it ready for a full rebuild.
func (h *HugoSites) reset(config *BuildCfg) {
if config.ResetState {
for i, s := range h.Sites {
h.Sites[i] = s // TODO1 s.reset()
for _, s := range h.Sites {
if r, ok := s.Fs.PublishDir.(hugofs.Reseter); ok {
r.Reset()
}
Expand Down
3 changes: 0 additions & 3 deletions hugolib/page__meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,6 @@ func (pm *pageMeta) setMetadata(parentBucket *pagesMapBucket, p *pageState, fron
break
}
fallthrough
case "_merge":
// TODO1

default:
// If not one of the explicit values, store in Params
switch vv := v.(type) {
Expand Down
17 changes: 0 additions & 17 deletions hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,6 @@ func (s *Site) isEnabled(kind string) bool {
return s.conf.IsKindEnabled(kind)
}

// reset returns a new Site prepared for rebuild.
func (s *Site) reset() *Site {
return &Site{
Deps: s.Deps,
relatedDocsHandler: s.relatedDocsHandler.Clone(),
siteRefLinker: s.siteRefLinker,
rc: s.rc,
frontmatterHandler: s.frontmatterHandler,
language: s.language,
siteBucket: s.siteBucket,
h: s.h,
publisher: s.publisher,
init: s.init,
PageCollections: s.PageCollections,
}
}

type siteRefLinker struct {
s *Site

Expand Down
14 changes: 0 additions & 14 deletions modules/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,6 @@ func IsNotExist(err error) bool {
return errors.Is(err, os.ErrNotExist)
}

// CreateProjectModule creates modules from the given config.
// This is used in tests only.
func CreateProjectModule(cfg config.AllProvider) (Module, error) {
workingDir := cfg.BaseConfig().WorkingDir
modConfig := cfg.GetConfigSection("module").(Config)

mod := createProjectModule(nil, workingDir, modConfig)
if err := ApplyProjectConfigDefaults(mod, cfg); err != nil {
return nil, err
}

return mod, nil
}

func (h *Client) Collect() (ModulesConfig, error) {
mc, coll := h.collect(true)
if coll.err != nil {
Expand Down
2 changes: 0 additions & 2 deletions modules/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ var DefaultModuleConfig = Config{

// ApplyProjectConfigDefaults applies default/missing module configuration for
// the main project.
// TODO1 consolidate this with the rest of this.
func ApplyProjectConfigDefaults(mod Module, cfgs ...config.AllProvider) error {

moda := mod.(*moduleAdapter)
Expand Down Expand Up @@ -167,7 +166,6 @@ func DecodeConfig(cfg config.Provider) (Config, error) {
return decodeConfig(cfg, nil)
}

// TODO1 config vs docs.
func decodeConfig(cfg config.Provider, pathReplacements map[string]string) (Config, error) {
c := DefaultModuleConfig
c.replacementsMap = pathReplacements
Expand Down
1 change: 1 addition & 0 deletions resources/page/page_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func DecodeCascadeConfig(in any) (*config.ConfigNamespace[[]PageMatcherParamsCon
var cfgs []PageMatcherParamsConfig

for _, m := range ms {
m = maps.CleanConfigStringMap(m)
c, err := mapToPageMatcherParamsConfig(m)
if err != nil {
return nil, nil, err
Expand Down

0 comments on commit 5693ed0

Please sign in to comment.