Skip to content

Commit

Permalink
server generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritesh-Patel committed Aug 22, 2019
1 parent 4ba2db0 commit d0665c8
Show file tree
Hide file tree
Showing 18 changed files with 263 additions and 108 deletions.
11 changes: 7 additions & 4 deletions Makefile
Expand Up @@ -10,8 +10,11 @@ run:
go run main.go

build:
packr2
go build -o sprout

clean:
packr2 build -o sprout
packr2 clean

build-example:
./sprout generate -c example/sprout.yml -l go -o example

clean-example:
rm -rf example/example
34 changes: 33 additions & 1 deletion cmd/generate.go
Expand Up @@ -3,16 +3,27 @@ package cmd
import (
"github.com/commitdev/sprout/config"
"github.com/commitdev/sprout/generate/golang"
"github.com/commitdev/sprout/generate/proto"
"log"

"github.com/spf13/cobra"
)

var configPath string
var outputPath string
var language string

const (
Go = "go"
)

var supportedLanguages = [...]string{Go}

func init() {

generateCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "sprout.yml", "config path")
generateCmd.PersistentFlags().StringVarP(&outputPath, "output-path", "o", "./", "config path")
generateCmd.PersistentFlags().StringVarP(&language, "language", "l", "", "language to generate project in")

rootCmd.AddCommand(generateCmd)
}
Expand All @@ -21,8 +32,29 @@ var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate project from config.",
Run: func(cmd *cobra.Command, args []string) {
if !ValidLanguage() {
log.Fatalf("'%s' is not a supported language.", language)
}

cfg := config.LoadConfig(configPath)
cfg.Print()
golang.Generate(Templator, cfg, outputPath)

proto.Generate(Templator, cfg, outputPath)

switch language {
case Go:
golang.Generate(Templator, cfg, outputPath)

}
},
}

func ValidLanguage() bool {
for _, l := range supportedLanguages {
if l == language {
return true
}
}

return false
}
34 changes: 0 additions & 34 deletions example/example/gen/go/helloworld2/helloworld2.pb.go

This file was deleted.

11 changes: 0 additions & 11 deletions example/example/idl/proto/helloworld2/helloworld2.proto

This file was deleted.

5 changes: 1 addition & 4 deletions example/example/idl/proto/prototool.yaml
Expand Up @@ -9,12 +9,9 @@ create:
- directory: helloworld
name: mycompany.helloworld

- directory: helloworld2
name: mycompany.helloworld2

generate:
go_options:
import_path: github.com/mycompany/test
import_path: github.com/commitdev/example/example
extra_modifiers:
google/api/annotations.proto: google.golang.org/genproto/googleapis/api/annotations
google/api/http.proto: google.golang.org/genproto/googleapis/api/annotations
Expand Down
21 changes: 21 additions & 0 deletions example/example/server/health/health.go
@@ -0,0 +1,21 @@
package health

import (
"context"
api "github.com/commitdev/example/example/gen/go/health"
)

type HealthServer struct {

}

func (s *HealthServer) Check(ctx context.Context, req *api.HealthCheckRequest) (*api.HealthCheckResponse, error) {
resp := &api.HealthCheckResponse{
Status: api.HealthCheckResponse_SERVING,
}
return resp,nil
}

func (s *HealthServer) Watch(req *api.HealthCheckRequest, server api.Health_WatchServer) error {
return nil
}
10 changes: 10 additions & 0 deletions example/example/server/helloworld/helloworld.go
@@ -0,0 +1,10 @@
package helloworld

import (
"context"
api "github.com/commitdev/example/example/gen/go/helloworld"
)

type HelloworldServer struct {

}
4 changes: 1 addition & 3 deletions example/sprout.yml
@@ -1,7 +1,7 @@
organization: mycompany
name: example
description: This is a test project
git-repo: github.com/mycompany/test
git-repo: github.com/commitdev/example/example
docker-repo: test.com
maintainers:
- name: bob
Expand All @@ -15,5 +15,3 @@ network:
services:
- name: helloworld
description: Hello world!.
- name: helloworld2
description: Hello world again!.
94 changes: 46 additions & 48 deletions generate/golang/generate.go
Expand Up @@ -8,73 +8,71 @@ import (
"github.com/commitdev/sprout/templator"
"log"
"os"
"os/exec"
)

