mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-25 17:24:00 -04:00
packer: Use strings operations, faster than regexp and easy
/cc @sit
This commit is contained in:
+5
-5
@@ -4,7 +4,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -72,12 +72,12 @@ func (f *FileCache) RUnlock(key string) {
|
||||
}
|
||||
|
||||
func (f *FileCache) cachePath(key string, hashKey string) string {
|
||||
var suffixPattern = regexp.MustCompile(`(\.\w+)$`)
|
||||
matches := suffixPattern.FindStringSubmatch(key)
|
||||
suffix := ""
|
||||
if matches != nil {
|
||||
suffix = matches[0]
|
||||
dotIndex := strings.LastIndex(key, ".")
|
||||
if dotIndex > -1 {
|
||||
suffix = key[dotIndex:len(key)]
|
||||
}
|
||||
|
||||
return filepath.Join(f.CacheDir, hashKey+suffix)
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ func TestFileCache(t *testing.T) {
|
||||
if !strings.HasSuffix(path, ".iso") {
|
||||
t.Fatalf("path doesn't end with suffix '%s': '%s'", ".iso", path)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(path, []byte("data"), 0666)
|
||||
if err != nil {
|
||||
t.Fatalf("error writing: %s", err)
|
||||
|
||||
Reference in New Issue
Block a user