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. convert Convert a message from binary to JSON or vice versa 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.
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest export PATH="$PATH:$(go env GOPATH)/bin"
进入bufbuilddemo/start/petapis,执行 buf mod init 会在当前目录中生成默认的buf.yaml文件,如果是一个新模块 就从这里开始让buf来管理你的proto文件。
cd bufbuilddemo/start/petapis buf mod init ----- 生成的buf.yaml文件-------------- version: v1 breaking: use: - FILE lint: use: - DEFAULT
lint 检查你的proto规范
回到示例仓库 。进入bufbuilddemo/start/petapis 。
buf lint ------输出-------------- google/type/datetime.proto:17:1:Package name "google.type" should be suffixed with a correctly formed version, such as "google.type.v1". pet/v1/pet.proto:44:10:Field name "petID" should be lower_snake_case, such as "pet_id". pet/v1/pet.proto:49:9:Service name "PetStore" should be suffixed with "Service".
上面输入的可读性很差 可以指定以json的方式输出就好看多了。
buf lint --error-format=json ------输出---------- {"path":"google/type/datetime.proto","start_line":17,"start_column":1,"end_line":17,"end_column":21,"type":"PACKAGE_VERSION_SUFFIX","message":"Package name \"google.type\" should be suffixed with a correctly formed version, such as \"google.type.v1\"."} {"path":"pet/v1/pet.proto","start_line":44,"start_column":10,"end_line":44,"end_column":15,"type":"FIELD_LOWER_SNAKE_CASE","message":"Field name \"petID\" should be lower_snake_case, such as \"pet_id\"."} {"path":"pet/v1/pet.proto","start_line":49,"start_column":9,"end_line":49,"end_column":17,"type":"SERVICE_SUFFIX","message":"Service name \"PetStore\" should be suffixed with \"Service\"."}
buf breaking --against "https://github.com/DemoHubs/bufbuilddemo.git#branch=main,subdir=start/petapis" --error-format=json -----------输出------------ {"path":"pet/v1/pet.proto","type":"SERVICE_NO_DELETE","message":"Previously present service \"PetStore\" was deleted from file."} {"path":"pet/v1/pet.proto","start_line":20,"start_column":3,"end_line":20,"end_column":9,"type":"FIELD_SAME_TYPE","message":"Field \"1\" on message \"Pet\" changed type from \"enum\" to \"string\"."} {"path":"pet/v1/pet.proto","start_line":44,"start_column":3,"end_line":44,"end_column":21,"type":"FIELD_SAME_JSON_NAME","message":"Field \"1\" with name \"pet_id\" on message \"DeletePetRequest\" changed option \"json_name\" from \"petID\" to \"petId\"."} {"path":"pet/v1/pet.proto","start_line":44,"start_column":10,"end_line":44,"end_column":16,"type":"FIELD_SAME_NAME","message":"Field \"1\" on message \"DeletePetRequest\" changed name from \"petID\" to \"pet_id\"."}
这里主要关注第2个错误\”Pet\” changed type from \”enum\” to \”string\”。就明确提示了 由enum变更为string。其它的错误是因为我们修复了默认的lint的几个规则。
buf beta registry repository create buf.build/peachyy/bufbuilddemo --visibility public ------------- 输出----------- WARN This command is in beta. It is unstable and likely to change. To suppress this warning, set BUF_BETA_SUPPRESS_WARNINGS=1 Full name Created buf.build/peachyy/bufbuilddemo 2022-12-19T10:12:01Z
再次执行buf build。同样是错误输出 但是多了一个警告 意思是你deps依赖库不在buf.lock中。运行 buf mod update 生成buf.lock文件 这个文件保存了deps依赖信息。
buf build -------------输出---------- WARN Specified deps are not covered in your buf.lock, run "buf mod update": - buf.build/googleapis/googleapis pet/v1/pet.proto:5:8:google/type/datetime.proto: does not exist
执行buf mod update 执行后生成的buf.lock文件记录了依赖的 远程服务地址、用户名、仓库名称、提交版本号。