Skip to content

Commit

Permalink
Avoid impporting deploy from config when nodeploy tag is set
Browse files Browse the repository at this point in the history
Test:

```
go list -tags nodeploy ./... | grep deploy
```

Fixes #12009
  • Loading branch information
bep committed Feb 7, 2024
1 parent a65622a commit 0257eb5
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 60 deletions.
10 changes: 6 additions & 4 deletions commands/deploy.go
Expand Up @@ -32,8 +32,10 @@ package commands
import (
"context"

"github.com/bep/simplecobra"
"github.com/gohugoio/hugo/deploy"
"github.com/gohugoio/hugo/deploy/deployconfig"

"github.com/bep/simplecobra"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -62,9 +64,9 @@ documentation.
cmd.Flags().Bool("confirm", false, "ask for confirmation before making changes to the target")
cmd.Flags().Bool("dryRun", false, "dry run")
cmd.Flags().Bool("force", false, "force upload of all files")
cmd.Flags().Bool("invalidateCDN", deploy.DefaultConfig.InvalidateCDN, "invalidate the CDN cache listed in the deployment target")
cmd.Flags().Int("maxDeletes", deploy.DefaultConfig.MaxDeletes, "maximum # of files to delete, or -1 to disable")
cmd.Flags().Int("workers", deploy.DefaultConfig.Workers, "number of workers to transfer files. defaults to 10")
cmd.Flags().Bool("invalidateCDN", deployconfig.DefaultConfig.InvalidateCDN, "invalidate the CDN cache listed in the deployment target")
cmd.Flags().Int("maxDeletes", deployconfig.DefaultConfig.MaxDeletes, "maximum # of files to delete, or -1 to disable")
cmd.Flags().Int("workers", deployconfig.DefaultConfig.Workers, "number of workers to transfer files. defaults to 10")
},
}
}
6 changes: 3 additions & 3 deletions config/allconfig/allconfig.go
Expand Up @@ -37,7 +37,7 @@ import (
"github.com/gohugoio/hugo/config/privacy"
"github.com/gohugoio/hugo/config/security"
"github.com/gohugoio/hugo/config/services"
"github.com/gohugoio/hugo/deploy"
"github.com/gohugoio/hugo/deploy/deployconfig"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/markup/markup_config"
Expand Down Expand Up @@ -141,8 +141,8 @@ 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:"-"`
// The deployment configuration section contains for hugo deployconfig.
Deployment deployconfig.DeployConfig `mapstructure:"-"`

// Module configuration.
Module modules.Config `mapstructure:"-"`
Expand Down
4 changes: 2 additions & 2 deletions config/allconfig/alldecoders.go
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/gohugoio/hugo/config/privacy"
"github.com/gohugoio/hugo/config/security"
"github.com/gohugoio/hugo/config/services"
"github.com/gohugoio/hugo/deploy"
"github.com/gohugoio/hugo/deploy/deployconfig"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/markup/markup_config"
"github.com/gohugoio/hugo/media"
Expand Down Expand Up @@ -333,7 +333,7 @@ var allDecoderSetups = map[string]decodeWeight{
key: "deployment",
decode: func(d decodeWeight, p decodeConfig) error {
var err error
p.c.Deployment, err = deploy.DecodeConfig(p.p)
p.c.Deployment, err = deployconfig.DecodeConfig(p.p)
return err
},
},
Expand Down
3 changes: 2 additions & 1 deletion deploy/cloudfront.go
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
"github.com/aws/aws-sdk-go-v2/service/cloudfront/types"
"github.com/gohugoio/hugo/deploy/deployconfig"
gcaws "gocloud.dev/aws"
)

Expand All @@ -38,7 +39,7 @@ var v2ConfigValidParams = map[string]bool{

// InvalidateCloudFront invalidates the CloudFront cache for distributionID.
// Uses AWS credentials config from the bucket URL.
func InvalidateCloudFront(ctx context.Context, target *Target) error {
func InvalidateCloudFront(ctx context.Context, target *deployconfig.Target) error {
u, err := url.Parse(target.URL)
if err != nil {
return err
Expand Down
21 changes: 11 additions & 10 deletions deploy/deploy.go
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/gobwas/glob"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/deploy/deployconfig"
"github.com/gohugoio/hugo/media"
"github.com/spf13/afero"
"golang.org/x/text/unicode/norm"
Expand All @@ -57,10 +58,10 @@ type Deployer struct {
mediaTypes media.Types // Hugo's MediaType to guess ContentType
quiet bool // true reduces STDOUT // TODO(bep) remove, this is a global feature.

cfg DeployConfig
cfg deployconfig.DeployConfig
logger loggers.Logger

target *Target // the target to deploy to
target *deployconfig.Target // the target to deploy to

// For tests...
summary deploySummary // summary of latest Deploy results
Expand All @@ -74,7 +75,7 @@ const metaMD5Hash = "md5chksum" // the meta key to store md5hash in

// New constructs a new *Deployer.
func New(cfg config.AllProvider, logger loggers.Logger, localFs afero.Fs) (*Deployer, error) {
dcfg := cfg.GetConfigSection(deploymentConfigKey).(DeployConfig)
dcfg := cfg.GetConfigSection(deployconfig.DeploymentConfigKey).(deployconfig.DeployConfig)
targetName := dcfg.Target

if len(dcfg.Targets) == 0 {
Expand All @@ -83,7 +84,7 @@ func New(cfg config.AllProvider, logger loggers.Logger, localFs afero.Fs) (*Depl
mediaTypes := cfg.GetConfigSection("mediaTypes").(media.Types)

// Find the target to deploy to.
var tgt *Target
var tgt *deployconfig.Target
if targetName == "" {
// Default to the first target.
tgt = dcfg.Targets[0]
Expand Down Expand Up @@ -133,7 +134,7 @@ func (d *Deployer) Deploy(ctx context.Context) error {
// Load local files from the source directory.
var include, exclude glob.Glob
if d.target != nil {
include, exclude = d.target.includeGlob, d.target.excludeGlob
include, exclude = d.target.IncludeGlob, d.target.ExcludeGlob
}
local, err := d.walkLocal(d.localFs, d.cfg.Matchers, include, exclude, d.mediaTypes)
if err != nil {
Expand Down Expand Up @@ -178,7 +179,7 @@ func (d *Deployer) Deploy(ctx context.Context) error {

// Order the uploads. They are organized in groups; all uploads in a group
// must be complete before moving on to the next group.
uploadGroups := applyOrdering(d.cfg.ordering, uploads)
uploadGroups := applyOrdering(d.cfg.Ordering, uploads)

nParallel := d.cfg.Workers
var errs []error
Expand Down Expand Up @@ -343,14 +344,14 @@ type localFile struct {
UploadSize int64

fs afero.Fs
matcher *Matcher
matcher *deployconfig.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 *deployconfig.Matcher, mt media.Types) (*localFile, error) {
f, err := fs.Open(nativePath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -482,7 +483,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 (d *Deployer) walkLocal(fs afero.Fs, matchers []*Matcher, include, exclude glob.Glob, mediaTypes media.Types) (map[string]*localFile, error) {
func (d *Deployer) walkLocal(fs afero.Fs, matchers []*deployconfig.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 @@ -521,7 +522,7 @@ func (d *Deployer) walkLocal(fs afero.Fs, matchers []*Matcher, include, exclude
}

// Find the first matching matcher (if any).
var m *Matcher
var m *deployconfig.Matcher
for _, cur := range matchers {
if cur.Matches(slashpath) {
m = cur
Expand Down
35 changes: 18 additions & 17 deletions deploy/deploy_test.go
Expand Up @@ -31,6 +31,7 @@ import (
"testing"

"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/deploy/deployconfig"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/media"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -110,7 +111,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: &deployconfig.Matcher{Force: true}, md5: hash1},
makeLocal("bbb", 2, hash1),
},
Remote: []*blob.ListObject{
Expand Down Expand Up @@ -293,7 +294,7 @@ func TestLocalFile(t *testing.T) {
tests := []struct {
Description string
Path string
Matcher *Matcher
Matcher *deployconfig.Matcher
MediaTypesConfig map[string]any
WantContent []byte
WantSize int64
Expand All @@ -319,7 +320,7 @@ func TestLocalFile(t *testing.T) {
{
Description: "CacheControl from matcher",
Path: "foo.txt",
Matcher: &Matcher{CacheControl: "max-age=630720000"},
Matcher: &deployconfig.Matcher{CacheControl: "max-age=630720000"},
WantContent: contentBytes,
WantSize: contentLen,
WantMD5: contentMD5[:],
Expand All @@ -328,7 +329,7 @@ func TestLocalFile(t *testing.T) {
{
Description: "ContentEncoding from matcher",
Path: "foo.txt",
Matcher: &Matcher{ContentEncoding: "foobar"},
Matcher: &deployconfig.Matcher{ContentEncoding: "foobar"},
WantContent: contentBytes,
WantSize: contentLen,
WantMD5: contentMD5[:],
Expand All @@ -337,7 +338,7 @@ func TestLocalFile(t *testing.T) {
{
Description: "ContentType from matcher",
Path: "foo.txt",
Matcher: &Matcher{ContentType: "foo/bar"},
Matcher: &deployconfig.Matcher{ContentType: "foo/bar"},
WantContent: contentBytes,
WantSize: contentLen,
WantMD5: contentMD5[:],
Expand All @@ -346,7 +347,7 @@ func TestLocalFile(t *testing.T) {
{
Description: "gzipped content",
Path: "foo.txt",
Matcher: &Matcher{Gzip: true},
Matcher: &deployconfig.Matcher{Gzip: true},
WantContent: gzBytes,
WantSize: gzLen,
WantMD5: gzMD5[:],
Expand Down Expand Up @@ -560,7 +561,7 @@ func TestEndToEndSync(t *testing.T) {
localFs: test.fs,
bucket: test.bucket,
mediaTypes: media.DefaultTypes,
cfg: DeployConfig{MaxDeletes: -1},
cfg: deployconfig.DeployConfig{MaxDeletes: -1},
}

// Initial deployment should sync remote with local.
Expand Down Expand Up @@ -643,7 +644,7 @@ func TestMaxDeletes(t *testing.T) {
localFs: test.fs,
bucket: test.bucket,
mediaTypes: media.DefaultTypes,
cfg: DeployConfig{MaxDeletes: -1},
cfg: deployconfig.DeployConfig{MaxDeletes: -1},
}

// Sync remote with local.
Expand Down Expand Up @@ -764,16 +765,16 @@ func TestIncludeExclude(t *testing.T) {
if err != nil {
t.Fatal(err)
}
tgt := &Target{
tgt := &deployconfig.Target{
Include: test.Include,
Exclude: test.Exclude,
}
if err := tgt.parseIncludeExclude(); err != nil {
if err := tgt.ParseIncludeExclude(); err != nil {
t.Error(err)
}
deployer := &Deployer{
localFs: fsTest.fs,
cfg: DeployConfig{MaxDeletes: -1}, bucket: fsTest.bucket,
cfg: deployconfig.DeployConfig{MaxDeletes: -1}, bucket: fsTest.bucket,
target: tgt,
mediaTypes: media.DefaultTypes,
}
Expand Down Expand Up @@ -830,7 +831,7 @@ func TestIncludeExcludeRemoteDelete(t *testing.T) {
}
deployer := &Deployer{
localFs: fsTest.fs,
cfg: DeployConfig{MaxDeletes: -1}, bucket: fsTest.bucket,
cfg: deployconfig.DeployConfig{MaxDeletes: -1}, bucket: fsTest.bucket,
mediaTypes: media.DefaultTypes,
}

Expand All @@ -848,11 +849,11 @@ func TestIncludeExcludeRemoteDelete(t *testing.T) {
}

// Second sync
tgt := &Target{
tgt := &deployconfig.Target{
Include: test.Include,
Exclude: test.Exclude,
}
if err := tgt.parseIncludeExclude(); err != nil {
if err := tgt.ParseIncludeExclude(); err != nil {
t.Error(err)
}
deployer.target = tgt
Expand Down Expand Up @@ -882,7 +883,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.DeployConfig{MaxDeletes: -1, Matchers: []*deployconfig.Matcher{{Pattern: ".*", Gzip: true, Re: regexp.MustCompile(".*")}}},
mediaTypes: media.DefaultTypes,
}

Expand Down Expand Up @@ -937,7 +938,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.DeployConfig{MaxDeletes: -1, Matchers: []*deployconfig.Matcher{{Pattern: "^subdir/aaa$", Force: true, Re: regexp.MustCompile("^subdir/aaa$")}}},
mediaTypes: media.DefaultTypes,
}

Expand All @@ -962,7 +963,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 = []*deployconfig.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

0 comments on commit 0257eb5

Please sign in to comment.