Skip to content

Commit

Permalink
Merge pull request #1 from MrAzharuddin/feat-swagger
Browse files Browse the repository at this point in the history
Feat swagger
  • Loading branch information
azar-writes-code authored Apr 26, 2024
2 parents 8728f7d + d6701ca commit 2b58632
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
11 changes: 11 additions & 0 deletions internal/languages/golang/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package golang

import "os/exec"

// commandExists checks if a command exists in the system.
// cmd: the command to check.
// bool: true if the command exists, false otherwise.
func commandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
20 changes: 20 additions & 0 deletions internal/languages/golang/frameworks/go-gin-server/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const SQLServiceFile = "sqls-service.go.tmpl"
const MySQLDaoFile = "mysql-dao.go.tmpl"
const SQLModelFile = "sqls-model.go.tmpl"

const UtilsControllerFile = "common.go.tmpl"

const DaoFile = "dao.go.tmpl"
const SQLiteDaoFile = "sqlite-dao.go.tmpl"
const MySQLDBConfigFile = "mysql.go.tmpl"
Expand Down Expand Up @@ -472,6 +474,15 @@ func (c *Copier) copyNoSQLDBResourceFiles(resourceName string, filePaths []*stri
}
filePaths = append(filePaths, &targetResourceControllerFileName)

// copy controller common file to a generated project
targetResourceControllerUtilsFileName := c.NodeDirectoryName + ControllersPath + "/" + UtilsControllerFile
_, err = utils.CopyFile(targetResourceControllerUtilsFileName, c.TemplatesRootPath+ControllersPath+"/"+UtilsControllerFile)
if err != nil {
log.Debugf("error copying controller utils file: %v", err)
return nil, err
}
filePaths = append(filePaths, &targetResourceControllerUtilsFileName)

// copy model files to a generated project
targetResourceModelFileName := c.NodeDirectoryName + ModelsPath + "/" + resourceName + "-" + strings.Replace(NoSQLModelFile, "nosqls-", "", 1)
_, err = utils.CopyFile(targetResourceModelFileName, c.TemplatesRootPath+ModelsPath+"/"+NoSQLModelFile)
Expand Down Expand Up @@ -515,6 +526,15 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
}
filePaths = append(filePaths, &targetResourceControllerFileName)

// copy controller common file to a generated project
targetResourceControllerUtilsFileName := c.NodeDirectoryName + ControllersPath + "/" + UtilsControllerFile
_, err = utils.CopyFile(targetResourceControllerUtilsFileName, c.TemplatesRootPath+ControllersPath+"/"+UtilsControllerFile)
if err != nil {
log.Debugf("error copying controller utils file: %v", err)
return nil, err
}
filePaths = append(filePaths, &targetResourceControllerUtilsFileName)

// copy service files to a generated project
targetResourceServiceFileName := c.NodeDirectoryName + ServicesPath + "/" + resourceName + "-" + strings.Replace(SQLServiceFile, "sqls-", "", 1)
_, err = utils.CopyFile(targetResourceServiceFileName, c.TemplatesRootPath+ServicesPath+"/"+SQLServiceFile)
Expand Down
7 changes: 7 additions & 0 deletions internal/languages/golang/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func Generate(ctx context.Context) error {
return err
}

// RunSwag runs the swag command to generate the swagger documentation
err = RunSwag(goValues.Values.NodeDirectoryName)
if err != nil {
log.Errorf("err : %s", err)
return err
}

return nil
}

Expand Down
6 changes: 0 additions & 6 deletions internal/languages/golang/make-proto-runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,3 @@ func RunMakeProto(directoryName string) error {
}
return fmt.Errorf("%s command doesn't exist", protocCommand)
}

// as util
func commandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
38 changes: 38 additions & 0 deletions internal/languages/golang/swag-runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package golang

import (
"bytes"
"fmt"
"os/exec"

log "github.com/sirupsen/logrus"
)

// RunSwag runs swag with args passed on generated code present in the directory passed.
func RunSwag(directoryName string) error {
swagCommand := "swag"
if commandExists(swagCommand) {
args := []string{"init", "--parseDependency", "--parseInternal"}
command := exec.Command(swagCommand, args...)
command.Dir = directoryName
var stdErr bytes.Buffer
command.Stderr = &stdErr
var stdOut bytes.Buffer
command.Stdout = &stdOut
if err := command.Run(); err != nil {
log.Errorf("%s\n", err)
log.Errorf("%s\n", stdErr.String())
log.Errorf("%s\n", stdOut.String())
return err
}
if len(stdErr.String()) > 0 {
log.Errorf("%s\n", stdErr.String())
}
if len(stdOut.String()) > 0 {
log.Infof("%s\n", stdOut.String())
}
return nil
} else {
return fmt.Errorf("%s command doesn't exist. Please install swag using https://github.com/swaggo/swag?tab=readme-ov-file#getting-started.\nInstalled, and still facing issue? Please check https://github.com/swaggo/swag/issues/197", swagCommand)
}
}

0 comments on commit 2b58632

Please sign in to comment.