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 <jess@ros.io>
This commit is contained in:
Jess 2025-07-28 14:52:32 -06:00
parent a76bec234c
commit 00dfce3b03
1 changed files with 8 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"maps"
"net/http" "net/http"
neturl "net/url" neturl "net/url"
"os" "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)) 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 var cacheKey string
replacer := strings.NewReplacer(":", "", "//", "_", "/", "_", ".", "_") replacer := strings.NewReplacer(":", "", "//", "_", "/", "_", ".", "_")
dirKey := replacer.Replace(srcDir) dirKey := replacer.Replace(srcDir)
if len(query) > 0 { if len(query) > 0 {
q, _ := neturl.ParseQuery(query) q := maps.Clone(query)
if q.Has("sshkey") { if q.Has("sshkey") {
q.Set("sshkey", "redacted") 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 var getterSrc string
if u.User != "" { if u.User != "" {
getterSrc = fmt.Sprintf("%s://%s@%s%s", u.Scheme, u.User, u.Host, u.Dir) 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 { 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) r.Logger.Debugf("remote> downloading %s to %s", getterSrc, getterDst)