Skip to content

Commit

Permalink
logging and module fixs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritesh-Patel committed Sep 10, 2019
1 parent 9eeb165 commit 94079f8
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,3 +1,3 @@
main-packr.go
packrd
sprout
/sprout
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -13,7 +13,7 @@ build:
packr2 build -o sprout
packr2 clean

build-example:
build-example: build clean-example
mkdir -p example
cd example && ../sprout create -p "hello-world"
cd example/hello-world && ../../sprout generate -l go
Expand Down
13 changes: 8 additions & 5 deletions cmd/create.go
@@ -1,10 +1,9 @@
package cmd

import (
"fmt"
"log"
"os"
"fmt"


"github.com/spf13/cobra"
)
Expand All @@ -23,9 +22,13 @@ var createCmd = &cobra.Command{
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)
rootDir := fmt.Sprintf("./%v", projectName)

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

sproutConfigPath := fmt.Sprintf("%v/sprout.yml", projectName)
Expand Down
9 changes: 5 additions & 4 deletions config/config.go
Expand Up @@ -13,22 +13,23 @@ type Maintainers struct {
}

type Grpc struct {
Host string
Port int
}

type Graphql struct {
Enabled bool
Port int
Port int
}

type Http struct {
Enabled bool
Port int
Port int
}

type Network struct {
Grpc Grpc
Http Http
Grpc Grpc
Http Http
Graphql Graphql
}

Expand Down
3 changes: 3 additions & 0 deletions example/hello-world-idl/go.mod
@@ -0,0 +1,3 @@
module github.com/yourrepo/hello-world-idl

go 1.12
7 changes: 7 additions & 0 deletions example/hello-world/go.mod
@@ -1,3 +1,10 @@
module github.com/yourrepo/hello-world

go 1.12

replace github.com/yourrepo/hello-world-idl => ../hello-world-idl


require (
github.com/yourrepo/hello-world-idl
)
22 changes: 22 additions & 0 deletions example/hello-world/main.go
@@ -1,4 +1,26 @@
package main
import (
"context"
"log"
"net"

health "github.com/yourrepo/hello-world/server/health"

"google.golang.org/grpc"
)

func main() {
lis, err := net.Listen("tcp", "0.0.0.0:3000")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()

//TODO: Register your servers here
healthServer := health.NewServer()
health.RegisterHealthServer(s, healthServer)

if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
1 change: 1 addition & 0 deletions example/hello-world/sprout.yml
Expand Up @@ -9,6 +9,7 @@ maintainers:

network:
grpc:
host: 0.0.0.0
port: 3000
http:
enabled: true
Expand Down
23 changes: 13 additions & 10 deletions generate/golang/generate.go
Expand Up @@ -18,16 +18,19 @@ func Generate(templator *templator.Templator, config *config.SproutConfig) {
}

func GenerateGoMain(templator *templator.Templator, config *config.SproutConfig) {
f, err := os.Create("main.go")
if _, err := os.Stat("main.go"); os.IsNotExist(err) {

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

templator.Go.GoMain.Execute(f, config)
}
f, err := os.Create("main.go")

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

templator.Go.GoMain.Execute(f, config)
} else {
log.Printf("main.go already exists. skipping.")
}
}

func GenerateGoMod(templator *templator.Templator, config *config.SproutConfig) {
f, err := os.Create("go.mod")
Expand All @@ -50,7 +53,7 @@ func GenerateServers(templator *templator.Templator, config *config.SproutConfig
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)
log.Printf("%s server exists skipping.", s.Name)
continue
}
log.Printf("generating %s", s.Name)
Expand All @@ -65,10 +68,10 @@ func GenerateServers(templator *templator.Templator, config *config.SproutConfig
log.Printf("Error: %v", err)
}

