-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (79 loc) · 2.03 KB
/
main.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
94
95
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"github.com/mauricioklein/text-search-engine/ranking"
"github.com/mauricioklein/text-search-engine/reader"
"github.com/mauricioklein/text-search-engine/report"
)
// CliArgs defines the command line arguments
// provided in the program execution
type CliArgs struct {
DirPath string
Reader string
Reporter string
RankAlgo string
NWorkers int
}
var inStream io.Reader = os.Stdin
var outStream io.Writer = os.Stdout
var errStream io.Writer = os.Stderr
func main() {
// parse command line arguments
args := parseCliFlags()
// instantiate the file reader
reader := instantiateReader(args.Reader)
// read the files
files, err := reader.Read(args.DirPath)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d file(s) read from the directory %s\n", len(files), args.DirPath)
// instantiate the necessary resources
reporter := instantiateReporter(args.Reporter)
rankAlgo := instantiateRankingAlgorithm(args.RankAlgo)
processor := ranking.NewProcessor(files, args.NWorkers, rankAlgo)
NewConsole(
processor,
reporter,
inStream,
outStream,
errStream,
).Run()
}
func parseCliFlags() CliArgs {
path := flag.String("directory", "", "Directory to be read")
reader := flag.String("reader", "disk", "The file reader to be used")
reporter := flag.String("reporter", "simple", "The result reporter to be used")
rankAlgo := flag.String("rank", "levenshtein", "The rank algorithm to be used")
nWorkers := flag.Int("workers", 3, "Number of paralel workers")
flag.Parse()
return CliArgs{
DirPath: *path,
Reader: *reader,
Reporter: *reporter,
RankAlgo: *rankAlgo,
NWorkers: *nWorkers,
}
}
func instantiateReader(readerType string) reader.Reader {
switch readerType {
default:
return reader.Disk{}
}
}
func instantiateReporter(reporterType string) report.Reporter {
switch reporterType {
default:
return report.SimpleReporter{}
}
}
func instantiateRankingAlgorithm(algType string) ranking.Algorithm {
switch algType {
default:
return ranking.LevenshteinRanking{}
}
}