-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (50 loc) · 1.14 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
// Command lmr is the main entrypoint for little map reduce.
package main
import (
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"github.com/joshkunz/lmr/chunker"
"github.com/joshkunz/lmr/mapper"
"github.com/joshkunz/lmr/runner"
)
func concatReducer(_ string, paths []string, out io.Writer) error {
for _, path := range paths {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open %q: %w", path, err)
}
if _, err := io.Copy(out, f); err != nil {
f.Close()
return fmt.Errorf("failed to read %q: %w", path, err)
}
f.Close()
}
return nil
}
func main() {
cmd := cobra.Command{
Use: "lmr",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("a mapper must be provided")
}
if len(args) > 1 {
return fmt.Errorf("too many command line arguments")
}
m, err := mapper.Exec(args[0])
if err != nil {
return err
}
r := runner.ReducerFunc(concatReducer)
chunker := chunker.NewLine(os.Stdin)
rnr, err := runner.NewRunner(m, r, ".lmr")
if err != nil {
return err
}
return rnr.Run(cmd.Context(), chunker)
},
}
cmd.Execute()
}