-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (100 loc) · 2.36 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/gookit/color"
"github.com/victorgama/tlvp/cmd"
"github.com/victorgama/tlvp/printer"
"github.com/victorgama/tlvp/tlv"
)
var help = flag.Bool("help", false, "Displays help")
var describe = flag.Bool("describe", false, "Shows tags description")
var noColor = flag.Bool("no-color", false, "Disables color output")
var forceColor = flag.Bool("force-color", false, "Forces color output")
var isPDOL = flag.Bool("pdol", false, "Reads input as a PDOL")
func main() {
flag.Parse()
if *help {
printHelp()
return
}
if *forceColor {
color.ForceOpenColor()
}
if *noColor {
color.Disable()
}
var (
data []byte
err error
reader io.Reader
)
if cmd.FromStdin() {
data, err = ioutil.ReadAll(os.Stdin)
if err != nil {
color.Error.Tips("tlvp found an error processing input: %s", err)
os.Exit(1)
}
reader = bytes.NewReader(data)
}
if flag.NArg() == 1 {
filePath := flag.Arg(0)
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
color.Error.Tips("tlvp could not find a file named \"%s\". Please ensure the path is correct and try again.", filePath)
os.Exit(1)
}
if err != nil {
color.Error.Tips("tlvp found an error opening \"%s\": %s", filePath, err)
os.Exit(1)
}
data, err = ioutil.ReadFile(filePath)
if err != nil {
color.Error.Tips("tlvp found an error processing %s: %s", filePath, err)
os.Exit(1)
}
reader = bytes.NewReader(data)
}
if reader == nil {
printHelp()
os.Exit(1)
}
parser, err := tlv.NewParser(reader, *isPDOL)
if err != nil {
color.Error.Tips("Error parsing: %s", err)
os.Exit(1)
}
entries, err := parser.Parse()
if err != nil {
color.Error.Tips("Error parsing: %s", err)
os.Exit(1)
}
printer.Print(entries, *describe)
}
func printHelp() {
fmt.Printf(`tlvp is a TLV parser for EMV data
Usage:
tlvp [--describe] [--force-color|--no-color] [--help] [input]
--describe:
Outputs extra descriptions of tags
--force-color:
Forces color formatting
--help:
Displays this message
--no-color:
Disables color formatting
--pdol:
Reads data as a PDOL.
[input]:
Path to a file to be parsed. Standard input can also be used by piping
data into tlvp.
Bug reports:
Please file an issue: https://github.com/victorgama/tlvp/issues/new
License:
MIT. Copyright (C) 2020 - Victor Gama ([email protected])
`)
}