Skip to content

Commit

Permalink
feat: setup protobuf ecosystem
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 24, 2019
1 parent 53f52ef commit 36c42a3
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 10 deletions.
17 changes: 17 additions & 0 deletions .circleci/config.yml
Expand Up @@ -33,9 +33,26 @@ jobs:
docker_layer_caching: true
- docker/build:
image: berty/berty
go-generate:
working_directory: /go/src/berty.tech/go
docker:
- image: bertytech/protoc:16
steps:
- checkout:
path: /go/src/berty.tech
- run: |
set -e
rm -f gen.sum
go mod vendor
make generate_local
make tidy
git status | cat
git diff -w | cat
git diff-index -w --quiet HEAD --
workflows:
main:
jobs:
- go-build
- go-docker
- go-generate
7 changes: 3 additions & 4 deletions .gitignore
Expand Up @@ -10,20 +10,19 @@
/zero-push/

# junk
go/internal/bar
go/internal/baz
go/internal/foo
profile.out
*#
*~
.#*
./package.json
.DS_Store
.agignore
.env
coverage.txt
.projectile
/client/react-native/desktop/output/
/go.mod
core-sources.jar
gin-bin
google-services.json
vendor/
.circleci/config-processed.yml
9 changes: 9 additions & 0 deletions api/baz.proto
@@ -0,0 +1,9 @@
syntax = "proto3";

package berty.baz;

option go_package = "berty.tech/go/internal/baz";

message Baz {
bool is_baz = 1;
}
12 changes: 12 additions & 0 deletions api/internal/bar.proto
@@ -0,0 +1,12 @@
syntax = "proto3";

package berty.internal.foo;

import "internal/foo.proto";

option go_package = "berty.tech/go/internal/bar";

message Bar {
foo.Foo foo = 1;
bool is_bar = 2;
}
22 changes: 22 additions & 0 deletions api/internal/foo.proto
@@ -0,0 +1,22 @@
syntax = "proto3";

package berty.internal.foo;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";

option go_package = "berty.tech/go/internal/foo";
option (gogoproto.sizer_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.unmarshaler_all) = true;

message Metadata {
string id = 1 [(gogoproto.moretags) = "gorm:\"primary_key\"", (gogoproto.customname) = "ID"];
google.protobuf.Timestamp created_at = 2 [(gogoproto.stdtime) = true, (gogoproto.nullable) = true];
google.protobuf.Timestamp updated_at = 3 [(gogoproto.stdtime) = true, (gogoproto.nullable) = true];
}

