Allow @ to be used for -protoset

This commit is contained in:
bufdotbuild 2019-10-14 14:04:40 -04:00
parent ccc9007156
commit 07d2568d01
2 changed files with 28 additions and 3 deletions

View File

@ -72,7 +72,8 @@ var (
requests. It defaults to the given address.`)) requests. It defaults to the given address.`))
data = flags.String("d", "", prettify(` data = flags.String("d", "", prettify(`
Data for request contents. If the value is '@' then the request contents Data for request contents. If the value is '@' then the request contents
are read from stdin. For calls that accept a stream of requests, the are read from stdin. Note that '@' cannot be used for both -data and -protoset
in the same invocation. For calls that accept a stream of requests, the
contents should include all such request messages concatenated together contents should include all such request messages concatenated together
(possibly delimited; see -format).`)) (possibly delimited; see -format).`))
format = flags.String("format", "json", prettify(` format = flags.String("format", "json", prettify(`
@ -139,7 +140,9 @@ func init() {
those exposed by the remote server), and the 'describe' action describes those exposed by the remote server), and the 'describe' action describes
symbols found in the given descriptors. May specify more than one via symbols found in the given descriptors. May specify more than one via
multiple -protoset flags. It is an error to use both -protoset and multiple -protoset flags. It is an error to use both -protoset and
-proto flags.`)) -proto flags. If the value is '@' then the protoset is read from stdin.
Note that '@' cannot be used for both -data and -protoset in the same
invocation.`))
flags.Var(&protoFiles, "proto", prettify(` flags.Var(&protoFiles, "proto", prettify(`
The name of a proto source file. Source files given will be used to The name of a proto source file. Source files given will be used to
determine the RPC schema instead of querying for it from the remote determine the RPC schema instead of querying for it from the remote
@ -278,6 +281,14 @@ func main() {
warn("The -import-path argument is not used unless -proto files are used.") warn("The -import-path argument is not used unless -proto files are used.")
} }
if *data == "@" && len(protoset) > 0 {
for _, fileName := range protoset {
if fileName == "@" {
fail(nil, "Cannot use '@' for both -data and -protoset.")
}
}
}
ctx := context.Background() ctx := context.Background()
if *maxTime > 0 { if *maxTime > 0 {
timeout := time.Duration(*maxTime * float64(time.Second)) timeout := time.Duration(*maxTime * float64(time.Second))

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"os"
"sync" "sync"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
@ -38,10 +39,23 @@ type DescriptorSource interface {
// DescriptorSourceFromProtoSets creates a DescriptorSource that is backed by the named files, whose contents // DescriptorSourceFromProtoSets creates a DescriptorSource that is backed by the named files, whose contents
// are encoded FileDescriptorSet protos. // are encoded FileDescriptorSet protos.
//
// The special fileName "@" denotes to read from os.Stdin. Multiple "@" values will result
// in only one read from stdin.
func DescriptorSourceFromProtoSets(fileNames ...string) (DescriptorSource, error) { func DescriptorSourceFromProtoSets(fileNames ...string) (DescriptorSource, error) {
stdinProcessed := false
files := &descpb.FileDescriptorSet{} files := &descpb.FileDescriptorSet{}
for _, fileName := range fileNames { for _, fileName := range fileNames {
b, err := ioutil.ReadFile(fileName) var b []byte
var err error
if fileName == "@" {
if !stdinProcessed {
stdinProcessed = true
b, err = ioutil.ReadAll(os.Stdin)
}
} else {
b, err = ioutil.ReadFile(fileName)
}
if err != nil { if err != nil {
return nil, fmt.Errorf("could not load protoset file %q: %v", fileName, err) return nil, fmt.Errorf("could not load protoset file %q: %v", fileName, err)
} }