Skip to content

Commit

Permalink
file structure changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritesh-Patel committed Aug 26, 2019
1 parent ebdd304 commit 41a5fae
Show file tree
Hide file tree
Showing 22 changed files with 129 additions and 60 deletions.
11 changes: 9 additions & 2 deletions Makefile
Expand Up @@ -14,7 +14,14 @@ build:
packr2 clean

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

clean-example:
rm -rf example/example
rm -rf example

install-linux: build
mkdir -p ${HOME}/bin
cp sprout ${HOME}/bin/sprout
chmod +x ${HOME}/bin/sprout
20 changes: 10 additions & 10 deletions README.md
Expand Up @@ -18,23 +18,23 @@ 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 a folder with 3 repos within it.
The generation will create 2 folders.

* A rep for the IDL's
* A repo that has the generated artifacts from the IDL
* A rep 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.)`

## The development cycle

1) Setup sprout config & run generation
2) Start adding your desired methods to the protobuf files generated
3) Rerun generation
4) Push the idl and the language generated repo
5) Implement these methods on the main application repo
6) When you feel the need to add more services add them to the sprout config
7) Repeat steps 1 - 5
1) Make folder and within that folder execute `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]`
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 `prototool 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) When you feel the need to add more services add them to the sprout config and repeate the generation process

## Dependencies

Expand Down
40 changes: 40 additions & 0 deletions cmd/create.go
@@ -0,0 +1,40 @@
package cmd

import (
"log"
"os"
"fmt"


"github.com/spf13/cobra"
)

var projectName string

func init() {

createCmd.PersistentFlags().StringVarP(&projectName, "project-name", "p", "", "project name")

rootCmd.AddCommand(createCmd)
}

var createCmd = &cobra.Command{
Use: "create",
Short: "Create new project.",
Run: func(cmd *cobra.Command, args []string) {

err := os.Mkdir(projectName, os.ModePerm)
if os.IsExist(err){
log.Fatalf("Directory %v already exists!", projectName)
}

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

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

Templator.Sprout.Execute(f, projectName)
},
}
8 changes: 3 additions & 5 deletions cmd/generate.go
Expand Up @@ -10,7 +10,6 @@ import (
)

var configPath string
var outputPath string
var language string

const (
Expand All @@ -22,15 +21,14 @@ 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)
}

