Skip to content

Commit

Permalink
v1.0.3 getDir
Browse files Browse the repository at this point in the history
  • Loading branch information
dungda-0794 committed May 29, 2023
1 parent 031b7f9 commit d3aefb8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ GLOBAL OPTIONS:
go install github.com/dung13890/go-base-gen@latest
```

## Using
## Usage
- Init Project base on clean architecture
```bash
## Short
Expand All @@ -53,6 +53,31 @@ go-base-gen domain -n <domain-name> -pj <project-name> -m <module-name>

## Long
go-base-gen domain --name <domain-name> --project <project-name> --module <module-name> --path <project-path>
```
- Example usage
```bash
# Genenrate project-demo
go-base-gen project -n project-demo -p ~/go/src

# cd to project-demo
cd project-demo/

# download dependencies
go mod tidy

# create env file
cp .env.example .env

# setup database
go run cmd/migrate/main.go
go run cmd/seed/main.go

# create domain product in module ecommerce
go-base-gen domain -n product -pj project-demo -m ecommerce -p ~/go/src

# Run project for development
make dev

```

## Structure project after generate
Expand Down
47 changes: 34 additions & 13 deletions cmd/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
)

var (
errProjectNotExists = errors.New("Project is not exists!")
errProjectNotExists = errors.New("Project is not exists!. Please create project first!")
errNameDomainInvalid = errors.New("Name of domain is invalid!")
errProjectInvalid = errors.New("Name of project is invalid!")
errModuleInvalid = errors.New("Name of module is invalid!")
Expand All @@ -42,10 +42,11 @@ var (
)

type domain struct {
Domain string
Project string
Module string
Path string
Domain string
Project string
Module string
Path string
ForcePath bool
}

// NewDomain is a function to create new domain in project
Expand Down Expand Up @@ -78,6 +79,11 @@ func NewDomain() *cli.Command {
Usage: "Path is a path to generate for domain will stay in project path",
DefaultText: "./",
},
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Force path is a flag to force path to generate for domain will stay in path",
},
},
Action: func(ctx *cli.Context) error {
dir, err := os.Getwd()
Expand All @@ -98,10 +104,11 @@ func NewDomain() *cli.Command {
}

d := &domain{
Domain: strings.ToLower(ctx.String("name")),
Project: strings.ToLower(ctx.String("project")),
Module: strings.ToLower(ctx.String("module")),
Path: rltDir,
Domain: strings.ToLower(ctx.String("name")),
Project: strings.ToLower(ctx.String("project")),
Module: strings.ToLower(ctx.String("module")),
Path: rltDir,
ForcePath: ctx.Bool("force"),
}
if err := d.checkProject(ctx.Context); err != nil {
return err
Expand All @@ -123,6 +130,7 @@ func NewDomain() *cli.Command {
}
}

// domainValidate is a function to validate input
func domainValidate(ctx *cli.Context) error {
// Validate
if ok := utils.ValidateDash(ctx.String("name")); !ok {
Expand All @@ -138,11 +146,24 @@ func domainValidate(ctx *cli.Context) error {
return nil
}

// getDir will check path is contain project or not and return path include project
func getDir(path, project string, forcePath bool) string {
if forcePath {
return path
}
if ok := strings.HasSuffix(path, project); ok {
return path
}

return filepath.Join(path, project)
}

// checkProject is a function to check project exist or not
func (d *domain) checkProject(context.Context) error {
dir := filepath.Join(d.Path, d.Project)
dir := getDir(d.Path, d.Project, d.ForcePath)
modDir := filepath.Join(dir, "go.mod")
// Check project exist or not
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
if _, err := os.Stat(modDir); errors.Is(err, os.ErrNotExist) {
return errProjectNotExists
}

Expand All @@ -164,7 +185,7 @@ func (*domain) destroy(context.Context) error {

// generateStruct is a function to generate struct for domain
func (d *domain) generateStruct(context.Context) error {
dir := filepath.Join(d.Path, d.Project)
dir := getDir(d.Path, d.Project, d.ForcePath)
// Generate struct
for _, s := range dStructs {
target := strings.Replace(s, ":name", d.Module, 1)
Expand All @@ -182,7 +203,7 @@ func (d *domain) generateStruct(context.Context) error {

// generateFile is a function to generate file for domain
func (d *domain) generateFile(_ context.Context) error {
dir := filepath.Join(d.Path, d.Project)
dir := getDir(d.Path, d.Project, d.ForcePath)
tmpl, err := template.NewTemplate("tmpl", []string{
"tmpl/*/*.tmpl",
"tmpl/*/*/*.tmpl",
Expand Down
1 change: 1 addition & 0 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func NewProject() *cli.Command {
}
}

// projectValidate is a function to validate project name
func projectValidate(ctx *cli.Context) error {
if ok := utils.ValidateDash(ctx.String("name")); !ok {
return errNameProjectInvalid
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
version string = "v1.0.2"
version string = "v1.0.3"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion template/tmpl/cmd/app/air.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "cmd/app/air.toml"}}root = "/var/www/app"
{{define "cmd/app/air.toml"}}root = "/var/www/app/{{.Project}}"
tmp_dir = "./tmp"

[build]
Expand Down

0 comments on commit d3aefb8

Please sign in to comment.