Commit 1bd9288c authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Use strings operations, faster than regexp and easy

/cc @sit
parent d999d1c5
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"path/filepath" "path/filepath"
"regexp" "strings"
"sync" "sync"
) )
...@@ -72,12 +72,12 @@ func (f *FileCache) RUnlock(key string) { ...@@ -72,12 +72,12 @@ func (f *FileCache) RUnlock(key string) {
} }
func (f *FileCache) cachePath(key string, hashKey string) string { func (f *FileCache) cachePath(key string, hashKey string) string {
var suffixPattern = regexp.MustCompile(`(\.\w+)$`)
matches := suffixPattern.FindStringSubmatch(key)
suffix := "" suffix := ""
if matches != nil { dotIndex := strings.LastIndex(key, ".")
suffix = matches[0] if dotIndex > -1 {
suffix = key[dotIndex:len(key)]
} }
return filepath.Join(f.CacheDir, hashKey+suffix) return filepath.Join(f.CacheDir, hashKey+suffix)
} }
......
...@@ -42,6 +42,7 @@ func TestFileCache(t *testing.T) { ...@@ -42,6 +42,7 @@ func TestFileCache(t *testing.T) {
if !strings.HasSuffix(path, ".iso") { if !strings.HasSuffix(path, ".iso") {
t.Fatalf("path doesn't end with suffix '%s': '%s'", ".iso", path) t.Fatalf("path doesn't end with suffix '%s': '%s'", ".iso", path)
} }
err = ioutil.WriteFile(path, []byte("data"), 0666) err = ioutil.WriteFile(path, []byte("data"), 0666)
if err != nil { if err != nil {
t.Fatalf("error writing: %s", err) t.Fatalf("error writing: %s", err)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment