1package filesystem 2 3import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 8 "github.com/pkg/errors" 9) 10 11type mapperFn func([]string) (interface{}, error) 12 13func CSVFileToEntities(csvFile string, mapper mapperFn) ([]interface{}, error) { 14 var errMapping error 15 var entityRows []interface{} 16 17 errReading := GenerateCSVLines( 18 csvFile, 19 func(columns []string) { 20 if errMapping != nil { 21 return 22 } 23 var entity interface{} 24 entity, errMapping = mapper(columns) 25 if errMapping == nil { 26 entityRows = append(entityRows, entity) 27 } 28 }, 29 ) 30 if errReading != nil { 31 return nil, errors.Wrap(errReading, fmt.Sprintf("Error reading %s file from filesystem", csvFile)) 32 } 33 if errMapping != nil { 34 return nil, errors.Wrap(errMapping, "Error mapping CSV lines to entities") 35 } 36 return entityRows, nil 37} 38 39func FindFnamesInDir(directory string, filenames ...string) []string { 40 var outputFilenames []string 41 for _, filename := range filenames { 42 findProjectsCmd := fmt.Sprintf("find %s | grep %s", directory, filename) 43 out, err := exec.Command("bash", "-c", findProjectsCmd).Output() 44 if err != nil { 45 return nil 46 } 47 outputFilenames = append( 48 outputFilenames, 49 filterEmptyStrings(strings.Split(string(out), "\n"))..., 50 ) 51 } 52 return outputFilenames 53} 54 55func filterEmptyStrings(strings []string) []string { 56 filtered := make([]string, len(strings)-countEmpty(strings)) 57 copyToIndex := 0 58 for _, str := range strings { 59 if str != "" { 60 filtered[copyToIndex] = str 61 copyToIndex++ 62 } 63 } 64 return filtered 65} 66 67func countEmpty(strings []string) int { 68 numEmpty := 0 69 for _, str := range strings { 70 if str == "" { 71 numEmpty++ 72 } 73 } 74 return numEmpty 75} 76