Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ vendor/

.idea
*.iml
.vscode/
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ kit help

Also read this [medium story](https://medium.com/@kujtimii.h/creating-a-todo-app-using-gokit-cli-20f066a58e1)
# Create a new service
The kit tool use modules to manage dependencies, please make sure your go version >= 1.3, or
GO111MODULE is set on. If you want to specify the module name, you should use the --module flag, otherwise, the module name in the go.mod file will be set as your project name.
```bash
kit new service hello
kit n s hello # using aliases
```
This will generate the initial folder structure and the service interface
or
```bash
kit new service hello --module github.com/{group name}/hello
kit n s hello -m github.com/{group name}/hello # using aliases
```

This will generate the initial folder structure, the go.mod file and the service interface

`service-name/pkg/service/service.go`
```go
Expand All @@ -47,6 +55,7 @@ type HelloService interface {
// e.x: Foo(ctx context.Context,s string)(rs string, err error)
}
```
When you are generating the service and the client library, the module name in the go.mod file could be autodetected.

# Generate the service
```bash
Expand Down Expand Up @@ -122,4 +131,4 @@ docker-compose up
```

After you run `docker-compose up` your services will start up and any change you make to your code will automatically
rebuild and restart your service (only the service that is changed)
rebuild and restart your service (only the service that is changed)
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func init() {
RootCmd.PersistentFlags().BoolP("debug", "d", false, "If you want to se the debug logs.")
RootCmd.PersistentFlags().BoolP("force", "f", false, "Force overide existing files without asking.")
RootCmd.PersistentFlags().StringP("folder", "b", "", "If you want to specify the base folder of the project.")

viper.BindPFlag("gk_folder", RootCmd.PersistentFlags().Lookup("folder"))
viper.BindPFlag("gk_force", RootCmd.PersistentFlags().Lookup("force"))
viper.BindPFlag("gk_debug", RootCmd.PersistentFlags().Lookup("debug"))
Expand Down
3 changes: 3 additions & 0 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/kujtimiihoxha/kit/generator"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var serviceCmd = &cobra.Command{
Expand All @@ -24,4 +25,6 @@ var serviceCmd = &cobra.Command{

func init() {
newCmd.AddCommand(serviceCmd)
serviceCmd.Flags().StringP("module", "m", "", "The module name that you plan to set in the project")
viper.BindPFlag("n_s_module", serviceCmd.Flags().Lookup("module"))
}
36 changes: 20 additions & 16 deletions generator/generate_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1683,35 +1683,39 @@ func (g *generateCmd) generateRun() (*PartialGenerator, error) {
jen.Lit("URL"),
jen.Id("*zipkinURL"),
),
jen.List(jen.Id("collector"), jen.Err()).Op(":=").Qual(
"github.com/openzipkin/zipkin-go-opentracing", "NewHTTPCollector",
jen.Id("reporter").Op(":=").Qual(
"github.com/openzipkin/zipkin-go/reporter/http", "NewReporter",
).Call(jen.Id("*zipkinURL")),
jen.Defer().Id("reporter").Dot("Close").Call(),
jen.List(jen.Id("endpoint"), jen.Id("err")).Op(":=").Qual(
"github.com/openzipkin/zipkin-go", "NewEndpoint",
).Call(
jen.Lit(g.name),
jen.Lit("localhost:80"),
),
jen.If(jen.Err().Op("!=").Nil()).Block(
jen.Id("logger").Dot("Log").Call(
jen.Lit("err"),
jen.Id("err"),
),
jen.Qual("os", "Exit").Call(jen.Lit(1)),
),
jen.Defer().Id("collector").Dot("Close").Call(),
jen.Id("recorder").Op(":=").Qual(
"github.com/openzipkin/zipkin-go-opentracing", "NewRecorder",
).Call(
jen.Id("collector"),
jen.Lit(false),
jen.Lit("localhost:80"),
jen.Lit(g.name),
),
jen.List(jen.Id("tracer"), jen.Id("err")).Op("=").Qual(
"github.com/openzipkin/zipkin-go-opentracing", "NewTracer",
).Call(jen.Id("recorder")),
jen.Id("localEndpoint").Op(":=").Qual("github.com/openzipkin/zipkin-go", "WithLocalEndpoint").Call(jen.Id("endpoint")),
jen.List(jen.Id("nativeTracer"), jen.Id("err")).Op(":=").Qual(
"github.com/openzipkin/zipkin-go", "NewTracer",
).Call(jen.Id("reporter"), jen.Id("localEndpoint")),
jen.If(jen.Err().Op("!=").Nil()).Block(
jen.Id("logger").Dot("Log").Call(
jen.Lit("err"),
jen.Id("err"),
),
jen.Qual("os", "Exit").Call(jen.Lit(1)),
),
jen.Id("tracer").Op("=").Qual(
"github.com/openzipkin-contrib/zipkin-go-opentracing", "Wrap",
).Call(
jen.Id("nativeTracer"),
),
).Else().If(jen.Id("*lightstepToken").Op("!=").Lit("")).Block(
jen.Id("logger").Dot("Log").Call(
jen.Lit("tracer"),
Expand All @@ -1728,8 +1732,8 @@ func (g *generateCmd) generateRun() (*PartialGenerator, error) {
),
),
jen.Defer().Qual(
"github.com/lightstep/lightstep-tracer-go", "FlushLightStepTracer",
).Call(jen.Id("tracer")),
"github.com/lightstep/lightstep-tracer-go", "Flush",
).Call(jen.Qual("context","Background").Call(), jen.Id("tracer")),
).Else().If(jen.Id("*appdashAddr").Op("!=").Lit("")).Block(
jen.Id("logger").Dot("Log").Call(
jen.Lit("tracer"),
Expand Down
23 changes: 23 additions & 0 deletions generator/new_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generator

import (
"fmt"
"os/exec"
"path"
"strings"

Expand Down Expand Up @@ -40,6 +41,12 @@ func NewNewService(name string) Gen {
// Generate will run the generator.
func (g *NewService) Generate() error {
g.CreateFolderStructure(g.destPath)
err := g.genModule()
if err != nil {
println(err.Error())
return err
}

comments := []string{
"Add your methods here",
"e.x: Foo(ctx context.Context,s string)(rs string, err error)",
Expand All @@ -53,3 +60,19 @@ func (g *NewService) Generate() error {
)
return g.fs.WriteFile(g.filePath, g.srcFile.GoString(), false)
}

func (g *NewService) genModule() error {
exist, _ := g.fs.Exists(g.name + "/go.mod")
if exist {
return nil
}

moduleName := g.name
if viper.GetString("n_s_module") != "" {
moduleName = viper.GetString("n_s_module")
}
cmdStr := "cd " + g.name + " && go mod init " + moduleName
cmd := exec.Command("sh", "-c", cmdStr)
_, err := cmd.Output()
return err
}
26 changes: 0 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
package main

import (
"os"
"path"
"path/filepath"
"runtime"
"strings"

"github.com/kujtimiihoxha/kit/cmd"
"github.com/kujtimiihoxha/kit/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/viper"
)

func main() {
setDefaults()
viper.AutomaticEnv()
gosrc := strings.TrimSuffix(utils.GetGOPATH(), afero.FilePathSeparator ) + afero.FilePathSeparator + "src" + afero.FilePathSeparator
pwd, err := os.Getwd()
if err != nil {
logrus.Error(err)
return
}
gosrc, err = filepath.EvalSymlinks(gosrc)
if err != nil {
logrus.Error(err)
return
}
pwd, err = filepath.EvalSymlinks(pwd)
if err != nil {
logrus.Error(err)
return
}
if !strings.HasPrefix(pwd, gosrc) {
logrus.Error("The project must be in the $GOPATH/src folder for the generator to work.")
return
}
cmd.Execute()
}

Expand Down
Loading