1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-12-22 00:36:41 +02:00
crud_generator/internal/config/config.go

239 lines
8.9 KiB
Go
Raw Normal View History

2023-09-28 16:45:44 +02:00
package config
import (
2023-11-17 14:08:31 +02:00
"github.com/ManyakRus/crud_generator/internal/constants"
2023-12-27 13:28:11 +02:00
ConfigMain "github.com/ManyakRus/starter/config_main"
2024-01-17 09:38:22 +02:00
"github.com/ManyakRus/starter/log"
2023-11-17 14:08:31 +02:00
"github.com/ManyakRus/starter/micro"
2023-09-28 16:45:44 +02:00
"os"
2023-10-06 15:48:58 +02:00
"strings"
2023-09-28 16:45:44 +02:00
)
// Settings хранит все нужные переменные окружения
var Settings SettingsINI
// SettingsINI - структура для хранения всех нужных переменных окружения
type SettingsINI struct {
2023-10-26 16:03:50 +02:00
INCLUDE_TABLES string
EXCLUDE_TABLES string
TEMPLATE_FOLDERNAME string
TEMPLATE_FOLDERNAME_MODEL string
2023-11-17 13:24:26 +02:00
TEMPLATE_FOLDERNAME_DB string
2023-11-16 14:08:10 +02:00
TEMPLATE_FOLDERNAME_CRUD string
TEMPLATE_FOLDERNAME_TABLES string
2023-11-15 16:59:21 +02:00
TEMPLATE_FOLDERNAME_GRPC string
2023-11-15 12:58:31 +02:00
TEMPLATE_FOLDERNAME_GRPC_PROTO string
2023-10-26 16:03:50 +02:00
TEMPLATE_FOLDERNAME_GRPC_SERVER string
TEMPLATE_FOLDERNAME_GRPC_CLIENT string
TEMPLATE_FOLDERNAME_NRPC string
TEMPLATE_FOLDERNAME_NRPC_SERVER string
TEMPLATE_FOLDERNAME_NRPC_CLIENT string
TEMPLATE_SERVICE_NAME string
TEMPLATE_FOLDERNAME_CRUD_STARTER string
TEMPLATE_FOLDERNAME_ALIAS string
TEMPLATE_FOLDER_CRUD_FUNCTIONS string
TEMPLATE_FOLDERNAME_MAIN string
TEMPLATE_REPOSITORY_URL string
TEMPLATE_EXTERNAL_PROTO_FILENAME string
2023-11-08 14:15:52 +02:00
NEED_CREATE_MODEL_STRUCT bool
NEED_CREATE_MODEL_CRUD bool
2023-11-14 16:07:41 +02:00
NEED_CREATE_DB bool
NEED_CREATE_DB_TEST bool
2024-01-17 09:41:15 +02:00
NEED_CREATE_DB_TABLES bool
2023-11-14 16:07:41 +02:00
NEED_CREATE_GRPC bool
NEED_CREATE_GRPC_SERVER_TEST bool
NEED_CREATE_GRPC_CLIENT_TEST bool
NEED_CREATE_NRPC bool
NEED_CREATE_NRPC_SERVER_TEST bool
NEED_CREATE_NRPC_CLIENT_TEST bool
2023-10-26 16:03:50 +02:00
SERVICE_NAME string
SERVICE_REPOSITORY_URL string
TEXT_TEMPLATE_MODEL string
TEXT_TEMPLATE_TABLENAME string
HAS_IS_DELETED bool
2023-11-01 16:34:57 +02:00
READY_FOLDERNAME string
2023-11-02 17:19:33 +02:00
TEXT_DB_MODIFIED_AT string
2023-12-25 16:15:24 +02:00
TEXT_DB_CREATED_AT string
2023-11-02 17:19:33 +02:00
TEXT_DB_IS_DELETED string
2023-11-07 16:58:02 +02:00
USE_DEFAULT_TEMPLATE bool
PREFIX_SERVER_GRPC string
2023-11-16 14:08:10 +02:00
PREFIX_CRUD string
PREFIX_MODEL string
2023-11-08 14:15:52 +02:00
COMMENT_MODEL_STRUCT string
2023-11-10 14:04:05 +02:00
TEXT_MODULE_GENERATED string
2023-11-16 14:08:10 +02:00
PREFIX_TABLE string
2023-09-28 16:45:44 +02:00
}
// FillSettings загружает переменные окружения в структуру из переменных окружения
func FillSettings() {
2023-11-01 16:34:57 +02:00
//dir := micro.ProgramDir_bin()
2023-09-28 16:45:44 +02:00
Settings = SettingsINI{}
Settings.INCLUDE_TABLES = os.Getenv("INCLUDE_TABLES")
Settings.EXCLUDE_TABLES = os.Getenv("EXCLUDE_TABLES")
2023-10-23 17:09:55 +02:00
Settings.TEMPLATE_FOLDERNAME = os.Getenv("TEMPLATE_FOLDERNAME")
Settings.TEMPLATE_FOLDERNAME_MODEL = os.Getenv("TEMPLATE_FOLDERNAME_MODEL")
2023-11-16 14:08:10 +02:00
Settings.TEMPLATE_FOLDERNAME_CRUD = os.Getenv("TEMPLATE_FOLDERNAME_CRUD")
2023-11-15 12:58:31 +02:00
Settings.TEMPLATE_FOLDERNAME_GRPC_PROTO = os.Getenv("TEMPLATE_FOLDERNAME_GRPC_PROTO")
2023-10-26 10:33:12 +02:00
Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER = os.Getenv("TEMPLATE_FOLDERNAME_GRPC_SERVER")
Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT = os.Getenv("TEMPLATE_FOLDERNAME_GRPC_CLIENT")
2023-10-23 17:09:55 +02:00
Settings.TEMPLATE_FOLDERNAME_NRPC = os.Getenv("TEMPLATE_FOLDERNAME_NRPC")
2023-10-26 10:33:12 +02:00
Settings.TEMPLATE_FOLDERNAME_NRPC_SERVER = os.Getenv("TEMPLATE_FOLDERNAME_NRPC_SERVER")
Settings.TEMPLATE_FOLDERNAME_NRPC_CLIENT = os.Getenv("TEMPLATE_FOLDERNAME_NRPC_CLIENT")
2023-10-24 17:03:04 +02:00
Settings.TEXT_TEMPLATE_MODEL = os.Getenv("TEXT_TEMPLATE_MODEL")
Settings.TEXT_TEMPLATE_TABLENAME = os.Getenv("TEXT_TEMPLATE_TABLENAME")
2023-10-26 13:18:38 +02:00
Settings.TEMPLATE_SERVICE_NAME = os.Getenv("TEMPLATE_SERVICE_NAME")
2023-10-26 16:03:50 +02:00
Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER = os.Getenv("TEMPLATE_FOLDERNAME_CRUD_STARTER")
2023-11-29 13:09:53 +02:00
SERVICE_REPOSITORY_URL := strings.ToLower(os.Getenv("SERVICE_REPOSITORY_URL"))
Settings.SERVICE_REPOSITORY_URL = SERVICE_REPOSITORY_URL
Settings.TEMPLATE_FOLDERNAME_ALIAS = os.Getenv("TEMPLATE_FOLDERNAME_ALIAS")
2023-11-07 16:58:02 +02:00
Settings.PREFIX_SERVER_GRPC = os.Getenv("PREFIX_SERVER_GRPC")
2023-10-26 16:03:50 +02:00
2023-10-25 13:52:49 +02:00
sHAS_IS_DELETED := os.Getenv("HAS_IS_DELETED")
2023-09-28 16:45:44 +02:00
2023-11-14 16:07:41 +02:00
sNEED_CREATE_DB := os.Getenv("NEED_CREATE_DB")
Settings.NEED_CREATE_DB = BoolFromString(sNEED_CREATE_DB)
2023-10-06 15:48:58 +02:00
2024-01-17 09:41:15 +02:00
sNEED_CREATE_DB_TABLES := os.Getenv("NEED_CREATE_DB_TABLES")
Settings.NEED_CREATE_DB_TABLES = BoolFromString(sNEED_CREATE_DB_TABLES)
sNEED_GRPC := os.Getenv("NEED_CREATE_GRPC")
Settings.NEED_CREATE_GRPC = BoolFromString(sNEED_GRPC)
2023-10-06 15:48:58 +02:00
sNEED_NRPC := os.Getenv("NEED_CREATE_NRPC")
Settings.NEED_CREATE_NRPC = BoolFromString(sNEED_NRPC)
2023-10-06 15:48:58 +02:00
2023-11-08 14:15:52 +02:00
sNEED_CREATE_MODEL_STRUCT := os.Getenv("NEED_CREATE_MODEL_STRUCT")
Settings.NEED_CREATE_MODEL_STRUCT = BoolFromString(sNEED_CREATE_MODEL_STRUCT)
sNEED_CREATE_MODEL_CRUD := os.Getenv("NEED_CREATE_MODEL_CRUD")
Settings.NEED_CREATE_MODEL_CRUD = BoolFromString(sNEED_CREATE_MODEL_CRUD)
2023-10-06 15:48:58 +02:00
Settings.SERVICE_NAME = os.Getenv("SERVICE_NAME")
2023-11-01 16:34:57 +02:00
Settings.READY_FOLDERNAME = strings.ToLower(Settings.SERVICE_NAME)
2023-10-06 15:48:58 +02:00
2023-10-23 17:09:55 +02:00
if Settings.TEMPLATE_FOLDERNAME == "" {
log.Panic("Need fill TEMPLATE_FOLDERNAME")
}
2023-10-24 17:03:04 +02:00
if Settings.TEXT_TEMPLATE_MODEL == "" {
Settings.TEXT_TEMPLATE_MODEL = "LawsuitStatusType"
}
if Settings.TEXT_TEMPLATE_TABLENAME == "" {
Settings.TEXT_TEMPLATE_TABLENAME = "lawsuit_status_types"
}
2023-10-25 13:52:49 +02:00
2023-11-07 16:58:02 +02:00
sUSE_DEFAULT_TEMPLATE := os.Getenv("USE_DEFAULT_TEMPLATE")
Settings.USE_DEFAULT_TEMPLATE = BoolFromString(sUSE_DEFAULT_TEMPLATE)
2023-10-25 13:52:49 +02:00
HAS_IS_DELETED := BoolFromString(sHAS_IS_DELETED)
Settings.HAS_IS_DELETED = HAS_IS_DELETED
2023-11-08 14:15:52 +02:00
Settings.COMMENT_MODEL_STRUCT = os.Getenv("COMMENT_MODEL_STRUCT")
2023-11-10 14:04:05 +02:00
Settings.TEXT_MODULE_GENERATED = os.Getenv("TEXT_MODULE_GENERATED")
sNEED_CREATE_DB_TEST := os.Getenv("NEED_CREATE_DB_TEST")
NEED_CREATE_DB_TEST := BoolFromString(sNEED_CREATE_DB_TEST)
Settings.NEED_CREATE_DB_TEST = NEED_CREATE_DB_TEST
sNEED_CREATE_GRPC_SERVER_TEST := os.Getenv("NEED_CREATE_GRPC_SERVER_TEST")
NEED_CREATE_GRPC_SERVER_TEST := BoolFromString(sNEED_CREATE_GRPC_SERVER_TEST)
Settings.NEED_CREATE_GRPC_SERVER_TEST = NEED_CREATE_GRPC_SERVER_TEST
sNEED_CREATE_GRPC_CLIENT_TEST := os.Getenv("NEED_CREATE_GRPC_CLIENT_TEST")
NEED_CREATE_GRPC_CLIENT_TEST := BoolFromString(sNEED_CREATE_GRPC_CLIENT_TEST)
Settings.NEED_CREATE_GRPC_CLIENT_TEST = NEED_CREATE_GRPC_CLIENT_TEST
sNEED_CREATE_NRPC_SERVER_TEST := os.Getenv("NEED_CREATE_NRPC_SERVER_TEST")
NEED_CREATE_NRPC_TEST := BoolFromString(sNEED_CREATE_NRPC_SERVER_TEST)
Settings.NEED_CREATE_NRPC_SERVER_TEST = NEED_CREATE_NRPC_TEST
sNEED_CREATE_NRPC_CLIENT_TEST := os.Getenv("NEED_CREATE_NRPC_CLIENT_TEST")
NEED_CREATE_NRPC_CLIENT_TEST := BoolFromString(sNEED_CREATE_NRPC_CLIENT_TEST)
Settings.NEED_CREATE_NRPC_CLIENT_TEST = NEED_CREATE_NRPC_CLIENT_TEST
2023-11-14 17:03:01 +02:00
Settings.TEMPLATE_FOLDERNAME_MAIN = os.Getenv("TEMPLATE_FOLDERNAME_MAIN")
2023-11-15 12:58:31 +02:00
Settings.TEMPLATE_REPOSITORY_URL = os.Getenv("TEMPLATE_REPOSITORY_URL")
2023-11-15 16:59:21 +02:00
Settings.TEMPLATE_FOLDERNAME_GRPC = os.Getenv("TEMPLATE_FOLDERNAME_GRPC")
2023-11-16 14:08:10 +02:00
Settings.PREFIX_CRUD = os.Getenv("PREFIX_CRUD")
Settings.PREFIX_TABLE = os.Getenv("PREFIX_TABLE")
Settings.TEMPLATE_FOLDERNAME_TABLES = os.Getenv("TEMPLATE_FOLDERNAME_TABLES")
Settings.PREFIX_MODEL = os.Getenv("PREFIX_MODEL")
2023-11-17 13:24:26 +02:00
Settings.TEMPLATE_FOLDERNAME_DB = os.Getenv("TEMPLATE_FOLDERNAME_DB")
Settings.TEMPLATE_FOLDER_CRUD_FUNCTIONS = os.Getenv("TEMPLATE_FOLDER_CRUD_FUNCTIONS")
Settings.TEMPLATE_EXTERNAL_PROTO_FILENAME = os.Getenv("TEMPLATE_EXTERNAL_PROTO_FILENAME")
2023-11-14 17:03:01 +02:00
2023-09-28 16:45:44 +02:00
}
// CurrentDirectory - возвращает текущую директорию ОС
func CurrentDirectory() string {
Otvet, err := os.Getwd()
if err != nil {
//log.Println(err)
}
return Otvet
}
// FillFlags - заполняет параметры из командной строки
func FillFlags() {
Args := os.Args[1:]
if len(Args) > 1 {
return
}
}
2023-10-06 15:48:58 +02:00
// BoolFromString - возвращает true если строка = true, или =1
func BoolFromString(s string) bool {
Otvet := false
s = strings.TrimLeft(s, " ")
s = strings.TrimRight(s, " ")
s = strings.ToLower(s)
if s == "true" || s == "1" {
Otvet = true
}
return Otvet
}
2023-11-17 14:08:31 +02:00
func LoadSettingsTxt() {
var err error
2024-01-15 12:19:27 +02:00
Fill_TEMPLATES_FOLDER_NAME()
2023-11-17 14:08:31 +02:00
DirBin := micro.ProgramDir_bin()
2024-01-15 12:19:27 +02:00
Dir := DirBin + constants.TEMPLATES_FOLDER_NAME + micro.SeparatorFile() + constants.CONFIG_FOLDER_NAME + micro.SeparatorFile()
2023-11-17 14:08:31 +02:00
FilenameEnv := Dir + ".env"
err = ConfigMain.LoadEnv_from_file_err(FilenameEnv)
if err == nil {
return
}
FilenameSettings := Dir + "settings.txt"
err = ConfigMain.LoadEnv_from_file_err(FilenameSettings)
if err != nil {
log.Panic("LoadSettingsTxt() filename: ", FilenameSettings, " error: ", err)
}
}
2024-01-15 12:19:27 +02:00
// Fill_TEMPLATES_FOLDER_NAME - заполняет переменную TEMPLATES_FOLDER_NAME = "templates_main" или "templates"
func Fill_TEMPLATES_FOLDER_NAME() {
DirBin := micro.ProgramDir_bin()
FileName := DirBin + "templates_main"
ok, err := micro.FileExists(FileName)
if err != nil {
log.Panic("FileExists() ", FileName, " error: ", err)
}
if ok == true {
constants.TEMPLATES_FOLDER_NAME = "templates_main"
}
2024-01-17 09:38:22 +02:00
log.Info("TEMPLATES_FOLDER_NAME = ", constants.TEMPLATES_FOLDER_NAME)
2024-01-15 12:19:27 +02:00
}