Skip to content

Commit

Permalink
Add all remaining features and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kujtimiihoxha committed Jul 7, 2017
1 parent 42e7dc8 commit 0327e75
Show file tree
Hide file tree
Showing 49 changed files with 2,831 additions and 126 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ vendor/

.DS_Store
*.iml
test_dir/
62 changes: 53 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Go-Kit generator.
### This generator is still a work in progress
Go-kit generator is a cli application that generates boilerplate code for your go-kit services.

## Why?
Expand All @@ -8,6 +7,7 @@ Go-kit generator is a cli application that generates boilerplate code for your g

## Installation
```bash
go get github.com/kujtimiihoxha/gk
go install github.com/kujtimiihoxha/gk
```
## Running the generator
Expand Down Expand Up @@ -35,27 +35,69 @@ project
**service.go**
```go
package service

// Implement yor service methods methods.
// e.x: Foo(ctx context.Context,s string)(rs string,err error)
type HelloService interface {
// Write your interface methods
}
```
Now you need to add the interface methods and initiate your service:
e.x:
```go
package service
import "context"
// Implement yor service methods methods.
// e.x: Foo(ctx context.Context,s string)(rs string,err error)
type HelloService interface {
// Write your interface methods
World(ctx context.Context, s string) (rs string, err error)
Foo(ctx context.Context,s string)(rs string,err error)
}
```
than run :
```bash
gk init hello
```
this will create the service `struct` , `methods`, `endpoints`, `transport` .