var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate project from config.",
Short: "Generate idl & application folders",
Run: func(cmd *cobra.Command, args []string) {
if !ValidLanguage() {
log.Fatalf("'%s' is not a supported language.", language)
Expand All @@ -39,11 +37,11 @@ var generateCmd = &cobra.Command{
cfg := config.LoadConfig(configPath)
cfg.Print()

proto.Generate(Templator, cfg, outputPath)
proto.Generate(Templator, cfg)

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

}
},
Expand Down
7 changes: 0 additions & 7 deletions example/README.MD

This file was deleted.

Expand Up @@ -11,12 +11,12 @@ create:

generate:
go_options:
import_path: github.com/commitdev
import_path: github.com/yourrepo
extra_modifiers:
google/api/annotations.proto: google.golang.org/genproto/googleapis/api/annotations
google/api/http.proto: google.golang.org/genproto/googleapis/api/annotations
plugins:
- name: go
type: go
flags: plugins=grpc
output: ../../sprout-example-go
output: ../gen/go
3 changes: 3 additions & 0 deletions example/hello-world/go.mod
@@ -0,0 +1,3 @@
module github.com/yourrepo/hello-world

go 1.12
Expand Up @@ -2,7 +2,7 @@ package health

import (
"context"
api "github.com/commitdev/sprout-example-go/health"
api "github.com/yourrepo/hello-world-go/health"
)

type HealthServer struct {
Expand Down
Expand Up @@ -2,9 +2,9 @@ package helloworld

import (
"context"
api "github.com/commitdev/sprout-example-go/helloworld"
api "github.com/yourrepo/hello-world-idl/gen/go/helloworld"
)

type HelloworldServer struct {

}
}
12 changes: 6 additions & 6 deletions example/sprout.yml → example/hello-world/sprout.yml
@@ -1,11 +1,11 @@
organization: mycompany
name: sprout-example
description: This is a test project
git-repo: github.com/commitdev
docker-repo: test.com
name: hello-world
description:
git-repo: github.com/yourrepo
docker-repo:
maintainers:
- name: bob
email: bob@test.com
# - name: bob
# email: bob@test.com

network:
grpc:
Expand Down
24 changes: 17 additions & 7 deletions generate/golang/generate.go
Expand Up @@ -10,14 +10,24 @@ import (
"os"
)

func Generate(templator *templator.Templator, config *config.SproutConfig, outPath string) {
GenerateHealthServer(templator, config, outPath)
GenerateServers(templator, config, outPath)
func Generate(templator *templator.Templator, config *config.SproutConfig) {
GenerateGoMod(templator, config)
GenerateHealthServer(templator, config)
GenerateServers(templator, config)
}

func GenerateGoMod(templator *templator.Templator, config *config.SproutConfig) {
f, err := os.Create("go.mod")

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

templator.Go.GoMod.Execute(f, config)
}

func GenerateServers(templator *templator.Templator, config *config.SproutConfig, outPath string) {
serverDirPath := fmt.Sprintf("%s/%s/%s/server", outPath, config.Name, config.Name)
func GenerateServers(templator *templator.Templator, config *config.SproutConfig) {
serverDirPath := "server"
err := util.CreateDirIfDoesNotExist(serverDirPath)
if err != nil {
log.Printf("Error creating server path: %v", err)
Expand Down Expand Up @@ -53,8 +63,8 @@ func GenerateServers(templator *templator.Templator, config *config.SproutConfig

}

func GenerateHealthServer(templator *templator.Templator, config *config.SproutConfig, outPath string) {
serverDirPath := fmt.Sprintf("%s/%s/%s/server", outPath, config.Name, config.Name)
func GenerateHealthServer(templator *templator.Templator, config *config.SproutConfig) {
serverDirPath := "server"
err := util.CreateDirIfDoesNotExist(serverDirPath)
if err != nil {
log.Printf("Error creating server path: %v", err)
Expand Down
30 changes: 15 additions & 15 deletions generate/proto/generate.go
Expand Up @@ -11,15 +11,15 @@ import (
"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 Generate(templator *templator.Templator, config *config.SproutConfig) {
GenerateProtoToolConfig(templator, config)
GenerateProtoHealth(templator, config)
GenerateProtoServices(templator, config)
GenerateProtoServiceLibs(config)
}

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

err := util.CreateDirIfDoesNotExist(protoPath)
Expand All @@ -34,8 +34,8 @@ func GenerateProtoToolConfig(templator *templator.Templator, config *config.Spro
templator.ProtoToolTemplate.Execute(f, config)
}

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

err := util.CreateDirIfDoesNotExist(protoHealthPath)
Expand All @@ -51,8 +51,8 @@ func GenerateProtoHealth(templator *templator.Templator, config *config.SproutCo
templator.ProtoHealthTemplate.Execute(f, config)
}

func GenerateProtoServices(templator *templator.Templator, config *config.SproutConfig, outPath string) {
protoToolConfigPath := fmt.Sprintf("%s/%s/%s-idl/proto", outPath, config.Name, config.Name)
func GenerateProtoServices(templator *templator.Templator, config *config.SproutConfig) {
protoToolConfigPath := fmt.Sprintf("../%s-idl/proto", config.Name)
for _, s := range config.Services {
idlPath := fmt.Sprintf("%s/%s", protoToolConfigPath, s.Name)
err := util.CreateDirIfDoesNotExist(idlPath)
Expand All @@ -69,12 +69,12 @@ func GenerateProtoServices(templator *templator.Templator, config *config.Sprout

}

func GenerateProtoServiceLibs(config *config.SproutConfig, outPath string) {
protoToolConfigPath := fmt.Sprintf("%s/%s/%s-idl/proto", outPath, config.Name, config.Name)
func GenerateProtoServiceLibs(config *config.SproutConfig) {
protoToolConfigPath := fmt.Sprintf("../%s-idl/proto", config.Name)
cmd := exec.Command("prototool", "generate")
cmd.Dir = protoToolConfigPath
err := cmd.Run()
bytes, err := cmd.Output()
if err != nil {
log.Printf("Error executing prototool generate: %v", err)
log.Printf("Error executing prototool generate: %v", string(bytes))
}
}
3 changes: 3 additions & 0 deletions templates/golang/go_mod.tmpl
@@ -0,0 +1,3 @@
module {{ .GitRepo }}/{{ .Name }}

go 1.12
Empty file added templates/golang/main.tmpl
Empty file.
4 changes: 2 additions & 2 deletions templates/golang/server.tmpl
Expand Up @@ -2,9 +2,9 @@ package {{ .ServiceName }}

import (
"context"
api "{{.GitRepo}}/{{ .ProjectName }}-go/{{ .ServiceName }}"
api "{{.GitRepo}}/{{ .ProjectName }}-idl/gen/go/{{ .ServiceName }}"
)

type {{ .ServiceName | Title }}Server struct {

}
}
Empty file added templates/golang/start.tmpl
Empty file.
2 changes: 1 addition & 1 deletion templates/proto/prototool.tmpl
Expand Up @@ -22,4 +22,4 @@ generate:
- name: go
type: go
flags: plugins=grpc
output: ../../{{ .Name }}-go
output: ../gen/go
15 changes: 15 additions & 0 deletions templator/templator.go
Expand Up @@ -7,12 +7,14 @@ import (
)

type GoTemplator struct {
GoMod *template.Template
GoServer *template.Template
GoHealthServer *template.Template

}

type Templator struct {
Sprout *template.Template
ProtoToolTemplate *template.Template
ProtoHealthTemplate *template.Template
Go *GoTemplator
Expand All @@ -29,6 +31,7 @@ func NewTemplator(box *packr.Box) *Templator {
ProtoToolTemplate: protoToolTemplate,
ProtoHealthTemplate: protoHealthTemplate,
Go: NewGoTemplator(box),
Sprout: NewSproutTemplator(box),
}
}

Expand All @@ -39,9 +42,21 @@ func NewGoTemplator(box *packr.Box) *GoTemplator {
goHealthTemplateSource, _ := box.FindString("golang/health_server.tmpl")
goHealthServerTemplate, _ := template.New("GoHealthServerTemplate").Parse(goHealthTemplateSource)

goModTemplateSource, _ := box.FindString("golang/go_mod.tmpl")
goModTemplate, _ := template.New("GoModTemplate").Parse(goModTemplateSource)

return &GoTemplator{
GoMod: goModTemplate,
GoServer: goServerTemplate,
GoHealthServer: goHealthServerTemplate,
}

}

func NewSproutTemplator(box *packr.Box) *template.Template {
templateSource, _ := box.FindString("sprout/sprout.tmpl")
template, _ := template.New("SproutTemplate").Funcs(util.FuncMap).Parse(templateSource)

return template
}

0 comments on commit 41a5fae

Please sign in to comment.