Skip to content

Commit

Permalink
Improve 'cache restore' command
Browse files Browse the repository at this point in the history
  • Loading branch information
lyoung-confluent committed Jun 26, 2024
1 parent d1f473e commit b5e60db
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions cache-cli/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"regexp"
"strings"
"sync"
"time"

"github.com/semaphoreci/toolbox/cache-cli/pkg/archive"
Expand Down Expand Up @@ -67,6 +68,7 @@ func RunRestore(cmd *cobra.Command, args []string) {
}

func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metricsManager metrics.MetricsManager, keys []string) {
cachedList := sync.OnceValues(storage.List)
for _, rawKey := range keys {
key := NormalizeKey(rawKey)
if ok, _ := storage.HasKey(key); ok {
Expand All @@ -75,7 +77,12 @@ func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metri
break
}

availableKeys, err := storage.List()
// If the key has no regex characters, skip the list/regexp match.
if regexp.QuoteMeta(key) == key {
continue
}

availableKeys, err := cachedList()
utils.Check(err)

matchingKey := findMatchingKey(availableKeys, key)
Expand All @@ -90,13 +97,15 @@ func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metri
}

func findMatchingKey(availableKeys []storage.CacheKey, match string) string {
pattern, err := regexp.Compile(match)
if err != nil {
return ""
}
for _, availableKey := range availableKeys {
isMatch, _ := regexp.MatchString(match, availableKey.Name)
if isMatch {
if pattern.MatchString(availableKey.Name) {
return availableKey.Name
}
}

return ""
}

Expand Down

0 comments on commit b5e60db

Please sign in to comment.