forked from johnkerl/miller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readfiles.go
93 lines (80 loc) · 2.29 KB
/
readfiles.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// ================================================================
// Routines for loading strings from files. Nominally for the put/filter verbs
// to load DSL strings from .mlr files.
// ================================================================
package lib
import (
"os"
"strings"
csv "github.com/johnkerl/miller/pkg/go-csv"
)
// LoadStringsFromFileOrDir calls LoadStringFromFile if path exists and is a
// file, or LoadStringsFromDir if path exists and is a directory. In the
// former case the extension is ignored; in the latter case it's used as a
// filter on the directory entries.
func LoadStringsFromFileOrDir(path string, extension string) ([]string, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return nil, err
}
if fileInfo.IsDir() {
return LoadStringsFromDir(path, extension)
} else {
dslString, err := LoadStringFromFile(path)
if err != nil {
return nil, err
} else {
return []string{dslString}, nil
}
}
}
// LoadStringFromFile is just a wrapper around os.ReadFile,
// with a cast from []byte to string.
func LoadStringFromFile(filename string) (string, error) {
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(data), nil
}
// LoadStringsFromDir loads all file contents for files in the given directory
// having the given extension. E.g. LoadStringsFromDir("/u/myfiles", ".mlr")
// will load /u/myfiles/foo.mlr and /u/myfiles/bar.mlr but will skip over
// /u/myfiles/data.csv and /u/myfiles/todo.txt.
func LoadStringsFromDir(dirname string, extension string) ([]string, error) {
dslStrings := make([]string, 0)
f, err := os.Open(dirname)
if err != nil {
return nil, err
}
defer f.Close()
names, err := f.Readdirnames(-1)
if err != nil {
return nil, err
}
for _, name := range names {
if !strings.HasSuffix(name, extension) {
continue
}
path := dirname + "/" + name
dslString, err := LoadStringFromFile(path)
if err != nil {
return nil, err
}
dslStrings = append(dslStrings, dslString)
}
return dslStrings, nil
}
func ReadCSVHeader(filename string) ([]string, error) {
handle, err := os.Open(filename)
if err != nil {
return nil, err
}
defer handle.Close()
csvReader := csv.NewReader(handle)
header, err := csvReader.Read()
if err != nil {
return nil, err
}
return header, nil
}