func Generate(templator *templator.Templator, config *config.SproutConfig, outPath string) {
GenerateProtoToolConfig(templator, config, outPath)
GenerateProtoHealth(templator, config, outPath)
GenerateProtoServices(templator, config, outPath)
GenerateProtoServiceLibs(config, outPath)
}
GenerateHealthServer(templator, config, outPath)
GenerateServers(templator, config, outPath)

func GenerateProtoToolConfig(templator *templator.Templator, config *config.SproutConfig, outPath string) {
protoPath := fmt.Sprintf("%s/%s/idl/proto", outPath, config.Name)
protoToolOutput := fmt.Sprintf("%s/prototool.yaml", protoPath)
}

err := util.CreateDirIfDoesNotExist(protoPath)
func GenerateServers(templator *templator.Templator, config *config.SproutConfig, outPath string) {
serverDirPath := fmt.Sprintf("%s/%s/server", outPath, config.Name)
err := util.CreateDirIfDoesNotExist(serverDirPath)
if err != nil {
log.Printf("Error generating prototool config: %v", err)
log.Printf("Error creating server path: %v", err)
}

f, err := os.Create(protoToolOutput)
if err != nil {
log.Printf("Error: %v", err)
}
templator.ProtoToolTemplate.Execute(f, config)
}
for _, s := range config.Services {
serverLibPath := fmt.Sprintf("%s/%s", serverDirPath, s.Name)
err := os.Mkdir(serverLibPath, os.ModePerm)
if os.IsExist(err) {
log.Printf("%s service exists skipping.", s.Name)
continue
}
log.Printf("generating %s", s.Name)
if err != nil {
log.Printf("Error generating server: %v", err)
}

func GenerateProtoHealth(templator *templator.Templator, config *config.SproutConfig, outPath string) {
protoHealthPath := fmt.Sprintf("%s/%s/idl/proto/health", outPath, config.Name)
protoHealthOutput := fmt.Sprintf("%s/health.proto", protoHealthPath)
serverFilePath := fmt.Sprintf("%s/%s.go", serverLibPath, s.Name)
f, err := os.Create(serverFilePath)

err := util.CreateDirIfDoesNotExist(protoHealthPath)
if err != nil {
log.Printf("Error: %v", err)
}
if err != nil {
log.Printf("Error: %v", err)
}

f, err := os.Create(protoHealthOutput)
if err != nil {
log.Printf("Error: %v", err)
data := map[string]string {
"ServiceName": s.Name,
"GitRepo": config.GitRepo,
}

templator.Go.GoServer.Execute(f, data)
}

templator.ProtoHealthTemplate.Execute(f, config)
}

func GenerateProtoServices(templator *templator.Templator, config *config.SproutConfig, outPath string) {
protoToolConfigPath := fmt.Sprintf("%s/%s/idl/proto", outPath, config.Name)
for _, s := range config.Services {
idlPath := fmt.Sprintf("%s/%s", protoToolConfigPath, s.Name)
err := util.CreateDirIfDoesNotExist(idlPath)
if err != nil {
log.Printf("Error generating service proto: %v", err)
}
func GenerateHealthServer(templator *templator.Templator, config *config.SproutConfig, outPath string) {
serverDirPath := fmt.Sprintf("%s/%s/server", outPath, config.Name)
err := util.CreateDirIfDoesNotExist(serverDirPath)
if err != nil {
log.Printf("Error creating server path: %v", err)
}

//local paths
protoPath := fmt.Sprintf("%s/%s.proto", s.Name, s.Name)
cmd := exec.Command("prototool", "create", protoPath)
cmd.Dir = protoToolConfigPath
cmd.Run()
serverLibPath := fmt.Sprintf("%s/%s", serverDirPath, "health")
err = util.CreateDirIfDoesNotExist(serverLibPath)
if err != nil {
log.Printf("Error generating server: %v", err)
}

}
serverFilePath := fmt.Sprintf("%s/%s.go", serverLibPath, "health")
f, err := os.Create(serverFilePath)

func GenerateProtoServiceLibs(config *config.SproutConfig, outPath string) {
protoToolConfigPath := fmt.Sprintf("%s/%s/idl/proto", outPath, config.Name)
cmd := exec.Command("prototool", "generate")
cmd.Dir = protoToolConfigPath
err := cmd.Run()
if err != nil {
log.Printf("Error executing prototool generate: %v", err)
log.Printf("Error: %v", err)
}

importPath := fmt.Sprintf("%s/gen/go/health", config.GitRepo)

templator.Go.GoHealthServer.Execute(f, importPath)
}
80 changes: 80 additions & 0 deletions generate/proto/generate.go
@@ -0,0 +1,80 @@
package proto

import (
"fmt"
"github.com/commitdev/sprout/util"

"github.com/commitdev/sprout/config"
"github.com/commitdev/sprout/templator"
"log"
"os"
"os/exec"
)

func Generate(templator *templator.Templator, config *config.SproutConfig, outPath string) {
GenerateProtoToolConfig(templator, config, outPath)
GenerateProtoHealth(templator, config, outPath)
GenerateProtoServices(templator, config, outPath)
GenerateProtoServiceLibs(config, outPath)
}

func GenerateProtoToolConfig(templator *templator.Templator, config *config.SproutConfig, outPath string) {
protoPath := fmt.Sprintf("%s/%s/idl/proto", outPath, config.Name)
protoToolOutput := fmt.Sprintf("%s/prototool.yaml", protoPath)

err := util.CreateDirIfDoesNotExist(protoPath)
if err != nil {
log.Printf("Error generating prototool config: %v", err)
}

f, err := os.Create(protoToolOutput)
if err != nil {
log.Printf("Error: %v", err)
}
templator.ProtoToolTemplate.Execute(f, config)
}

func GenerateProtoHealth(templator *templator.Templator, config *config.SproutConfig, outPath string) {
protoHealthPath := fmt.Sprintf("%s/%s/idl/proto/health", outPath, config.Name)
protoHealthOutput := fmt.Sprintf("%s/health.proto", protoHealthPath)

err := util.CreateDirIfDoesNotExist(protoHealthPath)
if err != nil {
log.Printf("Error: %v", err)
}

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

templator.ProtoHealthTemplate.Execute(f, config)
}

func GenerateProtoServices(templator *templator.Templator, config *config.SproutConfig, outPath string) {
protoToolConfigPath := fmt.Sprintf("%s/%s/idl/proto", outPath, config.Name)
for _, s := range config.Services {
idlPath := fmt.Sprintf("%s/%s", protoToolConfigPath, s.Name)
err := util.CreateDirIfDoesNotExist(idlPath)
if err != nil {
log.Printf("Error generating service proto: %v", err)
}

//local paths
protoPath := fmt.Sprintf("%s/%s.proto", s.Name, s.Name)
cmd := exec.Command("prototool", "create", protoPath)
cmd.Dir = protoToolConfigPath
cmd.Run()
}

}

func GenerateProtoServiceLibs(config *config.SproutConfig, outPath string) {
protoToolConfigPath := fmt.Sprintf("%s/%s/idl/proto", outPath, config.Name)
cmd := exec.Command("prototool", "generate")
cmd.Dir = protoToolConfigPath
err := cmd.Run()
if err != nil {
log.Printf("Error executing prototool generate: %v", err)
}
}
1 change: 1 addition & 0 deletions go.sum
Expand Up @@ -212,6 +212,7 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/uber/prototool v1.8.2-0.20190812135552-57293fc1b93a h1:e23fz+yc4zTRfbYAeGUqOt8oZvyZaCzbVg+D4hE6ytk=
Expand Down

0 comments on commit d0665c8

Please sign in to comment.