This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
76 lines (70 loc) · 1.6 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
// 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 archivist
import (
"os"
"io"
"fmt"
"path"
"strings"
"encoding/json"
"compress/gzip"
"github.com/stellar/go-stellar-base/xdr"
)
func DumpXdrAsJson(args []string) error {
var lhe xdr.LedgerHeaderHistoryEntry
var the xdr.TransactionHistoryEntry
var thre xdr.TransactionHistoryResultEntry
var bke xdr.BucketEntry
var scp xdr.ScpHistoryEntry
var tmp interface{}
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)
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)
}
xr := NewXdrStream(rdr)
n := 0
for {
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
}