diff --git a/internal/languages/golang/common.go b/internal/languages/golang/common.go new file mode 100644 index 000000000..b8efa2292 --- /dev/null +++ b/internal/languages/golang/common.go @@ -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 +} diff --git a/internal/languages/golang/frameworks/go-gin-server/copier.go b/internal/languages/golang/frameworks/go-gin-server/copier.go index de9d3d567..4905fcc8a 100644 --- a/internal/languages/golang/frameworks/go-gin-server/copier.go +++ b/internal/languages/golang/frameworks/go-gin-server/copier.go @@ -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" @@ -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) @@ -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) diff --git a/internal/languages/golang/generator.go b/internal/languages/golang/generator.go index 4e90be1cc..df55517ea 100644 --- a/internal/languages/golang/generator.go +++ b/internal/languages/golang/generator.go @@ -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 } diff --git a/internal/languages/golang/make-proto-runner.go b/internal/languages/golang/make-proto-runner.go index d6b1e94e2..c28a4575f 100644 --- a/internal/languages/golang/make-proto-runner.go +++ b/internal/languages/golang/make-proto-runner.go @@ -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 -} diff --git a/internal/languages/golang/swag-runner.go b/internal/languages/golang/swag-runner.go new file mode 100644 index 000000000..d1f20e7c8 --- /dev/null +++ b/internal/languages/golang/swag-runner.go @@ -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) + } +}