Skip to content

Commit

Permalink
file re-structure and few test case added for util.go and common.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mrasif committed Dec 9, 2024
1 parent 1fbf8f6 commit 82d920b
Show file tree
Hide file tree
Showing 17 changed files with 724 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Go Tests

on:
push:
branches: [ 'main', 'develop' ]
branches: [ '*', 'develop' ]
pull_request:
branches: [ '*' ]

Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ dist:
clean:
rm -rf gozen*
rm -rf build

generate-mocks:
# Mockery version v2.50.0
@mockery --name=ShellRepo --dir=cmd/repository --output=cmd/repository --outpkg=repository --filename=shell_repo_mock.go --structname=ShellRepoMock
@mockery --name=FileSystemRepo --dir=cmd/repository --output=cmd/repository --outpkg=repository --filename=file_system_repo_mock.go --structname=FileSystemRepoMock
76 changes: 76 additions & 0 deletions cmd/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cmd

import (
"fmt"

"github.com/tech-thinker/gozen/cmd/service"
"github.com/tech-thinker/gozen/models"
"github.com/urfave/cli/v2"
)

type App interface {
CreateProject() *cli.Command
}

type app struct {
appService service.AppService
}

func (c *app) CreateProject() *cli.Command {

var packageName, outputDir, driver string

return &cli.Command{
Name: "create",
Usage: "Create new Projects.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pkg",
Aliases: []string{"p"},
Value: "",
Usage: "Package name for new project.",
Destination: &packageName,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Value: ".",
Usage: "Output directory for new project.",
Destination: &outputDir,
},
&cli.StringFlag{
Name: "driver",
Aliases: []string{"d"},
Value: "sqlite",
Usage: "Database driver for new project. eg. [sqlite, mysql, postgres]",
Destination: &driver,
},
},
Action: func(ctx *cli.Context) error {
project := models.Project{
AppName: ctx.Args().Get(0),
PackageName: packageName,
Driver: driver,
WorkingDir: outputDir,
}

err := project.Validate()
if err != nil {
fmt.Println(err)
return nil
}

project.AutoFixes()

return c.appService.CreateApp(project)
},
}
}

func NewApp(
appService service.AppService,
) App {
return &app{
appService: appService,
}
}
27 changes: 17 additions & 10 deletions cmd/helper/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package helper
import (
"embed"
"fmt"
"os/exec"
"path/filepath"
"strings"

"github.com/tech-thinker/gozen/cmd/repository"
"github.com/tech-thinker/gozen/utils"
)

Expand All @@ -17,28 +17,30 @@ type CommonHelper interface {
}

type commonHelper struct {
templatesFS embed.FS
templatesFS embed.FS
shellRepo repository.ShellRepo
fileSystemRepo repository.FileSystemRepo
}

// Write: generate code and write to file
func (helper *commonHelper) Write(templatePath string, outputPath string, data interface{}) error {
baseDir := filepath.Dir(outputPath)
err := utils.CreateDirectory(baseDir)
err := helper.fileSystemRepo.CreateDirectory(baseDir)
if err != nil {
return err
}
tpl, err := utils.GenerateCode(helper.templatesFS, templatePath, data)
if err != nil {
return err
}
return utils.WriteFile(outputPath, tpl)
tpl = utils.ApplyEscapeChar(tpl)
return helper.fileSystemRepo.WriteFile(outputPath, tpl)
}

// ExecShell: execute shell command and return output as string slice
func (helper *commonHelper) ExecShell(command string, args ...string) ([]string, error) {
fmt.Printf(`%s %+v\n`, command, args)
cmd := exec.Command(command, args...)
output, err := cmd.Output()
output, err := helper.shellRepo.Exec(command, args...)
if err != nil {
fmt.Println("Error executing command:", err)
return nil, err
Expand All @@ -50,8 +52,7 @@ func (helper *commonHelper) ExecShell(command string, args ...string) ([]string,
// ExecShellRaw: execute shell command and return output as byte array
func (helper *commonHelper) ExecShellRaw(command string, args ...string) ([]byte, error) {
fmt.Printf(`%s %+v\n`, command, args)
cmd := exec.Command(command, args...)
output, err := cmd.Output()
output, err := helper.shellRepo.Exec(command, args...)
if err != nil {
fmt.Println("Error executing command:", err)
return nil, err
Expand All @@ -60,8 +61,14 @@ func (helper *commonHelper) ExecShellRaw(command string, args ...string) ([]byte
}

// NewCommonHelper returns a new CommonHelper
func NewCommonHelper(tpl embed.FS) CommonHelper {
func NewCommonHelper(
tpl embed.FS,
shellRepo repository.ShellRepo,
fileSystemRepo repository.FileSystemRepo,
) CommonHelper {
return &commonHelper{
templatesFS: tpl,
templatesFS: tpl,
shellRepo: shellRepo,
fileSystemRepo: fileSystemRepo,
}
}
Loading

0 comments on commit 82d920b

Please sign in to comment.