-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added caching which can be turned on by setting an enviornment variable on local to speedup your local dev loops while running pak [#181404983] Signed-off-by: James Norman <normanja@vmware.com>
- Loading branch information
1 parent
114c3cd
commit 4126e7c
Showing
6 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package packer | ||
|
||
import ( | ||
"crypto/md5" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path" | ||
|
||
cp "github.com/otiai10/copy" | ||
) | ||
|
||
func copyFromCache(cachePath string, source string, destination string) bool { | ||
if cachePath == "" { | ||
return false | ||
} | ||
cacheKey := buildCacheKey(cachePath, source) | ||
if _, err := os.Stat(cacheKey); err == nil { | ||
cp.Copy(cacheKey, destination) | ||
log.Println("\t", source, "found in cache at", cacheKey) | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
func populateCache(cachePath string, source string, destination string) { | ||
if cachePath == "" { | ||
return | ||
} | ||
cacheKey := buildCacheKey(cachePath, source) | ||
err := cp.Copy(destination, cacheKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func buildCacheKey(cachePath string, source string) string { | ||
return path.Join(cachePath, fmt.Sprintf("%x", md5.Sum([]byte(source)))) | ||
} | ||
|
||
func cachedFetchFile(getter func(source string, destination string) error, source, destination, cachePath string) error { | ||
if cachePath == "" { | ||
return getter(source, destination) | ||
} | ||
|
||
if copyFromCache(cachePath, source, destination) { | ||
return nil | ||
} | ||
err := getter(source, destination) | ||
if err != nil { | ||
return err | ||
} | ||
populateCache(cachePath, source, destination) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters