|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +func contains(s []string, e string) bool { |
| 11 | + for _, a := range s { |
| 12 | + if a == e { |
| 13 | + return true |
| 14 | + } |
| 15 | + } |
| 16 | + return false |
| 17 | +} |
| 18 | + |
| 19 | +func getFilesInDirectory(path string, extensions []string) []string { |
| 20 | + var outputSlice []string |
| 21 | + err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error { |
| 22 | + // Check if the file is a directory, if it is recurse and grab it's files |
| 23 | + // If its a file check its extension, if it matches one of the provided |
| 24 | + // file extensions, append it to the output array |
| 25 | + if info.IsDir() { |
| 26 | + if filePath != path { |
| 27 | + outputSlice = append(outputSlice, getFilesInDirectory(filePath, extensions)...) |
| 28 | + } |
| 29 | + } else { |
| 30 | + if contains(extensions, strings.Replace(filepath.Ext(filePath), ".", "", 1)) { |
| 31 | + outputSlice = append(outputSlice, filePath) |
| 32 | + } |
| 33 | + } |
| 34 | + return nil |
| 35 | + }) |
| 36 | + |
| 37 | + if err != nil { |
| 38 | + panic(err) |
| 39 | + } |
| 40 | + return outputSlice |
| 41 | +} |
| 42 | + |
| 43 | +func main() { |
| 44 | + args := os.Args[1:] |
| 45 | + extensions := os.Args[2:] |
| 46 | + if len(args) <= 0 { |
| 47 | + fmt.Printf("Not enough arguments\n") |
| 48 | + return |
| 49 | + } |
| 50 | + if len(extensions) <= 0 { |
| 51 | + fmt.Printf("Not enough extensions\n") |
| 52 | + return |
| 53 | + } |
| 54 | + var files []string |
| 55 | + var filesNoDuplicates []string |
| 56 | + files = getFilesInDirectory(args[0], extensions) |
| 57 | + for i := 0; i < len(files); i++ { |
| 58 | + if contains(filesNoDuplicates, files[i]) == false { |
| 59 | + filesNoDuplicates = append(filesNoDuplicates, files[i]) |
| 60 | + } |
| 61 | + } |
| 62 | + var output string |
| 63 | + for i := 0; i < len(filesNoDuplicates); i++ { |
| 64 | + output += filesNoDuplicates[i] + " " |
| 65 | + } |
| 66 | + fmt.Printf(output + "\n") |
| 67 | +} |
0 commit comments