forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
49 lines (43 loc) · 1.03 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package command
import (
"io/ioutil"
"log"
"os"
"path/filepath"
)
func mustString(s string, e error) string {
if e != nil {
panic(e)
}
return s
}
func createFiles(dir string, content map[string]string) {
for relPath, content := range content {
contentPath := filepath.Join(dir, relPath)
if err := os.MkdirAll(filepath.Dir(contentPath), 0777); err != nil {
panic(err)
}
if err := ioutil.WriteFile(contentPath, []byte(content), 0666); err != nil {
panic(err)
}
log.Printf("created tmp file: %s", contentPath)
}
}
type configDirSingleton struct {
dirs map[string]string
}
// when you call dir twice with the same key, the result should be the same
func (c *configDirSingleton) dir(key string) string {
if v, exists := c.dirs[key]; exists {
return v
}
c.dirs[key] = mustString(ioutil.TempDir("", "pkr-test-cfg-dir-"+key))
return c.dirs[key]
}
// fileExists returns true if the filename is found
func fileExists(filename string) bool {
if _, err := os.Stat(filename); err == nil {
return true
}
return false
}