Skip to content

Commit 9b46409

Browse files
committed
Add --ignoreCache CLI flag with description "Ignores the
cache directory for reading but still writes to it" as @spf13 suggested.
1 parent 3a571be commit 9b46409

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

‎commands/hugo.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Complete documentation is available at http://gohugo.io`,
5555
var hugoCmdV *cobra.Command
5656

5757
//Flags that are to be added to commands.
58-
var BuildWatch, Draft, Future, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap, PluralizeListTitles, NoTimes bool
58+
var BuildWatch, IgnoreCache, Draft, Future, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap, PluralizeListTitles, NoTimes bool
5959
var Source, CacheDir, Destination, Theme, BaseUrl, CfgFile, LogFile, Editor string
6060

6161
//Execute adds all child commands to the root command HugoCmd and sets flags appropriately.
@@ -83,6 +83,7 @@ func init() {
8383
HugoCmd.PersistentFlags().BoolVar(&DisableSitemap, "disableSitemap", false, "Do not build Sitemap file")
8484
HugoCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from")
8585
HugoCmd.PersistentFlags().StringVarP(&CacheDir, "cacheDir", "", "$TMPDIR/hugo_cache/", "filesystem path to cache directory")
86+
HugoCmd.PersistentFlags().BoolVarP(&IgnoreCache, "ignoreCache", "", false, "Ignores the cache directory for reading but still writes to it")
8687
HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to")
8788
HugoCmd.PersistentFlags().StringVarP(&Theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)")
8889
HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
@@ -125,6 +126,7 @@ func InitializeConfig() {
125126
viper.SetDefault("BuildFuture", false)
126127
viper.SetDefault("UglyUrls", false)
127128
viper.SetDefault("Verbose", false)
129+
viper.SetDefault("IgnoreCache", false)
128130
viper.SetDefault("CanonifyUrls", false)
129131
viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})
130132
viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
@@ -202,10 +204,19 @@ func InitializeConfig() {
202204
viper.Set("WorkingDir", dir)
203205
}
204206

