-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.go
More file actions
75 lines (61 loc) · 1.28 KB
/
Copy pathfiles.go
File metadata and controls
75 lines (61 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package csv_cli_tool
import (
"fmt"
"os"
"path/filepath"
)
func getCSVFileOrFilesInPath(path string, startRow int, endRow int) ([]string, error) {
if !isDir(path) {
fileName := path
if err := isCSVFile(fileName); err != nil {
return nil, err
}
return []string{fileName}, nil
}
files, err := findCSVFiles(path, startRow, endRow)
if err != nil {
return nil, err
}
return files, nil
}
func isCSVFile(fileName string) error {
matched, err := filepath.Match("*.csv", filepath.Base(fileName))
if err != nil {
return err
}
if !matched {
return fmt.Errorf("%s is not a csv file", fileName)
}
return nil
}
func isDir(path string) bool {
info, err := os.Stat(path)
if err != nil {
fmt.Println(err)
}
return err == nil && info.IsDir()
}
func findCSVFiles(root string, startRow int, endRow int) ([]string, error) {
var matches []string
i := 1
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if err := isCSVFile(path); err != nil {
fmt.Println(err)
return nil
}
if (i >= startRow && i <= endRow) || (startRow == -1 && endRow == -1) {
matches = append(matches, path)
}
return nil
})
if err != nil {
return nil, err
}
return matches, nil
}