Skip to content

Commit

Permalink
fix path join & update migration template
Browse files Browse the repository at this point in the history
  • Loading branch information
agungdwiprasetyo committed Jul 22, 2024
1 parent ec025bb commit 4431494
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
18 changes: 9 additions & 9 deletions cmd/candi/project_generator_add_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func addUsecase(flagParam *flagParameter, usecaseName string, deliveryHandlers [
targetDir := ""
packagePrefix := flagParam.serviceName
if isWorkdirMonorepo() {
targetDir = filepath.Join(flagParam.outputFlag, flagParam.serviceName) + "/"
targetDir = filepath.Join(flagParam.outputFlag, flagParam.serviceName)
packagePrefix = filepath.Join(flagParam.packagePrefixFlag, flagParam.serviceName)
}
moduleDir := targetDir + "internal/modules/" + flagParam.moduleName
moduleDir := filepath.Join(targetDir, "internal", "modules", flagParam.moduleName)

newUsecase := FileStructure{
FromTemplate: true, DataSource: map[string]any{
Expand All @@ -26,15 +26,15 @@ func addUsecase(flagParam *flagParameter, usecaseName string, deliveryHandlers [
Source: templateNewUsecase,
FileName: candihelper.ToDelimited(usecaseName, '_') + ".go",
}
if err := newUsecase.writeFile(moduleDir + "/usecase"); err != nil {
if err := newUsecase.writeFile(filepath.Join(moduleDir, "usecase")); err != nil {
log.Fatal(err)
}

modName := strings.Title(candihelper.ToCamelCase(flagParam.moduleName))
ucName := strings.Title(candihelper.ToCamelCase(usecaseName))
replaceFiles := []fileUpdate{
{
filepath: moduleDir + "/usecase/usecase.go",
filepath: filepath.Join(moduleDir, "usecase", "usecase.go"),
oldContent: `type ` + modName + `Usecase interface {`,
newContent: `type ` + modName + `Usecase interface {
` + ucName + `(ctx context.Context, req *domain.Request` + ucName + `) (resp domain.Response` + ucName + `, err error)`,
Expand All @@ -43,7 +43,7 @@ func addUsecase(flagParam *flagParameter, usecaseName string, deliveryHandlers [

var fileUpdateContent []fileUpdate
for _, dType := range []string{"Request", "Response"} {
fileLoc := moduleDir + "/domain/" + strings.ToLower(dType) + ".go"
fileLoc := filepath.Join(moduleDir, "domain", strings.ToLower(dType)+".go")
if _, err := os.Stat(fileLoc); os.IsNotExist(err) {
os.WriteFile(fileLoc, []byte("package domain\n\n"), 0644)
}
Expand Down Expand Up @@ -82,10 +82,10 @@ func getUpdateFileInExistingDelivery(flagParam *flagParameter, usecaseName strin
targetDir := ""
packagePrefix := flagParam.serviceName
if isWorkdirMonorepo() {
targetDir = filepath.Join(flagParam.outputFlag, flagParam.serviceName) + "/"
targetDir = filepath.Join(flagParam.outputFlag, flagParam.serviceName)
packagePrefix = filepath.Join(flagParam.packagePrefixFlag, flagParam.serviceName)
}
moduleDir := targetDir + "internal/modules/" + flagParam.moduleName
moduleDir := filepath.Join(targetDir, "internal", "modules", flagParam.moduleName)
modName := strings.Title(candihelper.ToCamelCase(flagParam.moduleName))
ucName := strings.Title(candihelper.ToCamelCase(usecaseName))

Expand Down Expand Up @@ -117,7 +117,7 @@ func getUpdateFileInExistingDelivery(flagParam *flagParameter, usecaseName strin
case GrpcHandler:
fu.newContent = getGRPCFuncTemplate(modName, ucName)
fu.skipContains = "func (h *GRPCHandler) " + ucName
protoSource := targetDir + "api/proto/" + flagParam.moduleName + "/" + flagParam.moduleName + ".proto"
protoSource := filepath.Join(targetDir, "api", "proto", flagParam.moduleName, flagParam.moduleName+".proto")
replaceFiles = append(replaceFiles, []fileUpdate{
{filepath: protoSource,
oldContent: `service ` + modName + `Handler {`,
Expand All @@ -138,7 +138,7 @@ message Response` + ucName + ` {
case GraphqlHandler:
fu.newContent = getGraphQLFuncTemplate(modName, ucName)
fu.skipContains = "func (m *GraphQLHandler) " + ucName
gqlSchemaSource := targetDir + "api/graphql/" + flagParam.moduleName + ".graphql"
gqlSchemaSource := filepath.Join(targetDir, "api", "graphql", flagParam.moduleName+".graphql")
replaceFiles = append(replaceFiles, []fileUpdate{
{filepath: gqlSchemaSource, oldContent: `type ` + modName + `QueryResolver {`,
newContent: `type ` + modName + `QueryResolver {
Expand Down
10 changes: 7 additions & 3 deletions cmd/candi/template_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ package main
import (
"context"
"embed"
"flag"
"log"
"os"{{if eq .SQLDriver "mysql"}}
Expand All @@ -57,6 +58,9 @@ import (
"gorm.io/gorm"
)
//go:embed migrations/*.sql
var migrationSource embed.FS
var (
flags = flag.NewFlagSet("goose", flag.ExitOnError)
)
Expand Down Expand Up @@ -88,14 +92,14 @@ func main() {
}
goose.SetDialect("{{.SQLDriver}}")
dir := os.Getenv("WORKDIR") + "cmd/migration/migrations"
goose.SetBaseFS(migrationSource)
switch args[0] {
case "create":
migrationType := "sql"
if len(args) > 2 {
migrationType = args[2]
}
if err := goose.Create(db, dir, args[1], migrationType); err != nil {
if err := goose.Create(db, "migrations", args[1], migrationType); err != nil {
log.Fatalf("goose %v: %v", args[1], err)
}
Expand All @@ -109,7 +113,7 @@ func main() {
tx.Commit()
}
if err := goose.RunWithOptionsContext(ctx, args[0], db, dir, arguments, goose.WithAllowMissing()); err != nil {
if err := goose.RunWithOptionsContext(ctx, args[0], db, "migrations", arguments, goose.WithAllowMissing()); err != nil {
log.Fatalf("goose %v: %v", args[0], err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/candi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,6 @@ func (f *FileStructure) parseTemplate() (buff []byte) {
}

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

0 comments on commit 4431494

Please sign in to comment.