Skip to content

Commit b2c6312

Browse files
committed
Added support back for -o shorthand for --output and added --output=bytes support
1 parent 567c271 commit b2c6312

File tree

5 files changed

+136
-6
lines changed

5 files changed

+136
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Operators, you should copy/paste content of this content straight to your projec
88

99
If you were at `firehose-core` version `1.0.0` and are bumping to `1.1.0`, you should copy the content between those 2 version to your own repository, replacing placeholder value `fire{chain}` with your chain's own binary.
1010

11+
## Unreleased
12+
13+
* Added support for `--output=bytes` mode which prints the chain's specific Protobuf block as bytes, the encoding for the bytes string printed is determined by `--bytes-encoding`, uses `hex` by default.
14+
15+
* Added back `-o` as shortand for `--output` in `firecore tools ...` sub-commands.
16+
1117
## v1.7.1
1218

1319
* Add back `grpc.health.v1.Health` service to `firehose` and `substreams-tier1` services (regression in 1.7.0)

cmd/tools/print/printer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ func GetOutputPrinter(cmd *cobra.Command, chainFileDescriptor protoreflect.FileD
7777
return NewTextOutputPrinter(bytesEncoding, registry, printTransactions), nil
7878
}
7979

80+
if printer == "bytes" {
81+
return NewBytesOutputPrinter(bytesEncoding, registry), nil
82+
}
83+
8084
return nil, fmt.Errorf("unsupported output printer %q", printer)
8185
}
8286

8387
//go:generate go-enum -f=$GOFILE --marshal --names --nocase
8488

85-
// ENUM(Text, JSON, JSONL, ProtoJSON, ProtoJSONL)
89+
// ENUM(Text, JSON, JSONL, ProtoJSON, ProtoJSONL, Bytes)
8690
type PrintOutputMode uint
8791

8892
type OutputPrinter interface {

cmd/tools/print/printer_bytes.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2021 dfuse Platform Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package print
16+
17+
import (
18+
"encoding/base64"
19+
"encoding/hex"
20+
"fmt"
21+
"io"
22+
23+
"github.com/mr-tron/base58"
24+
pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1"
25+
fcproto "github.com/streamingfast/firehose-core/proto"
26+
pbfirehose "github.com/streamingfast/pbgo/sf/firehose/v2"
27+
"google.golang.org/protobuf/proto"
28+
)
29+
30+
var _ OutputPrinter = (*BytesOutputPrinter)(nil)
31+
32+
type BytesOutputPrinter struct {
33+
bytesEncoding string
34+
registry *fcproto.Registry
35+
}
36+
37+
func NewBytesOutputPrinter(bytesEncoding string, registry *fcproto.Registry) *BytesOutputPrinter {
38+
return &BytesOutputPrinter{
39+
bytesEncoding: bytesEncoding,
40+
registry: registry,
41+
}
42+
}
43+
44+
func (p *BytesOutputPrinter) PrintTo(input any, out io.Writer) error {
45+
if pbblock, ok := input.(*pbbstream.Block); ok {
46+
return p.printBytes(pbblock.Payload.Value, out)
47+
}
48+
49+
if v, ok := input.(*pbfirehose.Response); ok {
50+
return p.printBytes(v.Block.Value, out)
51+
}
52+
53+
if v, ok := input.(*pbfirehose.SingleBlockResponse); ok {
54+
return p.printBytes(v.Block.Value, out)
55+
}
56+
57+
if v, ok := input.(proto.Message); ok {
58+
data, err := proto.Marshal(v)
59+
if err != nil {
60+
return fmt.Errorf("unable to marshal proto message: %w", err)
61+
}
62+
63+
return p.printBytes(data, out)
64+
}
65+
66+
return fmt.Errorf("unsupported type %T", input)
67+
}
68+
69+
var base64Encoding = base64.StdEncoding
70+
71+
func (p *BytesOutputPrinter) printBytes(data []byte, out io.Writer) error {
72+
var err error
73+
switch p.bytesEncoding {
74+
case "hex":
75+
err = p.printBytesHex(data, out)
76+
case "base58":
77+
err = p.printBytesBase58(data, out)
78+
case "base64":
79+
err = p.printBytesBase64(data, out)
80+
default:
81+
return fmt.Errorf("unsupported bytes encoding %q", p.bytesEncoding)
82+
}
83+
if err != nil {
84+
return fmt.Errorf("unable to print bytes: %w", err)
85+
}
86+
87+
return writeStringToWriter(out, "")
88+
}
89+
90+
func (p *BytesOutputPrinter) printBytesHex(data []byte, out io.Writer) error {
91+
encoder := hex.NewEncoder(out)
92+
_, err := encoder.Write(data)
93+
return err
94+
}
95+
96+
func (p *BytesOutputPrinter) printBytesBase58(data []byte, out io.Writer) error {
97+
return writeStringToWriter(out, base58.Encode(data))
98+
}
99+
100+
func (p *BytesOutputPrinter) printBytesBase64(data []byte, out io.Writer) error {
101+
encoder := base64.NewEncoder(base64Encoding, out)
102+
// This flushes the base64 encoder but doesn't close the underlying writer, which is
103+
// what we want here
104+
defer encoder.Close()
105+
106+
_, err := encoder.Write(data)
107+
return err
108+
}

cmd/tools/print/printer_enum.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/tools/tools.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,27 @@ func ConfigureToolsCmd[B firecore.Block](
4141
tracer logging.Tracer,
4242
) error {
4343
if flags := ToolsCmd.PersistentFlags(); flags != nil {
44-
flags.String("output", "", cli.Dedent(`
44+
flags.StringP("output", "o", "", cli.Dedent(`
4545
The default output printer to use to print responses and blocks across
4646
tools sub-command.
4747
4848
If defined, has precedence over tools specific flags. Bytes encoding is
49-
tried to be respected if possible, protojson and protojsonl are always
50-
using base64 today for compatibility across Protobuf supported languages.
49+
tried to be respected if possible. The 'protojson' and 'protojsonl' output
50+
are always using base64 for bytes values today for compatibility across
51+
Protobuf supported languages. The 'bytes' output is using the bytes-encoding
52+
flag value to determine how to encode Protobuf bytes to the console.
5153
5254
JSON and JSONL have the caveat to print enum value using the integer value
5355
instead of the name which would be more convenient.
5456
5557
ProtoJSON and ProtoJSONL being able to print only Protobuf messages, they
5658
are refused on commands that are not returning Protobuf messages.
5759
58-
One of: text, json, jsonl, protojson, protojsonl
60+
The 'bytes' output marshals the Protobuf message to bytes and prints
61+
the bytes as a string for which the encoding is determined by the
62+
--bytes-encoding flag value, hexadecimal by default.
63+
64+
One of: text, json, jsonl, protojson, protojsonl, bytes
5965
`))
6066

6167
flags.String("bytes-encoding", "hex", "Encoding for bytes fields when printing in 'text', 'json' or 'jsonl' --output, either 'hex', 'base58' or 'base64'")

0 commit comments

Comments
 (0)