| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package deployconfig |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
"regexp" |
| 20 |
|
| 21 |
"github.com/gobwas/glob" |
| 22 |
"github.com/gohugoio/hugo/config" |
| 23 |
hglob "github.com/gohugoio/hugo/hugofs/hglob" |
| 24 |
"github.com/mitchellh/mapstructure" |
| 25 |
) |
| 26 |
|
| 27 |
const DeploymentConfigKey = "deployment" |
| 28 |
|
| 29 |
// DeployConfig is the complete configuration for deployment. |
| 30 |
type DeployConfig struct { |
| 31 |
Targets []*Target |
| 32 |
Matchers []*Matcher |
| 33 |
Order []string |
| 34 |
|
| 35 |
// Usually set via flags. |
| 36 |
// Target deployment Name; defaults to the first one. |
| 37 |
Target string |
| 38 |
// Show a confirm prompt before deploying. |
| 39 |
Confirm bool |
| 40 |
// DryRun will try the deployment without any remote changes. |
| 41 |
DryRun bool |
| 42 |
// Force will re-upload all files. |
| 43 |
Force bool |
| 44 |
// Invalidate the CDN cache listed in the deployment target. |
| 45 |
InvalidateCDN bool |
| 46 |
// MaxDeletes is the maximum number of files to delete. |
| 47 |
MaxDeletes int |
| 48 |
// Number of concurrent workers to use when uploading files. |
| 49 |
Workers int |
| 50 |
|
| 51 |
Ordering []*regexp.Regexp `json:"-"` // compiled Order |
| 52 |
} |
| 53 |
|
| 54 |
type Target struct { |
| 55 |
Name string |
| 56 |
URL string |
| 57 |
|
| 58 |
CloudFrontDistributionID string |
| 59 |
|
| 60 |
// GoogleCloudCDNOrigin specifies the Google Cloud project and CDN origin to |
| 61 |
// invalidate when deploying this target. It is specified as <project>/<origin>. |
| 62 |
GoogleCloudCDNOrigin string |
| 63 |
|
| 64 |
// Optional patterns of files to include/exclude for this target. |
| 65 |
// Parsed using github.com/gobwas/glob. |
| 66 |
Include string |
| 67 |
Exclude string |
| 68 |
|
| 69 |
// Parsed versions of Include/Exclude. |
| 70 |
IncludeGlob glob.Glob `json:"-"` |
| 71 |
ExcludeGlob glob.Glob `json:"-"` |
| 72 |
|
| 73 |
// If true, any local path matching <dir>/index.html will be mapped to the |
| 74 |
// remote path <dir>/. This does not affect the top-level index.html file, |
| 75 |
// since that would result in an empty path. |
| 76 |
StripIndexHTML bool |
| 77 |
} |
| 78 |
|
| 79 |
func (tgt *Target) ParseIncludeExclude() error { |
| 80 |
var err error |
| 81 |
if tgt.Include != "" { |
| 82 |
tgt.IncludeGlob, err = hglob.GetGlob(tgt.Include) |
| 83 |
if err != nil { |
| 84 |
return fmt.Errorf("invalid deployment.target.include %q: %v", tgt.Include, err) |
| 85 |
} |
| 86 |
} |
| 87 |
if tgt.Exclude != "" { |
| 88 |
tgt.ExcludeGlob, err = hglob.GetGlob(tgt.Exclude) |
| 89 |
if err != nil { |
| 90 |
return fmt.Errorf("invalid deployment.target.exclude %q: %v", tgt.Exclude, err) |
| 91 |
} |
| 92 |
} |
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
// Matcher represents configuration to be applied to files whose paths match |
| 97 |
// a specified pattern. |
| 98 |
type Matcher struct { |
| 99 |
// Pattern is the string pattern to match against paths. |
| 100 |
// Matching is done against paths converted to use / as the path separator. |
| 101 |
Pattern string |
| 102 |
|
| 103 |
// CacheControl specifies caching attributes to use when serving the blob. |
| 104 |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control |
| 105 |
CacheControl string |
| 106 |
|
| 107 |
// ContentEncoding specifies the encoding used for the blob's content, if any. |
| 108 |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding |
| 109 |
ContentEncoding string |
| 110 |
|
| 111 |
// ContentType specifies the MIME type of the blob being written. |
| 112 |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type |
| 113 |
ContentType string |
| 114 |
|
| 115 |
// Gzip determines whether the file should be gzipped before upload. |
| 116 |
// If so, the ContentEncoding field will automatically be set to "gzip". |
| 117 |
Gzip bool |
| 118 |
|
| 119 |
// Force indicates that matching files should be re-uploaded. Useful when |
| 120 |
// other route-determined metadata (e.g., ContentType) has changed. |
| 121 |
Force bool |
| 122 |
|
| 123 |
// Re is Pattern compiled. |
| 124 |
Re *regexp.Regexp `json:"-"` |
| 125 |
} |
| 126 |
|
| 127 |
func (m *Matcher) Matches(path string) bool { |
| 128 |
return m.Re.MatchString(path) |
| 129 |
} |
| 130 |
|
| 131 |
var DefaultConfig = DeployConfig{ |
| 132 |
Workers: 10, |
| 133 |
InvalidateCDN: true, |
| 134 |
MaxDeletes: 256, |
| 135 |
} |
| 136 |
|
| 137 |
// DecodeConfig creates a config from a given Hugo configuration. |
| 138 |
func DecodeConfig(cfg config.Provider) (DeployConfig, error) { |
| 139 |
dcfg := DefaultConfig |
| 140 |
|
| 141 |
if !cfg.IsSet(DeploymentConfigKey) { |
| 142 |
return dcfg, nil |
| 143 |
} |
| 144 |
if err := mapstructure.WeakDecode(cfg.GetStringMap(DeploymentConfigKey), &dcfg); err != nil { |
| 145 |
return dcfg, err |
| 146 |
} |
| 147 |
|
| 148 |
if dcfg.Workers <= 0 { |
| 149 |
dcfg.Workers = 10 |
| 150 |
} |
| 151 |
|
| 152 |
for _, tgt := range dcfg.Targets { |
| 153 |
if *tgt == (Target{}) { |
| 154 |
return dcfg, errors.New("empty deployment target") |
| 155 |
} |
| 156 |
if err := tgt.ParseIncludeExclude(); err != nil { |
| 157 |
return dcfg, err |
| 158 |
} |
| 159 |
} |
| 160 |
var err error |
| 161 |
for _, m := range dcfg.Matchers { |
| 162 |
if *m == (Matcher{}) { |
| 163 |
return dcfg, errors.New("empty deployment matcher") |
| 164 |
} |
| 165 |
m.Re, err = regexp.Compile(m.Pattern) |
| 166 |
if err != nil { |
| 167 |
return dcfg, fmt.Errorf("invalid deployment.matchers.pattern: %v", err) |
| 168 |
} |
| 169 |
} |
| 170 |
for _, o := range dcfg.Order { |
| 171 |
re, err := regexp.Compile(o) |
| 172 |
if err != nil { |
| 173 |
return dcfg, fmt.Errorf("invalid deployment.orderings.pattern: %v", err) |
| 174 |
} |
| 175 |
dcfg.Ordering = append(dcfg.Ordering, re) |
| 176 |
} |
| 177 |
|
| 178 |
return dcfg, nil |
| 179 |
} |