Add --ignoreCache CLI flag with description "Ignores the

cache directory for reading but still writes to it" as @spf13
suggested.
This commit is contained in:
Cyrill Schumacher 2015-02-02 20:14:59 +11:00 committed by spf13
parent c168666b6d
commit 10c2b41b85
3 changed files with 50 additions and 20 deletions

View file

@ -55,7 +55,7 @@ Complete documentation is available at http://gohugo.io`,
var hugoCmdV *cobra.Command var hugoCmdV *cobra.Command
//Flags that are to be added to commands. //Flags that are to be added to commands.
var BuildWatch, Draft, Future, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap, PluralizeListTitles, NoTimes bool var BuildWatch, IgnoreCache, Draft, Future, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap, PluralizeListTitles, NoTimes bool
var Source, CacheDir, Destination, Theme, BaseUrl, CfgFile, LogFile, Editor string var Source, CacheDir, Destination, Theme, BaseUrl, CfgFile, LogFile, Editor string
//Execute adds all child commands to the root command HugoCmd and sets flags appropriately. //Execute adds all child commands to the root command HugoCmd and sets flags appropriately.
@ -84,6 +84,7 @@ func init() {
HugoCmd.PersistentFlags().BoolVar(&DisableSitemap, "disableSitemap", false, "Do not build Sitemap file") HugoCmd.PersistentFlags().BoolVar(&DisableSitemap, "disableSitemap", false, "Do not build Sitemap file")
HugoCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from") HugoCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from")
HugoCmd.PersistentFlags().StringVarP(&CacheDir, "cacheDir", "", "$TMPDIR/hugo_cache/", "filesystem path to cache directory") HugoCmd.PersistentFlags().StringVarP(&CacheDir, "cacheDir", "", "$TMPDIR/hugo_cache/", "filesystem path to cache directory")
HugoCmd.PersistentFlags().BoolVarP(&IgnoreCache, "ignoreCache", "", false, "Ignores the cache directory for reading but still writes to it")
HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to") HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to")
HugoCmd.PersistentFlags().StringVarP(&Theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)") HugoCmd.PersistentFlags().StringVarP(&Theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)")
HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
@ -127,6 +128,7 @@ func InitializeConfig() {
viper.SetDefault("BuildFuture", false) viper.SetDefault("BuildFuture", false)
viper.SetDefault("UglyUrls", false) viper.SetDefault("UglyUrls", false)
viper.SetDefault("Verbose", false) viper.SetDefault("Verbose", false)
viper.SetDefault("IgnoreCache", false)
viper.SetDefault("CanonifyUrls", false) viper.SetDefault("CanonifyUrls", false)
viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"}) viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})
viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0)) viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
@ -204,10 +206,19 @@ func InitializeConfig() {
viper.Set("WorkingDir", dir) viper.Set("WorkingDir", dir)
} }
if hugoCmdV.PersistentFlags().Lookup("ignoreCache").Changed {
viper.Set("IgnoreCache", IgnoreCache)
}
if CacheDir != "" { if CacheDir != "" {
if helpers.FilePathSeparator != CacheDir[len(CacheDir)-1:] { if helpers.FilePathSeparator != CacheDir[len(CacheDir)-1:] {
CacheDir = CacheDir + helpers.FilePathSeparator CacheDir = CacheDir + helpers.FilePathSeparator
} }
isDir, err := helpers.DirExists(CacheDir, hugofs.SourceFs)
utils.CheckErr(err)
if isDir == false {
mkdir(CacheDir)
}
viper.Set("CacheDir", CacheDir) viper.Set("CacheDir", CacheDir)
} else { } else {
viper.Set("CacheDir", helpers.GetTempDir("hugo_cache", hugofs.SourceFs)) viper.Set("CacheDir", helpers.GetTempDir("hugo_cache", hugofs.SourceFs))

View file

@ -64,7 +64,10 @@ func getCacheFileID(id string) string {
// resGetCache returns the content for an ID from the file cache or an error // resGetCache returns the content for an ID from the file cache or an error
// if the file is not found returns nil,nil // if the file is not found returns nil,nil
func resGetCache(id string, fs afero.Fs) ([]byte, error) { func resGetCache(id string, fs afero.Fs, ignoreCache bool) ([]byte, error) {
if ignoreCache {
return nil, nil
}
fID := getCacheFileID(id) fID := getCacheFileID(id)
isExists, err := helpers.Exists(fID, fs) isExists, err := helpers.Exists(fID, fs)
if err != nil { if err != nil {
@ -99,7 +102,7 @@ func resWriteCache(id string, c []byte, fs afero.Fs) error {
// resGetRemote loads the content of a remote file. This method is thread safe. // resGetRemote loads the content of a remote file. This method is thread safe.
func resGetRemote(url string, fs afero.Fs, hc *http.Client) ([]byte, error) { func resGetRemote(url string, fs afero.Fs, hc *http.Client) ([]byte, error) {
c, err := resGetCache(url, fs) c, err := resGetCache(url, fs, viper.GetBool("IgnoreCache"))
if c != nil && err == nil { if c != nil && err == nil {
return c, nil return c, nil
} }
@ -112,7 +115,7 @@ func resGetRemote(url string, fs afero.Fs, hc *http.Client) ([]byte, error) {
defer func() { remoteUrlLock.UrlUnlock(url) }() defer func() { remoteUrlLock.UrlUnlock(url) }()
// avoid multiple locks due to calling resGetCache twice // avoid multiple locks due to calling resGetCache twice
c, err = resGetCache(url, fs) c, err = resGetCache(url, fs, viper.GetBool("IgnoreCache"))
if c != nil && err == nil { if c != nil && err == nil {
return c, nil return c, nil
} }

View file

@ -30,19 +30,21 @@ func TestScpCache(t *testing.T) {
tests := []struct { tests := []struct {
path string path string
content []byte content []byte
ignore bool
}{ }{
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`)}, {"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
{"fOO,bar:foo%bAR", []byte(`T€st Content 123 fOO,bar:foo%bAR`)}, {"fOO,bar:foo%bAR", []byte(`T€st Content 123 fOO,bar:foo%bAR`), false},
{"FOo/BaR.html", []byte(`FOo/BaR.html T€st Content 123`)}, {"FOo/BaR.html", []byte(`FOo/BaR.html T€st Content 123`), false},
{"трям/трям", []byte(`T€st трям/трям Content 123`)}, {"трям/трям", []byte(`T€st трям/трям Content 123`), false},
{"은행", []byte(`T€st C은행ontent 123`)}, {"은행", []byte(`T€st C은행ontent 123`), false},
{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`)}, {"Банковский кассир", []byte(`Банковский кассир T€st Content 123`), false},
{"Банковский кассир", []byte(`Банковский кассир T€st Content 456`), true},
} }
fs := new(afero.MemMapFs) fs := new(afero.MemMapFs)
for _, test := range tests { for _, test := range tests {
c, err := resGetCache(test.path, fs) c, err := resGetCache(test.path, fs, test.ignore)
if err != nil { if err != nil {
t.Errorf("Error getting cache: %s", err) t.Errorf("Error getting cache: %s", err)
} }
@ -55,15 +57,21 @@ func TestScpCache(t *testing.T) {
t.Errorf("Error writing cache: %s", err) t.Errorf("Error writing cache: %s", err)
} }
c, err = resGetCache(test.path, fs) c, err = resGetCache(test.path, fs, test.ignore)
if err != nil { if err != nil {
t.Errorf("Error getting cache after writing: %s", err) t.Errorf("Error getting cache after writing: %s", err)
} }
if test.ignore {
if c != nil {
t.Errorf("Cache ignored but content is not nil: %s", string(c))
}
} else {
if bytes.Compare(c, test.content) != 0 { if bytes.Compare(c, test.content) != 0 {
t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c)) t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
} }
} }
} }
}
func TestScpGetLocal(t *testing.T) { func TestScpGetLocal(t *testing.T) {
fs := new(afero.MemMapFs) fs := new(afero.MemMapFs)
@ -111,10 +119,12 @@ func TestScpGetRemote(t *testing.T) {
tests := []struct { tests := []struct {
path string path string
content []byte content []byte
ignore bool
}{ }{
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`)}, {"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
{"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`)}, {"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`), false},
{"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`)}, {"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`), false},
{"http://Doppel.Gänger/Fizz_Bazz-Bar", []byte(`T€st Банковский кассир Cont€nt 456`), true},
} }
for _, test := range tests { for _, test := range tests {
@ -131,12 +141,18 @@ func TestScpGetRemote(t *testing.T) {
if bytes.Compare(c, test.content) != 0 { if bytes.Compare(c, test.content) != 0 {
t.Errorf("\nNet Expected: %s\nNet Actual: %s\n", string(test.content), string(c)) t.Errorf("\nNet Expected: %s\nNet Actual: %s\n", string(test.content), string(c))
} }
cc, cErr := resGetCache(test.path, fs) cc, cErr := resGetCache(test.path, fs, test.ignore)
if cErr != nil { if cErr != nil {
t.Error(cErr) t.Error(cErr)
} }
if test.ignore {
if cc != nil {
t.Errorf("Cache ignored but content is not nil: %s", string(cc))
}
} else {
if bytes.Compare(cc, test.content) != 0 { if bytes.Compare(cc, test.content) != 0 {
t.Errorf("\nCache Expected: %s\nCache Actual: %s\n", string(test.content), string(c)) t.Errorf("\nCache Expected: %s\nCache Actual: %s\n", string(test.content), string(cc))
}
} }
} }
} }