forked from gophergala/gomua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.go
74 lines (65 loc) · 1.18 KB
/
scan.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
package gomua
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/mail"
"os"
"path/filepath"
)
var msgs []*Message
func processFile(filename string, in io.Reader, stdin bool) error {
if in == nil {
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
in = bytes.NewReader(b)
/*
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
in = f
*/
}
m, err := mail.ReadMessage(in)
if err != nil {
return err
}
msg := ReadMessage(m)
msg.Filename, err = filepath.Abs(filename)
if err != nil {
return err
}
msgs = append(msgs, msg)
return nil
}
func visitFile(path string, f os.FileInfo, err error) error {
if err == nil {
err = processFile(path, nil, false)
}
if err != nil {
// This was printing errors when reaading a directory, nerfing the msg for now.
//fmt.Printf("%s", err)
}
return nil
}
func walkDir(path string) {
filepath.Walk(path, visitFile)
}
func Scan(path string) []*Message {
switch dir, err := os.Stat(path); {
case err != nil:
fmt.Printf("%s", err)
case dir.IsDir():
walkDir(path)
default:
if err := processFile(path, nil, false); err != nil {
fmt.Printf("%s", err)
}
}
return msgs
}