<!doctype html><metacharset=utf-8><metaname=viewportcontent="width=device-width,initial-scale=1"><linkrel=preconnecthref=https://fonts.googleapis.com><linkrel=preconnecthref=https://fonts.gstatic.comcrossorigin><linkhref="https://fonts.googleapis.com/css2?family=Recursive:wght,CASL,MONO@300..800,0..1,0..1&display=swap"rel=stylesheet><linkrel=stylesheethref=styles.css><ahref=index>Index</a><h1>Migrate to <code>buf</code> from <code>prototool</code></h1><p>Why? Because <code>prototool</code> is outdated, and can not run on M1 mac.<p>There are 2 usecase:<ul><li>For services only provide GRPC<li>For services provide REST and GRPC, which make use of <ahref=https://github.com/grpc-ecosystem/grpc-gateway>grpc-ecosystem/grpc-gateway</a>.</ul><p>We need 3 files:<ul><li><code>build.go</code>: need to install protoc-gen-* binaries with pin version in <code>go.mod</code><li><code>buf.yaml</code><li><code>buf.gen.yaml</code></ul><p>FYI, the libs version I use:<ul><li><ahref=https://github.com/golang/protobuf/releases/tag/v1.5.2>golang/protobuf v1.5.2</a><li><ahref=https://github.com/grpc-ecosystem/grpc-gateway/releases/tag/v1.16.0>grpc-ecosystem/grpc-gateway v1.16.0</a></ul><h2>GRPC only</h2><p><code>build.go</code>:<pre><codeclass=language-go>//go:build tools
go install github.com/golang/protobuf/protoc-gen-go
go install github.com/bufbuild/buf/cmd/buf@latest
buf format -w
buf generate
</code></pre><p>Then run <code>make gen</code> to check result.<h2>REST and GRPC using grpc-gateway</h2><p><code>build.go</code>:<pre><codeclass=language-go>//go:build tools
</code></pre><p>Run <code>make gen</code> to get fun of course.<h2>FAQ</h2><p>If use <code>vendor</code>:<ul><li>Replace <code>buf generate</code> with <code>buf generate --exclude-path vendor</code>.<li>Replace <code>buf format -w</code> with <code>buf format -w --exclude-path vendor</code>.</ul><p>Replace <code>import "third_party/googleapis/google/api/annotations.proto";</code> with <code>import "google/api/annotations.proto";</code><p>Delete <code>security_definitions</code>, <code>security</code>, in <code>option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger)</code>.<p>The last step is delete <code>prototool.yaml</code>.<h2>Thanks</h2><ul><li><ahref=https://github.com/uber/prototool>uber/prototool</a><li><ahref=https://github.com/bufbuild/buf>bufbuild/buf</a></ul><ahref=mailto:hauvipapro+posts@gmail.com>Feel free to ask me via email</a>