6
6
"os"
7
7
"regexp"
8
8
"strings"
9
+ "sync"
9
10
"time"
10
11
11
12
"github.com/semaphoreci/toolbox/cache-cli/pkg/archive"
@@ -67,6 +68,7 @@ func RunRestore(cmd *cobra.Command, args []string) {
67
68
}
68
69
69
70
func downloadAndUnpack (storage storage.Storage , archiver archive.Archiver , metricsManager metrics.MetricsManager , keys []string ) {
71
+ cachedList := sync .OnceValues (storage .List )
70
72
for _ , rawKey := range keys {
71
73
key := NormalizeKey (rawKey )
72
74
if ok , _ := storage .HasKey (key ); ok {
@@ -75,7 +77,7 @@ func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metri
75
77
break
76
78
}
77
79
78
- availableKeys , err := storage . List ()
80
+ availableKeys , err := cachedList ()
79
81
utils .Check (err )
80
82
81
83
matchingKey := findMatchingKey (availableKeys , key )
@@ -90,13 +92,24 @@ func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metri
90
92
}
91
93
92
94
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
+ }
97
111
}
98
112
}
99
-
100
113
return ""
101
114
}
102
115
0 commit comments