https://blog.p1ass.com/posts/introduction-buf/
こんにちは、@p1assです。
この記事では、Protocol Buffers に関連した様々なコマンドを実行できる Buf CLI を紹介します。 Buf CLI は、protoc とは異なり、Formatter や Linter、Breaking Change Detector、依存パッケージ管理など、Protocol Buffers を使う上で便利なコマンドが用意されています。 Protocol Buffers を使いたいけど CI のセットアップ等が面倒な人におすすめです。
Buf のロゴ(公式サイトより引用)
ヘルプコマンドを実行して利用可能なコマンドを確認します。 この中からいくつかピックアップして紹介します。
$ buf -h
The Buf CLI
A tool for working with Protocol Buffers and managing resources on the Buf Schema Registry (BSR).
Usage:
buf [flags]
buf [command]
Available Commands:
beta Beta commands. Unstable and likely to change.
breaking Verify that the input location has no breaking changes compared to the against location.
build Build all Protobuf files from the specified input and output a Buf image.
completion Generate auto-completion scripts for commonly used shells.
export Export the files from the input location to an output location.
format Format all Protobuf files from the specified input and output the result.
generate Generate stubs for protoc plugins using a template.
help Help about any command
lint Verify that the input location passes lint checks.
ls-files List all Protobuf files for the input.
mod Manage Buf modules.
push Push a module to a registry.
registry Manage assets on the Buf Schema Registry.
Flags:
--debug Turn on debug logging.
-h, --help help for buf
--log-format string The log format [text,color,json]. (default "color")
--timeout duration The duration until timing out. (default 2m0s)
-v, --verbose Turn on verbose mode.
--version Print the version.
Use "buf [command] --help" for more information about a command.
buf mod
コマンドは、go mod
コマンドと同様に依存パッケージを管理するコマンドです。
buf mod init
コマンドを実行することで、 buf.yaml
が生成されます。
# buf.yamlversion: v1breaking: use: - FILElint: use: - DEFAULT
buf.yaml
はModuleにおける設定を管理するファイルです。 デフォルトでは、Linter や Breaking Change Detector に関する設定が書かれています。
deps
を記述することで、Buf Schema Registryから依存パッケージをダウンロードしてくれます。
# buf.yamlversion: v1# 追加deps: - buf.build/googleapis/googleapisbreaking: use: - FILElint: use: - DEFAULT
この例では、googleapis/googleapisを依存に追加したので、google/type/datetime.proto
といった Google Common Types が使えるようになります。