-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.go
39 lines (32 loc) · 883 Bytes
/
watch.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
package main
import (
"io/ioutil"
"strings"
"time"
elastic "gopkg.in/olivere/elastic.v5"
log "github.com/Sirupsen/logrus"
)
func watchDir(dirName string, client *elastic.Client, index string) ([]string, error) {
allfiles, err := ioutil.ReadDir(dirName)
if err != nil {
log.Error("Error in reading directory", err)
return nil, err
}
// Get only files which are written fully
var files []string
for _, file := range allfiles {
// Check if it is a file and have not been modified atleast since last minute
if file.Mode().IsRegular() && file.ModTime().Add(60*time.Second).Unix() < time.Now().Unix() {
files = append(files, file.Name())
}
}
var newFiles []string
for _, file := range files {
if strings.Contains(file, srcExtension) {
if !isFileIndexed(file, client, index) {
newFiles = append(newFiles, file)
}
}
}
return newFiles, nil
}