Skip to content

Commit 5f0b10c

Browse files
Merge pull request #217 from MrAzharuddin/main
feat: added swagger feature to compage for go-gin-server framework
2 parents 8728f7d + 46f0bfb commit 5f0b10c

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package golang
2+
3+
import "os/exec"
4+
5+
// commandExists checks if a command exists in the system.
6+
// cmd: the command to check.
7+
// bool: true if the command exists, false otherwise.
8+
func commandExists(cmd string) bool {
9+
_, err := exec.LookPath(cmd)
10+
return err == nil
11+
}

internal/languages/golang/frameworks/go-gin-server/copier.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ const SQLServiceFile = "sqls-service.go.tmpl"
4242
const MySQLDaoFile = "mysql-dao.go.tmpl"
4343
const SQLModelFile = "sqls-model.go.tmpl"
4444

45+
// common file for common utility functions in controllers
46+
const ControllersCommonFile = "common.go.tmpl"
47+
4548
const DaoFile = "dao.go.tmpl"
4649
const SQLiteDaoFile = "sqlite-dao.go.tmpl"
4750
const MySQLDBConfigFile = "mysql.go.tmpl"
@@ -472,6 +475,15 @@ func (c *Copier) copyNoSQLDBResourceFiles(resourceName string, filePaths []*stri
472475
}
473476
filePaths = append(filePaths, &targetResourceControllerFileName)
474477

478+
// copy controller common file to a generated project
479+
targetResourceControllerCommonFileName := c.NodeDirectoryName + ControllersPath + "/" + ControllersCommonFile
480+
_, err = utils.CopyFile(targetResourceControllerCommonFileName, c.TemplatesRootPath+ControllersPath+"/"+ControllersCommonFile)
481+
if err != nil {
482+
log.Debugf("error copying controller utils file: %v", err)
483+
return nil, err
484+
}
485+
filePaths = append(filePaths, &targetResourceControllerCommonFileName)
486+
475487
// copy model files to a generated project
476488
targetResourceModelFileName := c.NodeDirectoryName + ModelsPath + "/" + resourceName + "-" + strings.Replace(NoSQLModelFile, "nosqls-", "", 1)
477489
_, err = utils.CopyFile(targetResourceModelFileName, c.TemplatesRootPath+ModelsPath+"/"+NoSQLModelFile)
@@ -515,6 +527,15 @@ func (c *Copier) copySQLDBResourceFiles(resourceName string, filePaths []*string
515527
}
516528
filePaths = append(filePaths, &targetResourceControllerFileName)
517529

530+
// copy controller common file to a generated project
531+
targetResourceControllerCommonFileName := c.NodeDirectoryName + ControllersPath + "/" + ControllersCommonFile
532+
_, err = utils.CopyFile(targetResourceControllerCommonFileName, c.TemplatesRootPath+ControllersPath+"/"+ControllersCommonFile)
533+
if err != nil {
534+
log.Debugf("error copying controller utils file: %v", err)
535+
return nil, err
536+
}
537+
filePaths = append(filePaths, &targetResourceControllerCommonFileName)
538+
518539
// copy service files to a generated project
519540
targetResourceServiceFileName := c.NodeDirectoryName + ServicesPath + "/" + resourceName + "-" + strings.Replace(SQLServiceFile, "sqls-", "", 1)
520541
_, err = utils.CopyFile(targetResourceServiceFileName, c.TemplatesRootPath+ServicesPath+"/"+SQLServiceFile)

internal/languages/golang/generator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ func Generate(ctx context.Context) error {
6060
return err
6161
}
6262

63+
// RunSwag runs the swag command to generate the swagger documentation
64+
err = RunSwag(goValues.Values.NodeDirectoryName)
65+
if err != nil {
66+
log.Errorf("err : %s", err)
67+
return err
68+
}
69+
6370
return nil
6471
}
6572

internal/languages/golang/make-proto-runner.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,3 @@ func RunMakeProto(directoryName string) error {
4040
}
4141
return fmt.Errorf("%s command doesn't exist", protocCommand)
4242
}
43-
44-
// as util
45-
func commandExists(cmd string) bool {
46-
_, err := exec.LookPath(cmd)
47-
return err == nil
48-
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package golang
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os/exec"
7+
8+
log "github.com/sirupsen/logrus"
9+
)
10+
11+
// RunSwag runs swag with args passed on generated code present in the directory passed.
12+
func RunSwag(directoryName string) error {
13+
swagCommand := "swag"
14+
if commandExists(swagCommand) {
15+
args := []string{"init", "--parseDependency", "--parseInternal"}
16+
command := exec.Command(swagCommand, args...)
17+
command.Dir = directoryName
18+
var stdErr bytes.Buffer
19+
command.Stderr = &stdErr
20+
var stdOut bytes.Buffer
21+
command.Stdout = &stdOut
22+
if err := command.Run(); err != nil {
23+
log.Errorf("%s\n", err)
24+
log.Errorf("%s\n", stdErr.String())
25+
log.Errorf("%s\n", stdOut.String())
26+
return err
27+
}
28+
if len(stdErr.String()) > 0 {
29+
log.Errorf("%s\n", stdErr.String())
30+
}
31+
if len(stdOut.String()) > 0 {
32+
log.Infof("%s\n", stdOut.String())
33+
}
34+
return nil
35+
} else {
36+
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)
37+
}
38+
}

0 commit comments

Comments
 (0)