Skip to content

Improve 'cache restore' command #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 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,7 @@ func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metri
break
}

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

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

func findMatchingKey(availableKeys []storage.CacheKey, match string) string {
for _, availableKey := range availableKeys {
isMatch, _ := regexp.MatchString(match, availableKey.Name)
if isMatch {
return availableKey.Name
// If the key has no regex characters, just use strings.Contains
if regexp.QuoteMeta(match) == match {
for _, availableKey := range availableKeys {
if strings.Contains(availableKey.Name, match) {
return availableKey.Name
}
}
} else {
pattern, err := regexp.Compile(match)
if err != nil {
return ""
}
for _, availableKey := range availableKeys {
if pattern.MatchString(availableKey.Name) {
return availableKey.Name
}
}
}

return ""
}

Expand Down