Skip to content

Commit

Permalink
Merge pull request #4 from k1LoW/buf-config
Browse files Browse the repository at this point in the history
Add BufConfig option
  • Loading branch information
k1LoW authored Apr 28, 2024
2 parents 7666cc9 + 4b26472 commit 0def81e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage.out
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ require (
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gofrs/uuid/v5 v5.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ github.com/bufbuild/buf v1.31.0 h1:YHLGIr8bjcLaTCIw0+/bCAvJLiR8u46QTwKvn7miSEg=
github.com/bufbuild/buf v1.31.0/go.mod h1:LlxpG2LF33f1Ixw29BTt0pyLriLzg3rXY1K9XQVHSio=
github.com/bufbuild/protocompile v0.13.0 h1:6cwUB0Y2tSvmNxsbunwzmIto3xOlJOV7ALALuVOs92M=
github.com/bufbuild/protocompile v0.13.0/go.mod h1:dr++fGGeMPWHv7jPeT06ZKukm45NJscd7rUxQVzEKRk=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
Expand Down
35 changes: 35 additions & 0 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

"github.com/bufbuild/buf/private/bufpkg/bufconfig"
"github.com/bufbuild/buf/private/bufpkg/buflock"
"github.com/bufbuild/protocompile"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -99,6 +100,40 @@ func BufLock(lockFile string) Option {
}
}

func BufConfig(configFile string) Option {
return func(r *Resolver) error {
b, err := os.ReadFile(configFile)
if err != nil {
return err
}
config := bufconfig.ExternalConfigV1{}
if err := yaml.Unmarshal(b, &config); err != nil {
return err
}
if config.Version != "v1" {
return fmt.Errorf("unsupported lock file version")
}
r.mu.Lock()
defer r.mu.Unlock()
for _, dep := range config.Deps {
splitted := strings.Split(dep, "/")
if len(splitted) != 3 {
return fmt.Errorf("dep should be in format <remote>/<owner>/<repository>: %s", dep)
}
commit := "main"
fdset, err := fetchFileDescriptorSet(splitted[1], splitted[2], commit)
if err != nil {
return err
}
for _, fd := range fdset.GetFile() {
// override if already exists
r.fds[fd.GetName()] = fd
}
}
return nil
}
}

func New(opts ...Option) (*Resolver, error) {
r := &Resolver{
fds: map[string]*descriptorpb.FileDescriptorProto{},
Expand Down
10 changes: 10 additions & 0 deletions resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ func TestBufLock(t *testing.T) {
}
}

func TestBufConfig(t *testing.T) {
b, err := New(BufConfig("testdata/buf.yaml"))
if err != nil {
t.Fatal(err)
}
if want := 6; len(b.fds) != want {
t.Errorf("got %d, want %d", len(b.fds), want)
}
}

func TestModule(t *testing.T) {
b, err := New(BufModule("buf.build/bufbuild/protovalidate/tree/b983156c5e994cc9892e0ce3e64e17e0"))
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions testdata/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v1
breaking:
use:
- FILE
lint:
use:
- DEFAULT
deps:
- buf.build/bufbuild/protovalidate

0 comments on commit 0def81e

Please sign in to comment.