Skip to content

Commit fec9d61

Browse files
Merge pull request #2 from fazpass/create-module/refactor-code
refactor create module
2 parents 1e1a8f9 + 1e6e8ee commit fec9d61

File tree

18 files changed

+386
-261
lines changed

18 files changed

+386
-261
lines changed

.env

Lines changed: 0 additions & 2 deletions
This file was deleted.

main.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,54 @@
11
package main
22

33
import (
4+
"flag"
5+
"fmt"
46
"log"
57
"os"
68

7-
"github.com/aryadiahmad4689/create-module/project"
9+
"github.com/aryadiahmad4689/create-module/src/data"
10+
mains "github.com/aryadiahmad4689/create-module/src/main"
11+
812
"github.com/joho/godotenv"
913
_ "github.com/joho/godotenv/autoload"
1014
)
1115

1216
func main() {
13-
args := os.Args[1:]
14-
if len(args) == 0 {
15-
log.Fatal("Please define your name module")
17+
var moduleName = flag.String("module_name", "", "Ini adalah flag module name exmaples: -module_name=test")
18+
var rootDir = flag.String("root_dir", "", "Ini adalah flag root directory exmaples: -root_dir=src/halo")
19+
var moduleInit = flag.String("module_init", "", "Ini adalah flag module init exmaples: -module_init=datz/common")
20+
flag.Parse()
21+
22+
if *moduleName == "" {
23+
fmt.Println("module name is empty")
24+
os.Exit(1)
1625
}
17-
err := project.Create(args[0])
26+
var modInit string
27+
var rootDirectory string
28+
if *moduleInit == "" {
29+
modInit = os.Getenv("MODULE_INIT")
30+
} else {
31+
modInit = *moduleInit
32+
}
33+
34+
if *rootDir == "" {
35+
rootDirectory = os.Getenv("ROOT_DIR")
36+
} else {
37+
rootDirectory = *rootDir
38+
}
39+
40+
var err = mains.InitMain().Create(data.MainData{
41+
ModuleName: *moduleName,
42+
RootDir: rootDirectory,
43+
ProjectDirectory: data.ProjectDirectory,
44+
ModInit: modInit,
45+
})
1846
if err != nil {
1947
log.Fatal(err.Error())
2048
}
2149
err = godotenv.Load()
2250
if err != nil {
2351
log.Fatal("Error loading .env file")
2452
}
53+
2554
}

project/project.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

readme.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
### Create module in fazpass
22

33
### how to install
4-
- go get github.com/fazpass/create-module@v1.0.5
4+
- go get github.com/fazpass/create-module@v2.0.0
55

66
### env
7-
- must to have in .env MODULE_INIT, ROOT_DIR
7+
- jika tidak menggunakan flag bisa di simpan di .env
88
- MODULE_INIT=citcall.com/datz -> example
99
- ROOT_DIR=src/modules/ ->example
1010

11+
### flag yang tersedia
12+
- [-module_name=]
13+
- [-root_dir=]
14+
- [-module_init=]
15+
- penjelasan lebih lengkap di create-module -help
16+
1117
### how to use
12-
- create-module nama_module
18+
- create-module -module_name=test

src/data/data.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package data
2+
3+
type Data struct {
4+
ModuleName string
5+
ModInit string
6+
Template string
7+
FileName string
8+
}
9+
10+
type MainData struct {
11+
ModuleName string
12+
ProjectDirectory []string
13+
RootDir string
14+
ModInit string
15+
}
16+
17+
// ini di gunakan untuk menggenerate template
18+
type DataGenerate struct {
19+
ModuleName string
20+
DirectoryFile string
21+
RootDir string
22+
ModuleInit string
23+
Template string
24+
FileName string
25+
}
26+
27+
var (
28+
ProjectDirectory = []string{"repository", "usecase", "endpoint", "transport/http/handler"}
29+
30+
TemplateUsecase = `package usecase
31+
32+
import (
33+
"{{.ModInit}}/src/app"
34+
"{{.ModInit}}/src/modules/{{.ModuleName}}/repository"
35+
)
36+
37+
type (
38+
UseCaseInterface interface {
39+
40+
}
41+
42+
UseCase struct {
43+
app *app.App
44+
repo *repository.Repository
45+
}
46+
)
47+
48+
func Init(app *app.App, repo *repository.Repository) UseCaseInterface {
49+
return &UseCase{
50+
app: app,
51+
repo: repo,
52+
}
53+
}
54+
`
55+
TemplateEndPoint = `package endpoint
56+
57+
import (
58+
59+
"{{.ModInit}}/src/app"
60+
"{{.ModInit}}/src/modules/{{.ModuleName}}/usecase"
61+
)
62+
63+
type (
64+
EndpointInterface interface {
65+
}
66+
Endpoint struct {
67+
app *app.App
68+
usecase usecase.UseCaseInterface
69+
}
70+
)
71+
72+
func Init(app *app.App, usecase usecase.UseCaseInterface) EndpointInterface {
73+
return &Endpoint{
74+
app: app,
75+
usecase: usecase,
76+
}
77+
}
78+
`
79+
TemplateRepository = `package repository
80+
81+
import (
82+
"{{.ModInit}}/src/app"
83+
)
84+
85+
type (
86+
Repository struct {
87+
}
88+
)
89+
90+
func Init(app *app.App) *Repository {
91+
return &Repository{
92+
}
93+
}
94+
`
95+
TemplateModule = `package module
96+
97+
import (
98+
"{{.ModInit}}/src/app"
99+
modulebase "{{.ModInit}}/src/app/module"
100+
"{{.ModInit}}/src/modules/{{.ModuleName}}/endpoint"
101+
"{{.ModInit}}/src/modules/{{.ModuleName}}/repository"
102+
transporthttp "{{.ModInit}}/src/modules/{{.ModuleName}}/transport/http"
103+
"{{.ModInit}}/src/modules/{{.ModuleName}}/usecase"
104+
"github.com/go-chi/chi"
105+
)
106+
107+
type Module struct {
108+
usecase usecase.UseCaseInterface
109+
endpoint endpoint.EndpointInterface
110+
repository *repository.Repository
111+
httpRouter *chi.Mux
112+
}
113+
114+
func Init(app *app.App) modulebase.ModuleInterface {
115+
var (
116+
repository = repository.Init(app)
117+
usecase = usecase.Init(app, repository)
118+
endpoint = endpoint.Init(app, usecase)
119+
httpRouter = transporthttp.Init(app, endpoint)
120+
)
121+
122+
return &Module{
123+
repository: repository,
124+
usecase: usecase,
125+
endpoint: endpoint,
126+
httpRouter: httpRouter,
127+
}
128+
}
129+
130+
func (module *Module) GetHttpRouter() *chi.Mux {
131+
return module.httpRouter
132+
}
133+
`
134+
TemplateTransport = `package handler
135+
136+
import (
137+
"{{.ModInit}}/src/app"
138+
"{{.ModInit}}/src/modules/{{.ModuleName}}/endpoint"
139+
)
140+
141+
type Handler struct {
142+
app *app.App
143+
endpoint endpoint.EndpointInterface
144+
}
145+
146+
func InitHandler(app *app.App, endpoint endpoint.EndpointInterface) *Handler {
147+
var handler = &Handler{
148+
app: app,
149+
endpoint: endpoint,
150+
}
151+
return handler
152+
}
153+
`
154+
TemplateRoute = `package http
155+
156+
import (
157+
"fmt"
158+
"{{.ModInit}}/src/app"
159+
"{{.ModInit}}/src/modules/{{.ModuleName}}/endpoint"
160+
"{{.ModInit}}/src/modules/{{.ModuleName}}/transport/http/handler"
161+
"github.com/go-chi/chi"
162+
163+
)
164+
165+
func Init(app *app.App, endpoint endpoint.EndpointInterface) *chi.Mux {
166+
var (
167+
router = chi.NewRouter()
168+
h = handler.InitHandler(app, endpoint)
169+
)
170+
171+
fmt.Print(h)
172+
173+
return router
174+
}
175+
`
176+
)

0 commit comments

Comments
 (0)