-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubparser.go
138 lines (115 loc) · 2.99 KB
/
subparser.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"github.com/EffnerApp/subparser/destination"
"github.com/EffnerApp/subparser/parsers"
"github.com/EffnerApp/subparser/source"
"github.com/mkideal/cli"
"os"
)
const (
ExitInvalidArgs = 1
ExitParserNotFound = 2
ExitFileReadFailed = 3
ExitDSBLoginFailed = 4
ExitLoadingFailed = 5
ExitParsingFailed = 5
ExitWritingFailed = 6
)
type arguments struct {
cli.Helper
Parser string `cli:"parser,P" usage:"name of the parser to use (default: effner) [effner, effner-de]"`
Input string `cli:"input,i" usage:"input file (required if source is file)"`
Source string `cli:"source,s" usage:"source for the data (default: file) [file,dsb,effner]"`
Destination string `cli:"destination,d" usage:"destination where to put the parsed data, (default: sysout) [file]"`
User string `cli:"user,u" usage:"username"`
Pass string `cli:"pass,p" usage:"password"`
Output string `cli:"output,o" usage:"file to output parsed data to (if unset SYSOUT is used)"`
}
func getParser(parser string) parsers.Parser {
switch parser {
case "effner":
return &parsers.EffnerDSBParser{}
case "effner-de":
return &parsers.EffnerDEParser{}
default:
return nil
}
}
func getSource(argv *arguments) source.Source {
switch argv.Source {
case "dsb":
return &source.DSBSource{
User: argv.User,
Pass: argv.Pass,
}
case "file":
return &source.FileSource{
Path: argv.Input,
}
case "effner":
return &source.EffnerDESource{
Password: argv.Pass,
}
default:
return nil
}
}
func getDestination(argv *arguments) destination.Destination {
switch argv.Destination {
case "file":
return &destination.FileDestination{
Path: argv.Output,
}
default:
return &destination.SysoutDestination{}
}
}
func handle(error error, exit int) {
fmt.Println("Error: ", error.Error())
os.Exit(exit)
}
func main() {
os.Exit(cli.Run(new(arguments), func(ctx *cli.Context) error {
argv := ctx.Argv().(*arguments)
parserName := argv.Parser
// validate and load parser
if parserName == "" {
parserName = "effner"
}
parser := getParser(parserName)
if parser == nil {
fmt.Println("Error: Parser not found! Allowed: effner")
os.Exit(ExitParserNotFound)
}
// we can only log to STDOUT if we don't use it to transport the result
canLog := argv.Destination != "stdout" && argv.Destination != ""
src := getSource(argv)
if src == nil {
fmt.Println("Error: Source not found! Allowed: effner, dsb, file")
os.Exit(ExitInvalidArgs)
}
dest := getDestination(argv)
if dest == nil {
fmt.Println("Error: Destination not found! Allowed: file")
os.Exit(ExitInvalidArgs)
}
// prepare the data to parse
data, err := src.Load()
if err != nil {
handle(err, ExitLoadingFailed)
}
plans, err := parser.Parse(data)
if err != nil {
handle(err, ExitParsingFailed)
}
if canLog {
fmt.Println("Parsing completed, Yay!")
}
err = dest.Write(plans)
if err != nil {
handle(err, ExitWritingFailed)
}
return nil
}))
}