[Example GIF](https://drive.google.com/open?id=0B11R03qTqELWbk9nYXRtOTRQdDg)

The final folder structure is the same as [addsvc](https://github.com/peterbourgon/go-microservices/tree/master/addsvc)
By Default the generator will use `default_transport` setting from `gk.json` and create the transport. If you want to specify
the transport use `-t` flag
```bash
gk init hello -t grpc
```

## Add other transports
To add another transport to your existing service use `gk add [transporteType] [serviceName]`
e.x adding grpc:
```bash
gk add grpc hello
```
For `grpc` and `thrift` after you execute the above command you will see this message :
```bash
INFO[0000] Generating grpc transport...
WARN[0000] --------------------------------------------------------------------
WARN[0000] The service is still not ready!!
WARN[0000] To create the grpc transport please create your protobuf.
WARN[0000] Than follow the instructions in compile.sh and compile the .proto file.
WARN[0000] After the file is compiled run `gk init grpc hello`.
WARN[0000] --------------------------------------------------------------------
```
to complete the generation of the grpc transport you need to implement the protobuffer and compile it, the compile script
with instructions on how to install proto is generated by `gk`. After you compile the protobuffer run:
```bash
gk init grpc hello
```
After this the `handler.go` file will be created and you will only need to implement the `Decode/Encode` of the grpc message.

[Example GIF](https://drive.google.com/open?id=0B11R03qTqELWZE9mcEhZVHhFWFk)

e.x adding thrift:
```bash
gk add thrift hello
```
This generator will work similar as the `grpc` generator.

[Example GIF](https://drive.google.com/open?id=0B11R03qTqELWbE9VeFB2ZDdhb2c)

## I don't like the folder structure!

Expand Down Expand Up @@ -88,19 +130,19 @@ Global Flags:
## What is working
The example you see here https://github.com/go-kit/kit/issues/70

## Examples
You can find examples under the `test_dir`

## TODO-s

- Implement support for other transports besides `http`
- Implement the update commands, this commands would be used to update an existing service e.x add
a new request parameter to an endpoint.
a new request parameter to an endpoint(Probably not needed).
- Implement middleware generator (service,endpoint).
- Implement generator for adding new transports to a service.
- Implement automatic creation of the service main file.
- Tests tests tests ...
## Warnings

- I only tested this on the mac, should work on other os-s but I have not tested it, I would appreciate feedback on this.
- Commands may change in the future, this project is still a work in progress.
## Contribute
Thanks a lot for contributing.

Expand All @@ -112,4 +154,6 @@ export GK_FOLDER="test_dir"
```
Create a folder in the `gk` repository called `test_dir`, now every time you run `go run main.go [anything]`
`gk` will treat `test_dir` as the project root.

If you edit the templates you need to run `compile.sh` inside the templates folder.

15 changes: 15 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"github.com/spf13/cobra"
)

// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "Use to add additional transports to a service",
}

func init() {
RootCmd.AddCommand(addCmd)
}
29 changes: 29 additions & 0 deletions cmd/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"github.com/Sirupsen/logrus"
"github.com/kujtimiihoxha/gk/generator"
"github.com/spf13/cobra"
)

// grpcCmd represents the grpc command
var grpcCmd = &cobra.Command{
Use: "grpc",
Short: "Initiates grpc transport after creating the protobuf",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
logrus.Error("You must provide the service name")
return
}
g := generator.NewGRPCInitGenerator()
err := g.Generate(args[0])
if err != nil {
logrus.Error(err)
return
}
},
}

func init() {
initCmd.AddCommand(grpcCmd)
}
40 changes: 40 additions & 0 deletions cmd/grpc_add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"github.com/Sirupsen/logrus"
"github.com/kujtimiihoxha/gk/generator"
"github.com/spf13/cobra"
)

// grpc_addCmd represents the grpc_add command
var grpc_addCmd = &cobra.Command{
Use: "grpc",
Short: "Add grpc transport to service",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
logrus.Error("You must provide the service name")
return
}
g := generator.NewAddGRPCGenerator()
err := g.Generate(args[0])
if err != nil {
logrus.Error(err)
return
}
},
}

func init() {
addCmd.AddCommand(grpc_addCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// grpc_addCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// grpc_addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
40 changes: 40 additions & 0 deletions cmd/http_add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"github.com/Sirupsen/logrus"
"github.com/kujtimiihoxha/gk/generator"
"github.com/spf13/cobra"
)

// http_addCmd represents the http_add command
var http_addCmd = &cobra.Command{
Use: "http",
Short: "Add http transport to service",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
logrus.Error("You must provide the service name")
return
}
g := generator.NewAddHttpGenerator()
err := g.Generate(args[0])
if err != nil {
logrus.Error(err)
return
}
},
}

func init() {
addCmd.AddCommand(http_addCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// http_addCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// http_addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
3 changes: 1 addition & 2 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
var newCmd = &cobra.Command{
Use: "new",
Aliases: []string{"n"},
Short: "A set of generators used to create new services/endpoints/transports/middlewares",
Short: "A set of generators used to create new services/transports/middlewares",
}

func init() {
RootCmd.AddCommand(newCmd)

}
28 changes: 28 additions & 0 deletions cmd/thrift.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"github.com/Sirupsen/logrus"
"github.com/kujtimiihoxha/gk/generator"
"github.com/spf13/cobra"
)

var thriftCmd = &cobra.Command{
Use: "thrift",
Short: "Initiates thrift transport after creating the thrift service",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
logrus.Error("You must provide the service name")
return
}
g := generator.NewThriftInitGenerator()
err := g.Generate(args[0])
if err != nil {
logrus.Error(err)
return
}
},
}

func init() {
initCmd.AddCommand(thriftCmd)
}
39 changes: 39 additions & 0 deletions cmd/thrift_add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"github.com/Sirupsen/logrus"
"github.com/kujtimiihoxha/gk/generator"
"github.com/spf13/cobra"
)

var thrift_addCmd = &cobra.Command{
Use: "thrift",
Short: "Add thrift transport to service",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
logrus.Error("You must provide the service name")
return
}
g := generator.NewAddThriftGenerator()
err := g.Generate(args[0])
if err != nil {
logrus.Error(err)
return
}
},
}

func init() {
addCmd.AddCommand(thrift_addCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// thrift_addCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// thrift_addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
Loading

0 comments on commit 0327e75

Please sign in to comment.