message Foo {
Metadata metadata = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
bool is_foo = 2;
}
File renamed without changes.
37 changes: 32 additions & 5 deletions go/Makefile
@@ -1,22 +1,49 @@
PROTOS_SRC := $(wildcard ../api/*.proto) $(wildcard ../api/internal/*.proto)
PROTOS_GEN := $(patsubst %.proto,%.pb.go,$(PROTOS_SRC))

.PHONY: install
install:
install: generate
go install ./cmd/...

.PHONY: test
test: unittest lint tidy

.PHONY: unittest
unittest:
unittest: generate
go test -v -cover -coverprofile=coverage.txt -covermode=atomic -race ./...

.PHONY: lint
lint:
lint: generate
golangci-lint run --verbose ./...

.PHONY: tidy
tidy:
tidy: generate
go mod tidy

.PHONY: docker.build
docker.build:
docker.build: generate
docker build -t bertytech/berty .

.PHONY: generate
generate: gen.sum
gen.sum: $(PROTOS_SRC) Makefile
md5sum -c $@ || ( \
set -e; \
go mod vendor; \
docker run \
--user=`id -u` \
--volume="$(PWD)/..:/go/src/berty.tech" \
--workdir="/go/src/berty.tech/go" \
--entrypoint="sh" \
--rm \
bertytech/protoc:16 \
-xec 'make generate_local' \
)
touch $@

.PHONY: generate_local
generate_local:
@set -e; for proto in $(PROTOS_SRC); do ( set -xe; \
protoc -I /protobuf:vendor:../api: --gofast_out="plugins=grpc:$(GOPATH)/src" "$$proto" \
); done
md5sum $(PROTOS_SRC) | sort -k2 > gen.sum
3 changes: 3 additions & 0 deletions go/gen.sum
@@ -0,0 +1,3 @@
ce71d433e26b7f98222d8fa7705c5aee ../api/baz.proto
af88d1e57ab838dd3e1883667e8eb536 ../api/internal/bar.proto
943ccf5086a2cc88f02b603d04f5506d ../api/internal/foo.proto
6 changes: 5 additions & 1 deletion go/go.mod
Expand Up @@ -2,4 +2,8 @@ module berty.tech/go

go 1.13

require github.com/peterbourgon/ff v1.6.0
require (
github.com/gogo/protobuf v1.3.0
github.com/golang/protobuf v1.3.2
github.com/peterbourgon/ff v1.6.0
)
7 changes: 7 additions & 0 deletions go/go.sum
@@ -1,7 +1,14 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/gogo/protobuf v1.3.0 h1:G8O7TerXerS4F6sx9OV7/nRfJdnXgHZu/S/7F2SN+UE=
github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/peterbourgon/ff v1.6.0 h1:DNnSOwtqmHfQ/yLgdOvtN4eFzP4ps+IjNhUEW9/ZkIg=
github.com/peterbourgon/ff v1.6.0/go.mod h1:8rO4i98n/oYmyP28qiK6V4jGB85nMNVr+qwSErTwFrs=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
10 changes: 10 additions & 0 deletions go/internal/gomodhack/gomodhack.go
@@ -0,0 +1,10 @@
// Package gomodhack ensures that `go mod` detect some required dependencies.
// This package should not be imported directly.
package gomodhack

import (
_ "github.com/gogo/protobuf/gogoproto" // required by protoc
_ "github.com/gogo/protobuf/types" // required by protoc
_ "github.com/golang/protobuf/proto" // required by protoc
_ "github.com/golang/protobuf/ptypes/timestamp" // required by protoc
)
41 changes: 41 additions & 0 deletions tool/docker-protoc/Dockerfile
@@ -0,0 +1,41 @@
FROM moul/protoc-gen-gotemplate:latest as pgg

# build image
FROM golang:1.13-alpine as builder
# install deps
RUN apk --no-cache add make git go rsync libc-dev openssh docker npm
# install compilers
RUN go get -u \
github.com/gogo/protobuf/gogoproto \
github.com/gogo/protobuf/jsonpb \
github.com/gogo/protobuf/proto \
github.com/gogo/protobuf/protoc-gen-gofast \
github.com/gogo/protobuf/protoc-gen-gogo \
github.com/spf13/cobra \
github.com/vektah/gorunpkg \
golang.org/x/tools/cmd/goimports \
moul.io/protoc-gen-gotemplate
RUN go get -d github.com/envoyproxy/protoc-gen-validate \
&& make -C $GOPATH/src/github.com/envoyproxy/protoc-gen-validate build

# runtime
FROM golang:1.13-alpine
RUN apk --no-cache add git openssh make protobuf gcc libc-dev nodejs-npm sudo \
&& mkdir -p /.cache/go-build \
&& chmod -R 777 /.cache \
&& mkdir -p /node/ \
&& cd /node \
&& npm install -g \
npm@6.9.0 \
protobufjs \
prettier \
prettier-eslint \
prettier-eslint-cli \
pbhbs@0.1.0 \
&& echo 'syntax = "proto3";' | /usr/bin/pbjs - > /dev/null
COPY --from=pgg /go/bin/* /go/bin/
COPY --from=builder /go/bin/* /go/bin/
COPY --from=pgg /protobuf /protobuf
ENV GOPATH=/go \
PATH=/go/bin:/node/node_modules/.bin:${PATH} \
GOROOT=/usr/local/go
10 changes: 10 additions & 0 deletions tool/docker-protoc/Makefile
@@ -0,0 +1,10 @@
IMAGE ?= bertytech/protoc
VERSION ?= 16

build:
docker build -t $(IMAGE):$(VERSION) .

publish: build
docker tag $(IMAGE):$(VERSION) $(IMAGE):latest
docker push $(IMAGE):$(VERSION)
docker push $(IMAGE):latest

0 comments on commit 36c42a3

Please sign in to comment.