207+
if hugoCmdV.PersistentFlags().Lookup("ignoreCache").Changed {
208+
viper.Set("IgnoreCache", IgnoreCache)
209+
}
210+
205211
if CacheDir != "" {
206212
if helpers.FilePathSeparator != CacheDir[len(CacheDir)-1:] {
207213
CacheDir = CacheDir + helpers.FilePathSeparator
208214
}
215+
isDir, err := helpers.DirExists(CacheDir, hugofs.SourceFs)
216+
utils.CheckErr(err)
217+
if isDir == false {
218+
mkdir(CacheDir)
219+
}
209220
viper.Set("CacheDir", CacheDir)
210221
} else {
211222
viper.Set("CacheDir", helpers.GetTempDir("hugo_cache", hugofs.SourceFs))

‎tpl/template_resources.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ func getCacheFileID(id string) string {
6464

6565
// resGetCache returns the content for an ID from the file cache or an error
6666
// if the file is not found returns nil,nil
67-
func resGetCache(id string, fs afero.Fs) ([]byte, error) {
67+
func resGetCache(id string, fs afero.Fs, ignoreCache bool) ([]byte, error) {
68+
if ignoreCache {
69+
return nil, nil
70+
}
6871
fID := getCacheFileID(id)
6972
isExists, err := helpers.Exists(fID, fs)
7073
if err != nil {
@@ -99,7 +102,7 @@ func resWriteCache(id string, c []byte, fs afero.Fs) error {
99102
// resGetRemote loads the content of a remote file. This method is thread safe.
100103
func resGetRemote(url string, fs afero.Fs, hc *http.Client) ([]byte, error) {
101104

102-
c, err := resGetCache(url, fs)
105+
c, err := resGetCache(url, fs, viper.GetBool("IgnoreCache"))
103106
if c != nil && err == nil {
104107
return c, nil
105108
}
@@ -112,7 +115,7 @@ func resGetRemote(url string, fs afero.Fs, hc *http.Client) ([]byte, error) {
112115
defer func() { remoteUrlLock.UrlUnlock(url) }()
113116

114117
// avoid multiple locks due to calling resGetCache twice
115-
c, err = resGetCache(url, fs)
118+
c, err = resGetCache(url, fs, viper.GetBool("IgnoreCache"))
116119
if c != nil && err == nil {
117120
return c, nil
118121
}

‎tpl/template_resources_test.go‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@ func TestScpCache(t *testing.T) {
3030
tests := []struct {
3131
path string
3232
content []byte
33+
ignore bool
3334
}{
34-
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`)},
35-
{"fOO,bar:foo%bAR", []byte(`T€st Content 123 fOO,bar:foo%bAR`)},
36-
{"FOo/BaR.html", []byte(`FOo/BaR.html T€st Content 123`)},
37-
{"трям/трям", []byte(`T€st трям/трям Content 123`)},
38-
{"은행", []byte(`T€st C은행ontent 123`)},
39-
{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`)},
35+
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
36+
{"fOO,bar:foo%bAR", []byte(`T€st Content 123 fOO,bar:foo%bAR`), false},
37+
{"FOo/BaR.html", []byte(`FOo/BaR.html T€st Content 123`), false},
38+
{"трям/трям", []byte(`T€st трям/трям Content 123`), false},
39+
{"은행", []byte(`T€st C은행ontent 123`), false},
40+
{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`), false},
41+
{"Банковский кассир", []byte(`Банковский кассир T€st Content 456`), true},
4042
}
4143

4244
fs := new(afero.MemMapFs)
4345

4446
for _, test := range tests {
45-
c, err := resGetCache(test.path, fs)
47+
c, err := resGetCache(test.path, fs, test.ignore)
4648
if err != nil {
4749
t.Errorf("Error getting cache: %s", err)
4850
}
@@ -55,12 +57,18 @@ func TestScpCache(t *testing.T) {
5557
t.Errorf("Error writing cache: %s", err)
5658
}
5759

58-
c, err = resGetCache(test.path, fs)
60+
c, err = resGetCache(test.path, fs, test.ignore)
5961
if err != nil {
6062
t.Errorf("Error getting cache after writing: %s", err)
6163
}
62-
if bytes.Compare(c, test.content) != 0 {
63-
t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
64+
if test.ignore {
65+
if c != nil {
66+
t.Errorf("Cache ignored but content is not nil: %s", string(c))
67+
}
68+
} else {
69+
if bytes.Compare(c, test.content) != 0 {
70+
t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
71+
}
6472
}
6573
}
6674
}
@@ -111,10 +119,12 @@ func TestScpGetRemote(t *testing.T) {
111119
tests := []struct {
112120
path string
113121
content []byte
122+
ignore bool
114123
}{
115-
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`)},
116-
{"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`)},
117-
{"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`)},
124+
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
125+
{"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`), false},
126+
{"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`), false},
127+
{"http://Doppel.Gänger/Fizz_Bazz-Bar", []byte(`T€st Банковский кассир Cont€nt 456`), true},
118128
}
119129

120130
for _, test := range tests {
@@ -131,12 +141,18 @@ func TestScpGetRemote(t *testing.T) {
131141
if bytes.Compare(c, test.content) != 0 {
132142
t.Errorf("\nNet Expected: %s\nNet Actual: %s\n", string(test.content), string(c))
133143
}
134-
cc, cErr := resGetCache(test.path, fs)
144+
cc, cErr := resGetCache(test.path, fs, test.ignore)
135145
if cErr != nil {
136146
t.Error(cErr)
137147
}
138-
if bytes.Compare(cc, test.content) != 0 {
139-
t.Errorf("\nCache Expected: %s\nCache Actual: %s\n", string(test.content), string(c))
148+
if test.ignore {
149+
if cc != nil {
150+
t.Errorf("Cache ignored but content is not nil: %s", string(cc))
151+
}
152+
} else {
153+
if bytes.Compare(cc, test.content) != 0 {
154+
t.Errorf("\nCache Expected: %s\nCache Actual: %s\n", string(test.content), string(cc))
155+
}
140156
}
141157
}
142158
}

0 commit comments

Comments
 (0)