0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

bufで他のレポにあるprotoからコード生成

Last updated at Posted at 2025-02-06

はじめに

Protoファイルを別のレポジトリで管理している時にアプリケーションレポジトリでProtoファイルから生成したコードを使用したい場合があると思います。
その場合に、bufを使ってどのように生成するのかを調べました。

bufのインストール

Macであればbrewでインストールします。他のインストールはリンクを。

$ brew install bufbuild/buf/buf

設定

buf.gen.yaml
version: v2
managed:
  enabled: true
  override:
    - file_option: go_package_prefix
      value: <module>/gen/sample # this makes the package name consistent with your app code
plugins:
  - remote: buf.build/protocolbuffers/go:v1.36.5
    out: gen/sample/ # directory to locate the generated go code
    opt:
      - paths=source_relative
    include_imports: true

inputs:
  - git_repo: ssh://git@github.com/<proto/repo> # git repo for proto
    depth: 1
    tag: v1.0.0 # tag

これでbuf generateを実行すると以下のPathにコードが生成されます。

gen/sample/<original path in the proto repo>/

アプリケーションコードからも <module>/gen/sample/xxx で使いたいpackageを指定して使えます。

特定のPath (a/b/c (proto repoのpath))だけに対してコード生成したい場合は以下のように指定することが出来ます。

buf generate --path a/b/c
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?