-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (57 loc) · 1.25 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
package main
import (
"bufio"
"encoding/base64"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"os"
"github.com/susji/ruuvi/data/rawv2"
)
func main() {
var mode string
flag.StringVar(&mode, "mode", "hex", "Input mode: raw, hex, or base64")
flag.Parse()
var dec func([]byte) ([]byte, error)
switch mode {
case "raw":
dec = func(b []byte) ([]byte, error) {
return b, nil
}
case "hex":
dec = func(b []byte) ([]byte, error) {
return hex.AppendDecode([]byte{}, b)
}
case "base64":
dec = func(b []byte) ([]byte, error) {
return base64.StdEncoding.AppendDecode([]byte{}, b)
}
default:
fmt.Fprintln(os.Stderr, "unknown mode:", mode)
os.Exit(1)
}
s := bufio.NewScanner(os.Stdin)
fmt.Fprintln(os.Stderr, "reading standard input as", mode)
for s.Scan() {
d, err := dec(s.Bytes())
if err != nil {
fmt.Fprintln(os.Stderr, "decoding input failed:", err)
continue
}
p, err := rawv2.Parse(d)
if err != nil {
fmt.Fprintln(os.Stderr, "parsing input failed:", err)
continue
}
out, err := json.Marshal(&p)
if err != nil {
fmt.Fprintln(os.Stderr, "packet json marshal failed:", err)
continue
}
fmt.Println(string(out))
}
if err := s.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading input failed:", err)
}
}