Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

little refactoring #6

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
FROM golang:1.21.0-alpine3.17
ARG BUILD_ARG_GO_VERSION=1.21.0
ARG BUILD_ARG_ALPINE_VERSION=3.18
FROM golang:${BUILD_ARG_GO_VERSION}-alpine${BUILD_ARG_ALPINE_VERSION} AS builder

# install tools (bash, git, protobuf, protoc-gen-go, protoc-grn-go-grpc, pkger)
# install tools (bash, git, protobuf, protoc-gen-go, protoc-grn-go-grpc)
RUN apk -U --no-cache add bash git protobuf &&\
go install -v github.com/golang/protobuf/protoc-gen-go@latest &&\
go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest &&\
go install github.com/markbates/pkger/cmd/pkger@latest
go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# cloning well-known-types
# only use needed files
RUN git clone --depth=1 https://github.com/google/protobuf.git /protobuf-repo &&\
RUN git clone --depth=1 https://github.com/protocolbuffers/protobuf.git /protobuf-repo &&\
mv /protobuf-repo/src/ /protobuf/ &&\
rm -rf /protobuf-repo

Expand All @@ -21,12 +22,19 @@ RUN mkdir /proto /stubs &&\
WORKDIR /go/src/github.com/tokopedia/gripmock/protoc-gen-gripmock

# install generator plugin
RUN pkger && go install -v
RUN go install -v

WORKDIR /go/src/github.com/tokopedia/gripmock/example/simple/client

RUN go get -u all

WORKDIR /go/src/github.com/tokopedia/gripmock

# install gripmock & build example to cache necessary imports
RUN go install -v && go build ./example/simple/client
RUN go install -v

# remove pkgs
RUN apk del git

EXPOSE 4770 4771

Expand Down
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
GOLANGCI_LING_IMAGE="golangci/golangci-lint:v1.54.0-alpine"

.PHONY: *

version=latest

build:
docker buildx build --load -t "bavix/gripmock:${version}" --platform linux/amd64,linux/arm64 .

test:
go test -tags mock -race -cover ./...

lint:
docker run --rm -v ./:/app -w /app $(GOLANGCI_LING_IMAGE) golangci-lint run --color always ${args}

lint-fix:
make lint args=--fix
18 changes: 8 additions & 10 deletions protoc-gen-gripmock/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"embed"
"fmt"
"io"
"log"
Expand All @@ -11,7 +12,6 @@ import (

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/markbates/pkger"
"golang.org/x/tools/imports"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
Expand Down Expand Up @@ -113,20 +113,18 @@ type Options struct {
format bool
}

var SERVER_TEMPLATE string
var ServerTemplate string

func init() {
f, err := pkger.Open("/server.tmpl")
if err != nil {
log.Fatalf("error opening server.tmpl: %s", err)
}
//go:embed server.tmpl
var serverTmpl embed.FS

bytes, err := io.ReadAll(f)
func init() {
data, err := serverTmpl.ReadFile("server.tmpl")
if err != nil {
log.Fatalf("error reading server.tmpl: %s", err)
}

SERVER_TEMPLATE = string(bytes)
ServerTemplate = string(data)
}

func generateServer(protos []*descriptor.FileDescriptorProto, opt *Options) error {
Expand All @@ -150,7 +148,7 @@ func generateServer(protos []*descriptor.FileDescriptorProto, opt *Options) erro
}

tmpl := template.New("server.tmpl")
tmpl, err := tmpl.Parse(SERVER_TEMPLATE)
tmpl, err := tmpl.Parse(ServerTemplate)
if err != nil {
return fmt.Errorf("template parse %v", err)
}
Expand Down
2 changes: 0 additions & 2 deletions protoc-gen-gripmock/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ go 1.21

require (
github.com/golang/protobuf v1.5.2
github.com/markbates/pkger v0.17.1
golang.org/x/tools v0.1.5
google.golang.org/protobuf v1.27.1
)

require (
github.com/gobuffalo/here v0.6.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
Expand Down
20 changes: 0 additions & 20 deletions protoc-gen-gripmock/go.sum
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno=
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
Expand All @@ -42,8 +27,3 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading