github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/pkg/ctrl/main.go (about)

     1  package ctrl
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/easysoft/zendata/internal/pkg/helper"
     7  	"github.com/easysoft/zendata/internal/pkg/service"
     8  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
     9  	"github.com/easysoft/zendata/pkg/utils/vari"
    10  )
    11  
    12  type MainCtrl struct {
    13  	MainService       *service.MainService       `inject:""`
    14  	FileService       *service.FileService       `inject:""`
    15  	TableParseService *service.TableParseService `inject:""`
    16  	MockService       *service.MockService       `inject:""`
    17  	ArticleService    *service.ArticleService    `inject:""`
    18  
    19  	SqlParseService *service.SqlParseService `inject:""`
    20  }
    21  
    22  func (c *MainCtrl) Generate(files []string) {
    23  	if len(files) == 0 {
    24  		return
    25  	}
    26  
    27  	files = c.FileService.HandleFiles(files)
    28  
    29  	if !helper.IsFromProtobuf(files[0]) { // default gen from yaml
    30  		c.MainService.GenerateFromContents(files)
    31  
    32  	} else { // gen from protobuf
    33  		c.MainService.GenerateFromProtobuf(files)
    34  
    35  	}
    36  }
    37  
    38  func (c *MainCtrl) GenYaml(input string) {
    39  	if vari.GlobalVars.DBDsn != "" { // from db table
    40  		c.TableParseService.GenYamlFromTable()
    41  		return
    42  	}
    43  
    44  	ext := filepath.Ext(input)
    45  	if ext == ".sql" { // from sql
    46  		c.SqlParseService.GenYamlFromSql(input)
    47  	} else if ext == ".txt" { // from article
    48  		c.ArticleService.GenYamlFromArticle(input)
    49  	}
    50  }
    51  
    52  func (c *MainCtrl) GenMock(input string) {
    53  	if vari.GlobalVars.Output == "" {
    54  		vari.GlobalVars.Output = fileUtils.GetFileOrFolderDir(input)
    55  	}
    56  
    57  	c.MockService.GenMockDef(input)
    58  }