github.com/vulppine/fotoDen@v0.3.0/generator/file.go (about) 1 package generator 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 ) 8 9 // GetArrayOfFilesAndFolders takes an array of os.FileInfo (usually from from os.Readdir()), and returns a string array of all non-directories. 10 // Also returns a string array of directories, so we don't have to copy and paste this function. 11 func GetArrayOfFilesAndFolders(directory []os.FileInfo) ([]string, []string) { 12 fileArray := make([]string, 0) 13 folderArray := make([]string, 0) 14 15 for i := 0; i < len(directory); i++ { 16 if directory[i].Mode().IsDir() == true { 17 folderArray = append(folderArray, directory[i].Name()) 18 } else { 19 fileArray = append(fileArray, directory[i].Name()) 20 } 21 } 22 23 return fileArray, folderArray 24 } 25 26 // GetArrayOfFiles takes an array of os.FileInfo, and runs it through GetArrayOfFilesAndFolders, returning only the array of files. 27 func GetArrayOfFiles(directory []os.FileInfo) []string { 28 fileArray, _ := GetArrayOfFilesAndFolders(directory) 29 return fileArray 30 } 31 32 // GetArrayOfFolders takes an array of os.FileInfo, and runs it through GetArrayOfFilesAndFolders, returning only the array of folders. 33 func GetArrayOfFolders(directory []os.FileInfo) []string { 34 _, folderArray := GetArrayOfFilesAndFolders(directory) 35 return folderArray 36 } 37 38 // CopyFile takes three arguments - the name of the file, the name of the new file, and the destination. 39 // This assumes the file we're renaming is in the current working directory, or it is reachable 40 // via the current working directory. 41 // 42 // Returns an error if one occurs - otherwise returns nil. 43 func CopyFile(file string, dest string) error { 44 verbose("Copying " + file + " to " + filepath.Join(dest)) 45 fileReader, err := ioutil.ReadFile(file) 46 if err != nil { 47 return err 48 } 49 50 toWrite, err := os.Create(filepath.Join(dest)) 51 defer toWrite.Close() 52 if err != nil { 53 return err 54 } 55 56 _, err = toWrite.Write(fileReader) 57 if err != nil { 58 return err 59 } 60 61 return nil 62 }