github.com/hashicorp/packer@v1.14.3/command/utils_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package command 5 6 import ( 7 "log" 8 "os" 9 "path/filepath" 10 ) 11 12 func mustString(s string, e error) string { 13 if e != nil { 14 panic(e) 15 } 16 return s 17 } 18 19 func createFiles(dir string, content map[string]string) { 20 for relPath, content := range content { 21 contentPath := filepath.Join(dir, relPath) 22 if err := os.MkdirAll(filepath.Dir(contentPath), 0777); err != nil { 23 panic(err) 24 } 25 if err := os.WriteFile(contentPath, []byte(content), 0666); err != nil { 26 panic(err) 27 } 28 log.Printf("created tmp file: %s", contentPath) 29 } 30 } 31 32 type configDirSingleton struct { 33 dirs map[string]string 34 } 35 36 // when you call dir twice with the same key, the result should be the same 37 func (c *configDirSingleton) dir(key string) string { 38 if v, exists := c.dirs[key]; exists { 39 return v 40 } 41 c.dirs[key] = mustString(os.MkdirTemp("", "pkr-test-cfg-dir-"+key)) 42 return c.dirs[key] 43 } 44 45 // fileExists returns true if the filename is found 46 func fileExists(filename string) bool { 47 if _, err := os.Stat(filename); err == nil { 48 return true 49 } 50 return false 51 }