Skip to content

Commit

Permalink
cli: add usecase generator & fix write config
Browse files Browse the repository at this point in the history
  • Loading branch information
agungdwiprasetyo committed Jul 11, 2024
1 parent eef29cb commit 0322cc4
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 5 deletions.
13 changes: 13 additions & 0 deletions cmd/candi/cli_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func parseInput(flagParam *flagParameter) (srvConfig serviceConfig) {
goto stageReadInputModule
}
goto stageSelectServerHandler

case AddUsecase:
stageAddUsecaseReadInputModule:
flagParam.moduleName = candihelper.ToDelimited(readInput("Please input existing module name to be added usecase(s):"), '-')
moduleDir := flagParam.getFullModuleChildDir()
if err := validateDir(moduleDir); err != nil {
fmt.Print(err.Error())
goto stageAddUsecaseReadInputModule
}

flagParam.serviceName = srvConfig.ServiceName
addUsecase(flagParam, readInput("Please input usecase name:"))
return
}

stageInputModules:
Expand Down
1 change: 1 addition & 0 deletions cmd/candi/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
InitMonorepo = "initMonorepo"
RunServiceMonorepo = "runServiceMonorepo"
AddHandler = "addHandler"
AddUsecase = "addUsecase"

RestHandler = "restHandler"
GrpcHandler = "grpcHandler"
Expand Down
5 changes: 3 additions & 2 deletions cmd/candi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func main() {
"1) Init monorepo codebase\n" +
"2) Init service\n" +
"3) Add module(s) in service\n" +
"4) Add delivery handler(s) in module\033[0m\n>> ")
"4) Add delivery handler(s) in module\n" +
"5) Add usecase(s) in module\033[0m\n>> ")
flagParam.scopeFlag = readInput()
}

Expand All @@ -108,7 +109,7 @@ func selectScope(flagParam flagParameter, scope string) {
}
monorepoGenerator(flagParam)
return
case RunServiceMonorepo: // 5
case RunServiceMonorepo: // 6
serviceRunner(flagParam.serviceName)
return
case InitService:
Expand Down
2 changes: 1 addition & 1 deletion cmd/candi/project_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func projectGenerator(flagParam flagParameter, scope string, srvConfig serviceConfig) {
if flagParam.addHandler {
if flagParam.addHandler || scope == AddUsecase {
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/candi/project_generator_add_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func scopeAddHandler(flagParam *flagParameter, cfg serviceConfig, serverHandlers
root := FileStructure{
Skip: true, Childs: []FileStructure{
apiStructure, internalServiceStructure, {
Source: modCfg.toJSONString(), FileName: "candi.json",
Source: cfg.toJSONString(), FileName: "candi.json",
},
},
}
Expand Down
33 changes: 33 additions & 0 deletions cmd/candi/project_generator_add_usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"log"
"strings"

"github.com/golangid/candi/candihelper"
)

func addUsecase(flagParam *flagParameter, usecaseName string) {
targetDir := ""
if isWorkdirMonorepo() {
targetDir = flagParam.outputFlag + flagParam.serviceName + "/"
}

fs := FileStructure{
FromTemplate: true,
DataSource: map[string]any{"ModuleName": flagParam.moduleName, "UsecaseName": usecaseName},
Source: templateNewUsecase,
FileName: candihelper.ToDelimited(usecaseName, '_') + ".go",
}
if err := fs.writeFile(targetDir + "internal/modules/" + flagParam.moduleName + "/usecase"); err != nil {
log.Fatal(err)
}

fu := fileUpdate{
filepath: targetDir + "internal/modules/" + flagParam.moduleName + "/usecase/usecase.go",
oldContent: `type ` + strings.Title(candihelper.ToCamelCase(flagParam.moduleName)) + `Usecase interface {`,
newContent: `type ` + strings.Title(candihelper.ToCamelCase(flagParam.moduleName)) + `Usecase interface {
` + strings.Title(candihelper.ToCamelCase(usecaseName)) + `(ctx context.Context) (err error)`,
}
fu.readFileAndApply()
}
16 changes: 16 additions & 0 deletions cmd/candi/template_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,5 +502,21 @@ func TestNew{{upper (camel .ModuleName)}}Usecase(t *testing.T) {
setFunc(nil)
assert.NotNil(t, uc)
}
`

templateNewUsecase = `package usecase
import (
"context"
"github.com/golangid/candi/tracer"
)
func (uc *{{camel .ModuleName}}UsecaseImpl) {{upper (camel .UsecaseName)}}(ctx context.Context) (err error) {
trace, ctx := tracer.StartTraceWithContext(ctx, "{{upper (camel .ModuleName)}}Usecase:{{upper (camel .UsecaseName)}}")
defer trace.Finish()
return
}
`
)
22 changes: 21 additions & 1 deletion cmd/candi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"time"
Expand All @@ -16,7 +17,7 @@ import (

var (
scopeMap = map[string]string{
"1": InitMonorepo, "2": InitService, "3": AddModule, "4": AddHandler,
"1": InitMonorepo, "2": InitService, "3": AddModule, "4": AddHandler, "5": AddUsecase,
}

dependencyMap = map[string]string{
Expand Down Expand Up @@ -204,3 +205,22 @@ type FileStructure struct {
SkipIfExist bool
Childs []FileStructure
}

func (f *FileStructure) parseTemplate() (buff []byte) {
if f.FromTemplate {
if f.Source != "" {
buff = loadTemplate(f.Source, f.DataSource)
} else {
lastDir := filepath.Dir(f.TargetDir)
buff = defaultDataSource(lastDir[strings.LastIndex(lastDir, "/")+1:])
}
} else {
buff = []byte(f.Source)
}
return
}

func (f *FileStructure) writeFile(targetPath string) error {
fmt.Printf("creating %s...\n", targetPath+"/"+f.FileName)
return os.WriteFile(targetPath+"/"+f.FileName, f.parseTemplate(), 0644)
}

0 comments on commit 0322cc4

Please sign in to comment.