diff --git a/config.go b/config.go index f01ce53..c8038c6 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "math" "os" + "path/filepath" "strings" "time" @@ -62,19 +63,31 @@ func (c *Config) InitDefaults() error { //nolint:gocyclo,gocognit return errors.E(op, errors.Errorf("malformed grpc address, provided: %s", c.Listen)) } - for i := 0; i < len(c.Proto); i++ { - if c.Proto[i] == "" { + protos := make([]string, 0, len(c.Proto)) + for _, path := range c.Proto { + if path == "" { continue } - if _, err := os.Stat(c.Proto[i]); err != nil { + if strings.ContainsAny(path, "*?[") { + files, err := filepath.Glob(path) + if err != nil { + return errors.E(op, err) + } + protos = append(protos, files...) + continue + } + + if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { - return errors.E(op, errors.Errorf("proto file '%s' does not exists", c.Proto[i])) + return errors.E(op, errors.Errorf("proto file '%s' does not exists", path)) } return errors.E(op, err) } + protos = append(protos, path) } + c.Proto = protos if c.EnableTLS() { if _, err := os.Stat(c.TLS.Key); err != nil { diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..5ec9e2e --- /dev/null +++ b/config_test.go @@ -0,0 +1,51 @@ +package grpc + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +const separator = string(filepath.Separator) + +func TestInitDefaults(t *testing.T) { + c := Config{} + assert.Error(t, c.InitDefaults()) + + c.Listen = "localhost:1234" + assert.NoError(t, c.InitDefaults()) + + c.Proto = []string{""} + assert.NoError(t, c.InitDefaults()) + assert.Empty(t, c.Proto) + + c.Proto = []string{"parser/pong.proto"} + assert.NoError(t, c.InitDefaults()) + assert.Equal(t, []string{"parser/pong.proto"}, c.Proto) + + c.Proto = []string{"parser/nonexitent*.notproto"} + assert.NoError(t, c.InitDefaults()) + assert.Empty(t, c.Proto) + + c.Proto = []string{"config.go", "parser/*.proto"} + assert.NoError(t, c.InitDefaults()) + assert.Equal(t, []string{ + "config.go", + "parser" + separator + "message.proto", + "parser" + separator + "pong.proto", + "parser" + separator + "test.proto", + "parser" + separator + "test_import.proto", + }, c.Proto) + + c.Proto = []string{"parser/?est.proto"} + assert.NoError(t, c.InitDefaults()) + assert.Equal(t, []string{"parser" + separator + "test.proto"}, c.Proto) + + c.Proto = []string{"parser/[tb]est.proto"} + assert.NoError(t, c.InitDefaults()) + assert.Equal(t, []string{"parser" + separator + "test.proto"}, c.Proto) + + c.Proto = []string{"[[[error"} + assert.Error(t, c.InitDefaults()) +} diff --git a/go.work.sum b/go.work.sum index 57f72c3..ee26478 100644 --- a/go.work.sum +++ b/go.work.sum @@ -230,6 +230,7 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 h1:hzAQntlaYRkVSFEfj9OTWlVV1H155FMD8BTKktLv0QI= github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 h1:zH8ljVhhq7yC0MIeUL/IviMtY8hx2mK8cN9wEYb8ggw= github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -281,6 +282,7 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= @@ -293,6 +295,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1: github.com/roadrunner-server/endure/v2 v2.0.1 h1:2greoQ669iCjsYNt14dwXXQGHAKDmLtFzL0OIE0ZATc= github.com/roadrunner-server/endure/v2 v2.0.1/go.mod h1:RDrC9SFlyCGqGA2v9SqFIA+EqWTFmPxafIb4SMeHCHM= github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=