2023-11-15 10:43:15 +02:00
|
|
|
package main_file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/config"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/constants"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/create_files"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/folders"
|
|
|
|
"github.com/ManyakRus/starter/log"
|
|
|
|
"github.com/ManyakRus/starter/micro"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateAllFiles - создаёт все файлы в папке grpc proto
|
|
|
|
func CreateAllFiles() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = CreateFileMain()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("CreateFileMain() error: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFileMain - создаёт 1 файл в папке grpc
|
|
|
|
func CreateFileMain() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirTemplatesMain := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_MAIN + micro.SeparatorFile()
|
|
|
|
DirReadyMain := DirReady + config.Settings.TEMPLATE_FOLDERNAME_MAIN + micro.SeparatorFile() + config.Settings.SERVICE_NAME + micro.SeparatorFile()
|
|
|
|
FilenameReadyMain := DirReadyMain + "main.go"
|
|
|
|
FilenameTemplateMain := DirTemplatesMain + "main.go_"
|
|
|
|
|
|
|
|
bytes, err := os.ReadFile(FilenameTemplateMain)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateMain, " error: ", err)
|
|
|
|
}
|
|
|
|
TextMain := string(bytes)
|
|
|
|
|
|
|
|
//создадим папку ready
|
|
|
|
folders.CreateFolder(DirReadyMain)
|
|
|
|
|
2023-11-20 16:08:58 +02:00
|
|
|
//
|
2023-11-15 10:43:15 +02:00
|
|
|
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
2023-11-20 16:08:58 +02:00
|
|
|
TextMain = create_files.DeleteTemplateRepositoryImports(TextMain)
|
|
|
|
|
2023-11-15 10:43:15 +02:00
|
|
|
//GRPC
|
|
|
|
if config.Settings.NEED_CREATE_GRPC == true {
|
2023-11-17 13:24:26 +02:00
|
|
|
TextImport := create_files.FindGRPCServerURL()
|
2023-11-15 10:43:15 +02:00
|
|
|
TextMain = create_files.AddImport(TextMain, TextImport)
|
|
|
|
} else {
|
|
|
|
TextMain = strings.ReplaceAll(TextMain, "\n\tserver_grpc.Start()", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
//NRPC
|
|
|
|
if config.Settings.NEED_CREATE_NRPC == true {
|
|
|
|
TextImport := create_files.FindNRPCServerlURL()
|
|
|
|
TextMain = create_files.AddImport(TextMain, TextImport)
|
|
|
|
} else {
|
|
|
|
TextMain = strings.ReplaceAll(TextMain, "\n\tserver_nrpc.Start()", "")
|
|
|
|
}
|
2023-11-20 16:08:58 +02:00
|
|
|
|
|
|
|
URLCrudStarter := create_files.FindCrudStarterURL()
|
|
|
|
TextMain = create_files.AddImport(TextMain, URLCrudStarter)
|
2023-11-15 10:43:15 +02:00
|
|
|
}
|
|
|
|
|
2023-11-15 16:59:21 +02:00
|
|
|
//замена импортов на новые URL
|
|
|
|
TextMain = create_files.ReplaceServiceURLImports(TextMain)
|
|
|
|
|
2023-11-15 10:43:15 +02:00
|
|
|
//запись файла
|
|
|
|
err = os.WriteFile(FilenameReadyMain, []byte(TextMain), constants.FILE_PERMISSIONS)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|