-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (70 loc) · 1.53 KB
/
main.go
File metadata and controls
89 lines (70 loc) · 1.53 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
_ "net/http/pprof"
)
const (
// CSV type indicates the file format
CSV string = "csv"
// JSON type indicates the file format
JSON string = "json"
)
var logger = log.New(os.Stdout, "main: ", log.LstdFlags)
var fileList []File
var fileChannel = make(chan []File)
var fileType string
var targetType string
var folderName string
func observeDirectory() {
logger.Printf("observing this directory %s", folderName)
tick := time.Tick(1000 * time.Millisecond)
for range tick {
go trackFiles()
}
}
func main() {
flag.StringVar(&fileType, "filetype", CSV, "input file format")
flag.StringVar(&targetType, "targetType", JSON, "target file format")
flag.StringVar(&folderName, "folder", "C:\\Users\\user\\Desktop\\", "folder name")
flag.Parse()
// for profiling
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
// for profiling
go observeDirectory()
go func() {
for {
select {
case fileList := <-fileChannel:
for i := range fileList {
if !fileList[i].processed {
go processFile(&fileList[i])
}
}
}
}
}()
ctx := shutdown(context.Background())
<-ctx.Done()
}
func shutdown(ctx context.Context) context.Context {
ctx, done := context.WithCancel(ctx)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go func() {
defer done()
<-quit
signal.Stop(quit)
close(quit)
logger.Println("Application is shutting down")
}()
return ctx
}