github.com/hashicorp/packer@v1.14.3/packer_test/common/check/file_gadgets.go (about) 1 package check 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 ) 8 9 type fileExists struct { 10 filepath string 11 isDir bool 12 } 13 14 func (fe fileExists) Check(_, _ string, _ error) error { 15 st, err := os.Stat(fe.filepath) 16 if err != nil { 17 return fmt.Errorf("failed to stat %q: %s", fe.filepath, err) 18 } 19 20 if st.IsDir() && !fe.isDir { 21 return fmt.Errorf("file %q is a directory, wasn't supposed to be", fe.filepath) 22 } 23 24 if !st.IsDir() && fe.isDir { 25 return fmt.Errorf("file %q is not a directory, was supposed to be", fe.filepath) 26 } 27 28 return nil 29 } 30 31 func FileExists(filePath string, isDir bool) Checker { 32 return fileExists{ 33 filepath: filePath, 34 isDir: isDir, 35 } 36 } 37 38 type fileGlob struct { 39 filepath string 40 } 41 42 func (fe fileGlob) Check(_, _ string, _ error) error { 43 matches, err := filepath.Glob(fe.filepath) 44 if err != nil { 45 return fmt.Errorf("error evaluating file glob pattern %q: %v", fe.filepath, err) 46 } 47 48 if len(matches) == 0 { 49 return fmt.Errorf("no matches found for file glob pattern %q", fe.filepath) 50 } 51 52 return nil 53 } 54 55 func FileGlob(filename string) Checker { 56 return fileGlob{ 57 filepath: filename, 58 } 59 }