Skip to content

Commit 94079f8

Browse files
committed
logging and module fixs
1 parent 9eeb165 commit 94079f8

File tree

19 files changed

+163
-38
lines changed

19 files changed

+163
-38
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
main-packr.go
22
packrd
3-
sprout
3+
/sprout

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ build:
1313
packr2 build -o sprout
1414
packr2 clean
1515

16-
build-example:
16+
build-example: build clean-example
1717
mkdir -p example
1818
cd example && ../sprout create -p "hello-world"
1919
cd example/hello-world && ../../sprout generate -l go

cmd/create.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"log"
56
"os"
6-
"fmt"
7-
87

98
"github.com/spf13/cobra"
109
)
@@ -23,9 +22,13 @@ var createCmd = &cobra.Command{
2322
Short: "Create new project.",
2423
Run: func(cmd *cobra.Command, args []string) {
2524

26-
err := os.Mkdir(projectName, os.ModePerm)
27-
if os.IsExist(err){
28-
log.Fatalf("Directory %v already exists!", projectName)
25+
rootDir := fmt.Sprintf("./%v", projectName)
26+
27+
err := os.Mkdir(rootDir, os.ModePerm)
28+
if os.IsExist(err) {
29+
log.Fatalf("Directory %v already exists! Error: %v", projectName, err)
30+
} else if err != nil {
31+
log.Fatalf("Error creating root: %v ", err)
2932
}
3033

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

config/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ type Maintainers struct {
1313
}
1414

1515
type Grpc struct {
16+
Host string
1617
Port int
1718
}
1819

1920
type Graphql struct {
2021
Enabled bool
21-
Port int
22+
Port int
2223
}
2324

2425
type Http struct {
2526
Enabled bool
26-
Port int
27+
Port int
2728
}
2829

2930
type Network struct {
30-
Grpc Grpc
31-
Http Http
31+
Grpc Grpc
32+
Http Http
3233
Graphql Graphql
3334
}
3435

example/hello-world-idl/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/yourrepo/hello-world-idl
2+
3+
go 1.12

example/hello-world/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
module github.com/yourrepo/hello-world
22

33
go 1.12
4+
5+
replace github.com/yourrepo/hello-world-idl => ../hello-world-idl
6+
7+
8+
require (
9+
github.com/yourrepo/hello-world-idl
10+
)

example/hello-world/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
package main
2+
import (
3+
"context"
4+
"log"
5+
"net"
6+
7+
health "github.com/yourrepo/hello-world/server/health"
8+
9+
"google.golang.org/grpc"
10+
)
211

312
func main() {
13+
lis, err := net.Listen("tcp", "0.0.0.0:3000")
14+
if err != nil {
15+
log.Fatalf("failed to listen: %v", err)
16+
}
17+
s := grpc.NewServer()
18+
19+
//TODO: Register your servers here
20+
healthServer := health.NewServer()
21+
health.RegisterHealthServer(s, healthServer)
22+
23+
if err := s.Serve(lis); err != nil {
24+
log.Fatalf("failed to serve: %v", err)
25+
}
426
}

example/hello-world/sprout.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ maintainers:
99

1010
network:
1111
grpc:
12+
host: 0.0.0.0
1213
port: 3000
1314
http:
1415
enabled: true

generate/golang/generate.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ func Generate(templator *templator.Templator, config *config.SproutConfig) {
1818
}
1919

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

23-
if err != nil {
24-
log.Printf("Error: %v", err)
25-
}
26-
27-
templator.Go.GoMain.Execute(f, config)
28-
}
23+
f, err := os.Create("main.go")
2924

25+
if err != nil {
26+
log.Printf("Error: %v", err)
27+
}
3028

29+
templator.Go.GoMain.Execute(f, config)
30+
} else {
31+
log.Printf("main.go already exists. skipping.")
32+
}
33+
}
3134

3235
func GenerateGoMod(templator *templator.Templator, config *config.SproutConfig) {
3336
f, err := os.Create("go.mod")
@@ -50,7 +53,7 @@ func GenerateServers(templator *templator.Templator, config *config.SproutConfig
5053
serverLibPath := fmt.Sprintf("%s/%s", serverDirPath, s.Name)
5154
err := os.Mkdir(serverLibPath, os.ModePerm)
5255
if os.IsExist(err) {
53-
log.Printf("%s service exists skipping.", s.Name)
56+
log.Printf("%s server exists skipping.", s.Name)
5457
continue
5558
}
5659
log.Printf("generating %s", s.Name)
@@ -65,10 +68,10 @@ func GenerateServers(templator *templator.Templator, config *config.SproutConfig
6568
log.Printf("Error: %v", err)
6669
}
6770

68-
data := map[string]string {
71+
data := map[string]string{
6972
"ProjectName": config.Name,
7073
"ServiceName": s.Name,
71-
"GitRepo": config.GitRepo,
74+
"GitRepo": config.GitRepo,
7275
}
7376

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

generate/proto/generate.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ func Generate(templator *templator.Templator, config *config.SproutConfig) {
1616
GenerateProtoHealth(templator, config)
1717
GenerateProtoServices(templator, config)
1818
GenerateProtoServiceLibs(config)
19+
GenerateGoModIDL(templator, config)
20+
}
21+
22+
func GenerateGoModIDL(templator *templator.Templator, config *config.SproutConfig) {
23+
idlPath := fmt.Sprintf("../%s-idl", config.Name)
24+
idlOutput := fmt.Sprintf("%s/go.mod", idlPath)
25+
26+
f, err := os.Create(idlOutput)
27+
28+
err = util.CreateDirIfDoesNotExist(idlPath)
29+
if err != nil {
30+
log.Printf("Error: %v", err)
31+
}
32+
33+
templator.Go.GoModIDL.Execute(f, config)
1934
}
2035

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

@@ -74,6 +90,8 @@ func GenerateProtoServiceLibs(config *config.SproutConfig) {
7490
cmd := exec.Command("prototool", "generate")
7591
cmd.Dir = protoToolConfigPath
7692
bytes, err := cmd.Output()
93+
94+
log.Print("Generating proto service libs...")
7795
if err != nil {
7896
log.Printf("Error executing prototool generate: %v", string(bytes))
7997
}

0 commit comments

Comments
 (0)