-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (88 loc) · 3.42 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
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"strings"
"log"
"os"
"github.com/davidporter-id-au/small-join/smalljoin"
)
func main() {
var joinStr string
var join smalljoin.Jointype
var rightIndexFile string
var rightExecStr string
var lSeparator string
var lJsonSubquery string
var lJoinColumn int
var rSeparator string
var rJsonSubquery string
var rJoinColumn int
var debugMode bool
var continueOnError bool
var attemptToClean bool
flag.StringVar(&rightIndexFile, "right", "", "the right side of the join file with the incoming stream, ie the indexes to read in")
flag.StringVar(&rightExecStr, "right-exec-with-exit-code", "", "A bash string to execute to execute for each line, to attempt to join on")
flag.StringVar(&joinStr, "join", "inner", "options: [inner|left|right-is-null] The 'sql' type of join to apply on the two data streams")
flag.BoolVar(&debugMode, "verbose", false, "output debug information")
flag.BoolVar(&continueOnError, "continue", false, "continue on error")
flag.BoolVar(&attemptToClean, "clean", true, "try to clean up data before joining")
flag.StringVar(&lSeparator, "left-separator", ",", "a separator for the incoming stream")
flag.StringVar(&lJsonSubquery, "left-json-subquery", "", "the JMES path to query and do a join on")
flag.IntVar(&lJoinColumn, "left-join-column", -1, "the column number with which to attempt to join on. -1 imples there's no columns and to join on the entire row")
flag.StringVar(&rSeparator, "right-separator", "", "a separator for the index file's columns with which to split it (eg, a comman for CSVs)")
flag.StringVar(&rJsonSubquery, "right-json-subquery", "", "the JMES path to query and do a join on (if the contents of the column are JSON)")
flag.IntVar(&rJoinColumn, "right-column", -1, "the column number with which to attempt to join on if there's a need to join only on a single column. \n-1 implies there's no clumns and join on the entire row")
flag.Parse()
if rightIndexFile != "" && rightExecStr != "" {
log.Fatalf("Only an index file or exec string can be specified, not both")
}
if rightIndexFile == "" && rightExecStr == "" {
log.Fatalf("An input from the right-side of the on is required. Use --help to see options")
}
if rightIndexFile != "" {
s, err := os.Stat(rightIndexFile)
if err != nil {
log.Fatalf("Could not read right join file: %v, file: %q", err, rightIndexFile)
}
if s.IsDir() {
log.Fatalf("not a valid file to join on")
}
}
switch strings.ToLower(joinStr) {
case "inner":
join = smalljoin.JoinTypeInner
case "left":
join = smalljoin.JoinTypeLeft
case "right-is-null":
join = smalljoin.JoinTypeRightIsNull
default:
log.Fatalf("not a valid join %q, options are: 'inner', 'left', 'right-is-null'\n", joinStr)
}
joiner := smalljoin.New(
os.Stdin,
os.Stdout,
os.Stderr,
smalljoin.Options{
IndexFile: rightIndexFile,
RightExecStr: rightExecStr,
Jointype: join,
OutputDebugMode: debugMode,
ContinueOnErr: continueOnError,
LeftQueryOptions: smalljoin.QueryOptions{
JoinColumn: lJoinColumn,
Separator: lSeparator,
JsonSubquery: lJsonSubquery,
AttemptToClean: attemptToClean,
},
RightQueryOptions: smalljoin.QueryOptions{
JoinColumn: rJoinColumn,
Separator: rSeparator,
JsonSubquery: rJsonSubquery,
AttemptToClean: attemptToClean,
},
})
err := joiner.Run()
if err != nil {
log.Fatalf("Fatal error while trying to join: %s", err)
}
}