forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
79 lines (70 loc) · 1.62 KB
/
json.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
// Copyright 2016 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
package historyarchive
import (
"compress/gzip"
"encoding/json"
"fmt"
"io"
"os"
"path"
"strings"
"github.com/pownieh/stellar_go/xdr"
)
func DumpXdrAsJson(args []string) error {
var tmp xdr.DecoderFrom
var rdr io.ReadCloser
var err error
for _, arg := range args {
rdr, err = os.Open(arg)
if err != nil {
return err
}
if strings.HasSuffix(arg, ".gz") {
rdr, err = gzip.NewReader(rdr)
if err != nil {
return err
}
}
base := path.Base(arg)
xr := NewXdrStream(rdr)
n := 0
for {
var lhe xdr.LedgerHeaderHistoryEntry
var the xdr.TransactionHistoryEntry
var thre xdr.TransactionHistoryResultEntry
var bke xdr.BucketEntry
var scp xdr.ScpHistoryEntry
if strings.HasPrefix(base, "bucket") {
tmp = &bke
} else if strings.HasPrefix(base, "ledger") {
tmp = &lhe
} else if strings.HasPrefix(base, "transactions") {
tmp = &the
} else if strings.HasPrefix(base, "results") {
tmp = &thre
} else if strings.HasPrefix(base, "scp") {
tmp = &scp
} else {
return fmt.Errorf("Error: unrecognized XDR file type %s", base)
}
if err = xr.ReadOne(tmp); err != nil {
if err == io.EOF {
break
} else {
return fmt.Errorf("Error on XDR record %d of %s",
n, arg)
}
}
n++
buf, err := json.MarshalIndent(tmp, "", " ")
if err != nil {
return err
}
os.Stdout.Write(buf)
}
xr.Close()
}
return nil
}