Skip to content

Commit

Permalink
Merge pull request #12 from commitdev/dockerize-cli-#9
Browse files Browse the repository at this point in the history
Dockerize cli #9
  • Loading branch information
Pritesh-Patel committed Oct 6, 2019
2 parents d92dcae + eeb085b commit 7316876
Show file tree
Hide file tree
Showing 25 changed files with 135 additions and 507 deletions.
63 changes: 63 additions & 0 deletions Dockerfile
@@ -0,0 +1,63 @@
FROM golang:1.12.4-alpine3.9 as builder

ENV GO111MODULE=on
ENV GOLANG_PROTOBUF_VERSION=1.3.1
ENV GRPC_GATEWAY_VERSION=1.11.3
ENV GRPC_WEB_VERSION=1.0.6
ENV PROTOBUF_VERSION=3.9.2

RUN apk add --update --no-cache build-base curl git upx && \
rm -rf /var/cache/apk/*

RUN go get \
github.com/golang/protobuf/protoc-gen-go@v${GOLANG_PROTOBUF_VERSION} &&\
mv /go/bin/protoc-gen-go* /usr/local/bin/

RUN curl -sSL \
https://github.com/grpc-ecosystem/grpc-gateway/releases/download/v${GRPC_GATEWAY_VERSION}/protoc-gen-grpc-gateway-v${GRPC_GATEWAY_VERSION}-linux-x86_64 \
-o /usr/local/bin/protoc-gen-grpc-gateway && \
curl -sSL \
https://github.com/grpc-ecosystem/grpc-gateway/releases/download/v${GRPC_GATEWAY_VERSION}/protoc-gen-swagger-v${GRPC_GATEWAY_VERSION}-linux-x86_64 \
-o /usr/local/bin/protoc-gen-swagger && \
chmod +x /usr/local/bin/protoc-gen-grpc-gateway && \
chmod +x /usr/local/bin/protoc-gen-swagger

RUN curl -sSL \
https://github.com/grpc/grpc-web/releases/download/${GRPC_WEB_VERSION}/protoc-gen-grpc-web-${GRPC_WEB_VERSION}-linux-x86_64 \
-o /usr/local/bin/protoc-gen-grpc-web && \
chmod +x /usr/local/bin/protoc-gen-grpc-web

RUN mkdir -p /tmp/protoc && \
curl -sSL \
https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/protoc-${PROTOBUF_VERSION}-linux-x86_64.zip \
-o /tmp/protoc/protoc.zip && \
cd /tmp/protoc && \
unzip protoc.zip && \
mv /tmp/protoc/include /usr/local/include

RUN GO111MODULE=off go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

WORKDIR tmp/sprout
COPY go.mod .
COPY go.sum .
RUN go mod tidy && go mod download
COPY . .
RUN make build-deps && make build && \
mv sprout /usr/local/bin
RUN upx --lzma /usr/local/bin/*

FROM alpine:edge
ENV \
PROTOTOOL_PROTOC_BIN_PATH=/usr/bin/protoc \
PROTOTOOL_PROTOC_WKT_PATH=/usr/local/include \
PROTOBUF_VERSION=3.9.2 \
ALPINE_PROTOBUF_VERSION_SUFFIX=r0 \
GOPATH=/proto-libs
RUN mkdir ${GOPATH}
COPY --from=builder /usr/local/bin /usr/local/bin
COPY --from=builder /usr/local/include /usr/local/include
COPY --from=builder /go/src/github.com/grpc-ecosystem/grpc-gateway ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway
WORKDIR /project
RUN apk add --update --no-cache make bash curl git protobuf=${PROTOBUF_VERSION}-${ALPINE_PROTOBUF_VERSION_SUFFIX} && \
rm -rf /var/cache/apk/*
ENTRYPOINT ["sprout"]
29 changes: 18 additions & 11 deletions Makefile
Expand Up @@ -4,7 +4,6 @@ PROTOC_WEB_VERSION := 1.0.6
PROTO_SOURCES := -I /usr/local/include
PROTO_SOURCES += -I .
PROTO_SOURCES += -I ${GOPATH}/src
PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway

VERSION:= 0.0.1
Expand All @@ -14,41 +13,49 @@ deps-linux: deps-go deps-protoc-linux deps-grpc-web-linux
deps-protoc-linux:
curl -OL https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip
unzip protoc-$(PROTOC_VERSION)-linux-x86_64.zip -d protoc3
sudo mv protoc3/bin/* /usr/local/bin/
sudo mv protoc3/include/* /usr/local/include/
mv protoc3/bin/protoc /usr/local/bin
mv protoc3/include/* /usr/local/include
rm -rf protoc3 protoc-$(PROTOC_VERSION)-linux-x86_64.zip

deps-grpc-web-linux:
curl -OL https://github.com/grpc/grpc-web/releases/download/$(PROTOC_WEB_VERSION)/protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64
sudo mv protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web
mv protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web
chmod +x /usr/local/bin/protoc-gen-grpc-web

deps-go:
go get -u github.com/gobuffalo/packr/v2/packr2
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go

build-deps:
go install github.com/gobuffalo/packr/v2/packr2

fmt:
go fmt ./...

run:
go run main.go

build:
packr2 build -o sprout
CGO_ENABLED=0 packr2 build -o sprout
packr2 clean

build-example: build clean-example
mkdir -p example
cd example && ../sprout create "hello-world"
cd example/hello-world && ../../sprout generate -l go

build-example-docker: clean-example
mkdir -p example
docker run -v "$(shell pwd)/example:/project" --user $(shell id -u):$(shell id -g) sprout:v0 create "hello-world"
docker run -v "$(shell pwd)/example/hello-world:/project" --user $(shell id -u):$(shell id -g) sprout:v0 generate -l go

build-docker-local:
docker build . -t sprout:v0

clean-example:
rm -rf example

install-linux: build
mkdir -p ${HOME}/bin
cp sprout ${HOME}/bin/sprout
chmod +x ${HOME}/bin/sprout
@printf "\n Please run 'source ~/.profile' to add this installation to the path."
install-go:
CGO_ENABLED=0 packr2 build -o ${GOPATH}/bin/sprout
packr2 clean
29 changes: 16 additions & 13 deletions README.md
Expand Up @@ -18,24 +18,27 @@ It will also live with your project, when you add a new service to the config it

## What does it generate?

The generation will create 2 folders.
The generation will create project folder, within this there will be your implementation and an IDL folder

* A repo for the IDL's, this folder will also contain generated artifacts from the IDL under 'gen'
* A repo that implements the interfaces of the generated artifacts

`NOTE: It only creates the folders for these repos, you will still need to create the git repos on your respected platform. Aswell as initialise each folder as a git repo and push when there have been changes. (if there is a strong desire we can look at how to make this process easier.)`
* A parent directory that implements a skeleton and sets up your service implementation of the generated artifacts
* A child directory for the IDL's, this folder will also contain generated artifacts from the IDL under 'gen'

## The development cycle

1) Make folder with the name of your project and within that folder execute `sprout create [PROJECT_NAME]`
1) To create a project run `sprout create [PROJECT_NAME]`
2) A folder will be created and within that update the `sprout.yml` and then run `sprout generate -l=[LANGUAGE OF CHOICE] eg. go`
3) Move back to the root folder and you will see that there is now an idl folder created.
4) Modify the the protobuf services generated with your desired methods
5) Either run `make generate` or return to the application folder and re run `sprout generate`
6) Push up the IDL repo
6) Implement these methods on the main application repo
7) Push up the IDL repo & remove the replace function in the go.mod in the implementation project (this will swap it over to using the live IDL pushed up to git instead of your local one)
8) When you feel the need to add more services add them to the sprout config and re-run `sprout generate` and repeat steps 4 - 7.
3) You will see that there is now an idl folder created.
4) Within the idl folder modify the the protobuf services generated with your desired methods
5) Go up to the parrent directory and re run `sprout generate -l=[LANGUAGE OF CHOICE]`
6) Return back to the parent directory and implement the methods
7) Once you have tested your implementation and are happy with it return to the idl repo push that directory up to git
8) Return to the parent directory and check the depency file, for go it will be the go.mod file remove the lines that point it to your local directory, this will now point it to the version on git that was pushed up previously
10) Test and push up your implementation!
9) When you feel the need to add more services add them to the sprout config and re-run `sprout generate` and repeat steps 4 - 7.

## Usage & installation

As there alot of dependencies it will be easier to use this tool within the provided image, clone the repo and then run `make build-docker-local`. The best way then to use this is to alias `docker run -v "$(pwd):/project" --user $(id -u):$(id -g) sprout:v0` as sprout from then you can use the CLI as if it was installed as usual on your machine.

## Dependencies

Expand Down
10 changes: 8 additions & 2 deletions cmd/create.go
Expand Up @@ -34,13 +34,19 @@ var createCmd = &cobra.Command{
log.Fatalf("Error creating root: %v ", err)
}

sproutConfigPath := fmt.Sprintf("%v/sprout.yml", projectName)
sproutConfigPath := fmt.Sprintf("%v/sprout.yml", rootDir)

f, err := os.Create(sproutConfigPath)
if err != nil {
log.Printf("Error creating sprout config: %v", err)
}

Templator.Sprout.Execute(f, projectName)

gitIgnorePath := fmt.Sprintf("%v/.gitignore", rootDir)
f, err = os.Create(gitIgnorePath)
if err != nil {
log.Printf("Error creating sprout config: %v", err)
}
Templator.GitIgnore.Execute(f, projectName)
},
}
1 change: 0 additions & 1 deletion cmd/generate.go
Expand Up @@ -39,7 +39,6 @@ var generateCmd = &cobra.Command{
cfg.Print()

proto.Generate(Templator, cfg)

switch language {
case Go:
golang.Generate(Templator, cfg)
Expand Down
159 changes: 0 additions & 159 deletions example/hello-world-idl/gen/http/health/health.pb.gw.go

This file was deleted.

0 comments on commit 7316876

Please sign in to comment.