Skip to content

Commit b5e60db

Browse files
Improve 'cache restore' command
1 parent d1f473e commit b5e60db

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

cache-cli/cmd/restore.go

Lines changed: 13 additions & 4 deletions
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,12 @@ func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metri
7577
break
7678
}
7779

78-
availableKeys, err := storage.List()
80+
// If the key has no regex characters, skip the list/regexp match.
81+
if regexp.QuoteMeta(key) == key {
82+
continue
83+
}
84+
85+
availableKeys, err := cachedList()
7986
utils.Check(err)
8087

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

9299
func findMatchingKey(availableKeys []storage.CacheKey, match string) string {
100+
pattern, err := regexp.Compile(match)
101+
if err != nil {
102+
return ""
103+
}
93104
for _, availableKey := range availableKeys {
94-
isMatch, _ := regexp.MatchString(match, availableKey.Name)
95-
if isMatch {
105+
if pattern.MatchString(availableKey.Name) {
96106
return availableKey.Name
97107
}
98108
}
99-
100109
return ""
101110
}
102111

0 commit comments

Comments
 (0)