From 00dfce3b03ef43a10dde3eb255b64f75d9159a7b Mon Sep 17 00:00:00 2001 From: Jess Date: Mon, 28 Jul 2025 14:52:32 -0600 Subject: [PATCH] Allow caching of remote files to be disabled Make it possible to automatically update the cache of remote resources by disabling the caching of those resources using a query string parameter (`cache=false`). Signed-off-by: Jess --- pkg/remote/remote.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index c0845f23..8b6006f6 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "maps" "net/http" neturl "net/url" "os" @@ -213,13 +214,16 @@ func (r *Remote) Fetch(path string, cacheDirOpt ...string) (string, error) { return "", fmt.Errorf("[bug] cacheDirOpt's length: want 0 or 1, got %d", len(cacheDirOpt)) } - query := u.RawQuery + query, _ := neturl.ParseQuery(u.RawQuery) + + should_cache := query.Get("cache") != "false" + delete(query, "cache") var cacheKey string replacer := strings.NewReplacer(":", "", "//", "_", "/", "_", ".", "_") dirKey := replacer.Replace(srcDir) if len(query) > 0 { - q, _ := neturl.ParseQuery(query) + q := maps.Clone(query) if q.Has("sshkey") { q.Set("sshkey", "redacted") } @@ -262,7 +266,7 @@ func (r *Remote) Fetch(path string, cacheDirOpt ...string) (string, error) { } } - if !cached { + if !cached || !should_cache { var getterSrc string if u.User != "" { getterSrc = fmt.Sprintf("%s://%s@%s%s", u.Scheme, u.User, u.Host, u.Dir) @@ -271,7 +275,7 @@ func (r *Remote) Fetch(path string, cacheDirOpt ...string) (string, error) { } if len(query) > 0 { - getterSrc = strings.Join([]string{getterSrc, query}, "?") + getterSrc = strings.Join([]string{getterSrc, query.Encode()}, "?") } r.Logger.Debugf("remote> downloading %s to %s", getterSrc, getterDst)