Skip to content

Commit 957f244

Browse files
Improve 'cache restore' command
1 parent d1f473e commit 957f244

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

cache-cli/cmd/restore.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"regexp"
88
"strings"
9+
"sync"
910
"time"
1011

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

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

78-
availableKeys, err := storage.List()
80+
availableKeys, err := cachedList()
7981
utils.Check(err)
8082

8183
matchingKey := findMatchingKey(availableKeys, key)
@@ -90,13 +92,24 @@ func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metri
9092
}
9193

9294
func findMatchingKey(availableKeys []storage.CacheKey, match string) string {
93-
for _, availableKey := range availableKeys {
94-
isMatch, _ := regexp.MatchString(match, availableKey.Name)
95-
if isMatch {
96-
return availableKey.Name
95+
// If the key has no regex characters, just use strings.Contains
96+
if regexp.QuoteMeta(match) == match {
97+
for _, availableKey := range availableKeys {
98+
if strings.Contains(availableKey.Name, match) {
99+
return availableKey.Name
100+
}
101+
}
102+
} else {
103+
pattern, err := regexp.Compile(match)
104+
if err != nil {
105+
return ""
106+
}
107+
for _, availableKey := range availableKeys {
108+
if pattern.MatchString(availableKey.Name) {
109+
return availableKey.Name
110+
}
97111
}
98112
}
99-
100113
return ""
101114
}
102115

0 commit comments

Comments
 (0)