Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 4, 2023
1 parent 748c81c commit 2697291
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 89 deletions.
22 changes: 5 additions & 17 deletions commandsnew/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ import (
"context"

"github.com/bep/simplecobra"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/deploy"
"github.com/spf13/cobra"
)

func newDeployCommand() simplecobra.Commander {

// Flags.
var invalidateCDN bool
var maxDeletes int
var workers int

return &simpleCommand{
name: "deploy",
short: "Deploy your site to a Cloud provider.",
Expand All @@ -41,30 +35,24 @@ See https://gohugo.io/hosting-and-deployment/hugo-deploy/ for detailed
documentation.
`,
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
// TODO1 todelidoo.
cfg := config.New()
cfg.Set("invalidateCDN", invalidateCDN)
cfg.Set("maxDeletes", maxDeletes)
cfg.Set("workers", workers)
h, err := r.Hugo(flagsToCfg(cd, nil))
h, err := r.Hugo(flagsToCfgWithAdditionalConfigBase(cd, nil, "deployment"))
if err != nil {
return err
}
deployer, err := deploy.New(h.Configs.LoadingInfo.Cfg, h.PathSpec.PublishFs)
deployer, err := deploy.New(h.Configs.GetFirstLanguageConfig(), h.PathSpec.PublishFs)
if err != nil {
return err
}
return deployer.Deploy(context.Background())
},
withc: func(cmd *cobra.Command) {

cmd.Flags().String("target", "", "target deployment from deployments section in config file; defaults to the first one")
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().BoolVar(&invalidateCDN, "invalidateCDN", true, "invalidate the CDN cache listed in the deployment target")
cmd.Flags().IntVar(&maxDeletes, "maxDeletes", 256, "maximum # of files to delete, or -1 to disable")
cmd.Flags().IntVar(&workers, "workers", 10, "number of workers to transfer files. defaults to 10")
cmd.Flags().Bool("invalidateCDN", true, "invalidate the CDN cache listed in the deployment target")
cmd.Flags().Int("maxDeletes", 256, "maximum # of files to delete, or -1 to disable")
cmd.Flags().Int("workers", 10, "number of workers to transfer files. defaults to 10")
},
}
}
8 changes: 7 additions & 1 deletion commandsnew/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
)

func flagsToCfg(cd *simplecobra.Commandeer, cfg config.Provider) config.Provider {
return flagsToCfgWithAdditionalConfigBase(cd, cfg, "")
}

func flagsToCfgWithAdditionalConfigBase(cd *simplecobra.Commandeer, cfg config.Provider, additionalConfigBase string) config.Provider {
if cfg == nil {
cfg = config.New()
}
Expand All @@ -33,13 +37,15 @@ func flagsToCfg(cd *simplecobra.Commandeer, cfg config.Provider) config.Provider
flags := cmd.Flags()

flags.VisitAll(func(f *pflag.Flag) {
fmt.Println(f.Name, " ===>", f.Changed)
if f.Changed {
targetKey := f.Name
if mapped, ok := keyMap[targetKey]; ok {
targetKey = mapped
}
setValueFromFlag(flags, f.Name, cfg, targetKey, false)
if additionalConfigBase != "" {
setValueFromFlag(flags, f.Name, cfg, additionalConfigBase+"."+targetKey, true)
}
}
})

Expand Down
5 changes: 5 additions & 0 deletions config/allconfig/allconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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/helpers"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/markup/markup_config"
Expand Down Expand Up @@ -138,6 +139,9 @@ 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 Expand Up @@ -463,6 +467,7 @@ type RootConfig struct {
CleanDestinationDir bool

// TODO1 doc these. Move?
Quiet bool
Clock string
Watch bool
DisableLiveReload bool
Expand Down
9 changes: 9 additions & 0 deletions config/allconfig/alldecoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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/langs"
"github.com/gohugoio/hugo/markup/markup_config"
"github.com/gohugoio/hugo/media"
Expand Down Expand Up @@ -280,6 +281,14 @@ var allDecoderSetups = map[string]decodeWeight{
return err
},
},
"deploment": {
key: "deployment",
decode: func(d decodeWeight, p decodeParams) error {
var err error
p.c.Deployment, err = deploy.DecodeConfig(p.p)
return err
},
},
"author": {
key: "author",
decode: func(d decodeWeight, p decodeParams) error {
Expand Down
6 changes: 6 additions & 0 deletions config/allconfig/configlanguage.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (c ConfigLanguage) DirsBase() config.CommonDirs {
return c.m.Base.CommonDirs
}

func (c ConfigLanguage) Quiet() bool {
return c.m.Base.Quiet
}

// GetConfigSection is mostly used in tests. The switch statement isn't complete, but what's in use.
func (c ConfigLanguage) GetConfigSection(s string) any {
switch s {
Expand All @@ -120,6 +124,8 @@ func (c ConfigLanguage) GetConfigSection(s string) any {
return c.config.Minify
case "activeModules":
return c.m.Modules
case "deployment":
return c.config.Deployment
default:
panic("not implemented: " + s)
}
Expand Down
1 change: 1 addition & 0 deletions config/configProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type AllProvider interface {
NoBuildLock() bool
BaseConfig() BaseConfig
Dirs() CommonDirs
Quiet() bool
DirsBase() CommonDirs
GetConfigSection(string) any
GetConfig() any
Expand Down
75 changes: 27 additions & 48 deletions deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,12 @@ type Deployer struct {
localFs afero.Fs
bucket *blob.Bucket

target *target // the target to deploy to
matchers []*matcher // matchers to apply to uploaded files
mediaTypes media.Types // Hugo's MediaType to guess ContentType
ordering []*regexp.Regexp // orders uploads
quiet bool // true reduces STDOUT
confirm bool // true enables confirmation before making changes
dryRun bool // true skips conformations and prints changes instead of applying them
force bool // true forces upload of all files
invalidateCDN bool // true enables invalidate CDN cache (if possible)
maxDeletes int // caps the # of files to delete; -1 to disable
workers int // The number of workers to transfer files
mediaTypes media.Types // Hugo's MediaType to guess ContentType
quiet bool // true reduces STDOUT

cfg DeployConfig

target *target // the target to deploy to

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

// New constructs a new *Deployer.
// TODO1
func New(cfg config.Provider, localFs afero.Fs) (*Deployer, error) {
targetName := cfg.GetString("target")
func New(cfg config.AllProvider, localFs afero.Fs) (*Deployer, error) {

// Load the [deployment] section of the config.
dcfg, err := decodeConfig(cfg)
if err != nil {
return nil, err
}
dcfg := cfg.GetConfigSection(deploymentConfigKey).(DeployConfig)
targetName := dcfg.Target

if len(dcfg.Targets) == 0 {
return nil, errors.New("no deployment targets found")
}
mediaTypes := cfg.GetConfigSection("mediaTypes").(media.Types)

// Find the target to deploy to.
var tgt *target
Expand All @@ -109,18 +101,11 @@ func New(cfg config.Provider, localFs afero.Fs) (*Deployer, error) {
}

return &Deployer{
localFs: localFs,
target: tgt,
matchers: dcfg.Matchers,
ordering: dcfg.ordering,
mediaTypes: dcfg.mediaTypes,
quiet: cfg.GetBool("quiet"),
confirm: cfg.GetBool("confirm"),
dryRun: cfg.GetBool("dryRun"),
force: cfg.GetBool("force"),
invalidateCDN: cfg.GetBool("invalidateCDN"),
maxDeletes: cfg.GetInt("maxDeletes"),
workers: cfg.GetInt("workers"),
localFs: localFs,
target: tgt,
quiet: cfg.BuildExpired(),
mediaTypes: mediaTypes,
cfg: dcfg,
}, nil
}

Expand All @@ -144,7 +129,7 @@ func (d *Deployer) Deploy(ctx context.Context) error {
if d.target != nil {
include, exclude = d.target.includeGlob, d.target.excludeGlob
}
local, err := walkLocal(d.localFs, d.matchers, include, exclude, d.mediaTypes)
local, err := walkLocal(d.localFs, d.cfg.Matchers, include, exclude, d.mediaTypes)
if err != nil {
return err
}
Expand All @@ -160,7 +145,7 @@ func (d *Deployer) Deploy(ctx context.Context) error {
d.summary.NumRemote = len(remote)

// Diff local vs remote to see what changes need to be applied.
uploads, deletes := findDiffs(local, remote, d.force)
uploads, deletes := findDiffs(local, remote, d.cfg.Force)
d.summary.NumUploads = len(uploads)
d.summary.NumDeletes = len(deletes)
if len(uploads)+len(deletes) == 0 {
Expand All @@ -174,7 +159,7 @@ func (d *Deployer) Deploy(ctx context.Context) error {
}

// Ask for confirmation before proceeding.
if d.confirm && !d.dryRun {
if d.cfg.Confirm && !d.cfg.DryRun {
fmt.Printf("Continue? (Y/n) ")
var confirm string
if _, err := fmt.Scanln(&confirm); err != nil {
Expand All @@ -187,15 +172,9 @@ 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.ordering, uploads)
uploadGroups := applyOrdering(d.cfg.ordering, uploads)

// Apply the changes in parallel, using an inverted worker
// pool (https://www.youtube.com/watch?v=5zXAHh5tJqQ&t=26m58s).
// sem prevents more than nParallel concurrent goroutines.
if d.workers <= 0 {
d.workers = 10
}
nParallel := d.workers
nParallel := d.cfg.Workers
var errs []error
var errMu sync.Mutex // protects errs

Expand All @@ -208,7 +187,7 @@ func (d *Deployer) Deploy(ctx context.Context) error {
// Within the group, apply uploads in parallel.
sem := make(chan struct{}, nParallel)
for _, upload := range uploads {
if d.dryRun {
if d.cfg.DryRun {
if !d.quiet {
jww.FEEDBACK.Printf("[DRY RUN] Would upload: %v\n", upload)
}
Expand All @@ -231,15 +210,15 @@ func (d *Deployer) Deploy(ctx context.Context) error {
}
}

if d.maxDeletes != -1 && len(deletes) > d.maxDeletes {
jww.WARN.Printf("Skipping %d deletes because it is more than --maxDeletes (%d). If this is expected, set --maxDeletes to a larger number, or -1 to disable this check.\n", len(deletes), d.maxDeletes)
if d.cfg.MaxDeletes != -1 && len(deletes) > d.cfg.MaxDeletes {
jww.WARN.Printf("Skipping %d deletes because it is more than --maxDeletes (%d). If this is expected, set --maxDeletes to a larger number, or -1 to disable this check.\n", len(deletes), d.cfg.MaxDeletes)
d.summary.NumDeletes = 0
} else {
// Apply deletes in parallel.
sort.Slice(deletes, func(i, j int) bool { return deletes[i] < deletes[j] })
sem := make(chan struct{}, nParallel)
for _, del := range deletes {
if d.dryRun {
if d.cfg.DryRun {
if !d.quiet {
jww.FEEDBACK.Printf("[DRY RUN] Would delete %s\n", del)
}
Expand Down Expand Up @@ -275,9 +254,9 @@ func (d *Deployer) Deploy(ctx context.Context) error {
jww.FEEDBACK.Println("Success!")
}

if d.invalidateCDN {
if d.cfg.InvalidateCDN {
if d.target.CloudFrontDistributionID != "" {
if d.dryRun {
if d.cfg.DryRun {
if !d.quiet {
jww.FEEDBACK.Printf("[DRY RUN] Would invalidate CloudFront CDN with ID %s\n", d.target.CloudFrontDistributionID)
}
Expand All @@ -290,7 +269,7 @@ func (d *Deployer) Deploy(ctx context.Context) error {
}
}
if d.target.GoogleCloudCDNOrigin != "" {
if d.dryRun {
if d.cfg.DryRun {
if !d.quiet {
jww.FEEDBACK.Printf("[DRY RUN] Would invalidate Google Cloud CDN with origin %s\n", d.target.GoogleCloudCDNOrigin)
}
Expand Down
Loading

0 comments on commit 2697291

Please sign in to comment.