data := map[string]string {
data := map[string]string{
"ProjectName": config.Name,
"ServiceName": s.Name,
"GitRepo": config.GitRepo,
"GitRepo": config.GitRepo,
}

templator.Go.GoServer.Execute(f, data)
Expand Down
18 changes: 18 additions & 0 deletions generate/proto/generate.go
Expand Up @@ -16,6 +16,21 @@ func Generate(templator *templator.Templator, config *config.SproutConfig) {
GenerateProtoHealth(templator, config)
GenerateProtoServices(templator, config)
GenerateProtoServiceLibs(config)
GenerateGoModIDL(templator, config)
}

func GenerateGoModIDL(templator *templator.Templator, config *config.SproutConfig) {
idlPath := fmt.Sprintf("../%s-idl", config.Name)
idlOutput := fmt.Sprintf("%s/go.mod", idlPath)

f, err := os.Create(idlOutput)

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

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

func GenerateProtoToolConfig(templator *templator.Templator, config *config.SproutConfig) {
Expand Down Expand Up @@ -64,6 +79,7 @@ func GenerateProtoServices(templator *templator.Templator, config *config.Sprout
protoPath := fmt.Sprintf("%s/%s.proto", s.Name, s.Name)
cmd := exec.Command("prototool", "create", protoPath)
cmd.Dir = protoToolConfigPath
log.Printf("Generating %v", protoPath)
cmd.Run()
}

Expand All @@ -74,6 +90,8 @@ func GenerateProtoServiceLibs(config *config.SproutConfig) {
cmd := exec.Command("prototool", "generate")
cmd.Dir = protoToolConfigPath
bytes, err := cmd.Output()

log.Print("Generating proto service libs...")
if err != nil {
log.Printf("Error executing prototool generate: %v", string(bytes))
}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -19,6 +19,7 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/commitdev/sprout/example/hello-world v0.0.0-20190827182108-cfad4c3d94cc h1:zBLjQxkC2LT1e9eMs0I2k26NXXZVT/XGrnuqdAsFt3A=
github.com/commitdev/sprout/example/hello-world v0.0.0-20190827183525-9eeb1651f4f4 h1:pQw2XR8va971sW7QIX18I7FWft54TjLdQex5MHCcVtg=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/bbolt v1.3.3/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down Expand Up @@ -70,6 +71,7 @@ github.com/gofrs/flock v0.7.1 h1:DP+LD/t0njgoPBvT5MJLeliUIVQR03hiKR6vezdwHlc=
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -7,7 +7,7 @@ import (
)

func main() {
templates := packr.New("templates","./templates")
templates := packr.New("templates", "./templates")
templator := templator.NewTemplator(templates)
cmd.Execute(templator)
}
6 changes: 6 additions & 0 deletions templates/golang/go_mod.tmpl
@@ -1,3 +1,9 @@
module {{ .GitRepo }}/{{ .Name }}

go 1.12

replace {{ .GitRepo }}/{{ .Name }}-idl => ../{{ .Name }}-idl

require (
{{ .GitRepo }}/{{ .Name }}-idl v0.0.0
)
3 changes: 3 additions & 0 deletions templates/golang/go_mod_idl.tmpl
@@ -0,0 +1,3 @@
module {{ .GitRepo }}/{{ .Name }}-idl

go 1.12
8 changes: 6 additions & 2 deletions templates/golang/health_server.tmpl
Expand Up @@ -2,13 +2,17 @@ package health

import (
"context"
api "{{ .GitRepo }}/{{ .Name }}-go/health"
api "{{ .GitRepo }}/{{ .Name }}-idl/gen/go/health"
)

type HealthServer struct {

}

func NewHealthServer() *HealthServer {
return &HealthServer{}
}

func (s *HealthServer) Check(ctx context.Context, req *api.HealthCheckRequest) (*api.HealthCheckResponse, error) {
resp := &api.HealthCheckResponse{
Status: api.HealthCheckResponse_SERVING,
Expand All @@ -18,4 +22,4 @@ func (s *HealthServer) Check(ctx context.Context, req *api.HealthCheckRequest) (

func (s *HealthServer) Watch(req *api.HealthCheckRequest, server api.Health_WatchServer) error {
return nil
}
}
26 changes: 26 additions & 0 deletions templates/golang/main.tmpl
@@ -1,4 +1,30 @@
package main
import (
"log"
"net"

health "{{ .GitRepo }}/{{ .Name }}/server/health"
healthpb "{{ .GitRepo }}/{{ .Name }}-idl/gen/go/health"


"google.golang.org/grpc"
)

func main() {
grpcAddr := "{{ .Network.Grpc.Host }}:{{ .Network.Grpc.Port }}"
lis, err := net.Listen("tcp", grpcAddr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()

//TODO: Register your servers here
healthServer := health.NewHealthServer()
healthpb.RegisterHealthServer(s, healthServer)

log.Printf("Starting grpc server on %v...", grpcAddr)

if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
23 changes: 23 additions & 0 deletions templates/sprout/sprout.tmpl
@@ -0,0 +1,23 @@
organization: mycompany
name: {{.}}
description:
git-repo: github.com/yourrepo
docker-repo:
maintainers:
# - name: bob
# email: bob@test.com

network:
grpc:
host: 0.0.0.0
port: 3000
http:
enabled: true
port: 8080
graphql:
enabled: true
port: 8082

services:
- name: helloworld
description: Hello world!.
27 changes: 15 additions & 12 deletions templator/templator.go
@@ -1,21 +1,21 @@
package templator

import (
"github.com/gobuffalo/packr/v2"
"github.com/commitdev/sprout/util"
"github.com/gobuffalo/packr/v2"
"text/template"
)

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

}

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

Expand All @@ -46,13 +46,17 @@ func NewGoTemplator(box *packr.Box) *GoTemplator {
goModTemplateSource, _ := box.FindString("golang/go_mod.tmpl")
goModTemplate, _ := template.New("GoModTemplate").Parse(goModTemplateSource)

goModIDLTemplateSource, _ := box.FindString("golang/go_mod_idl.tmpl")
goModIDLTemplate, _ := template.New("GoModTemplate").Parse(goModIDLTemplateSource)

goMainTemplateSource, _ := box.FindString("golang/main.tmpl")
goMainTemplate, _ := template.New("GoModTemplate").Parse(goMainTemplateSource)
goMainTemplate, _ := template.New("GoMainTemplate").Parse(goMainTemplateSource)

return &GoTemplator{
GoMain: goMainTemplate,
GoMod: goModTemplate,
GoServer: goServerTemplate,
GoMain: goMainTemplate,
GoMod: goModTemplate,
GoModIDL: goModIDLTemplate,
GoServer: goServerTemplate,
GoHealthServer: goHealthServerTemplate,
}

Expand All @@ -64,4 +68,3 @@ func NewSproutTemplator(box *packr.Box) *template.Template {

return template
}

0 comments on commit 94079f8

Please sign in to comment.