172 lines
4.9 KiB
Go
172 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.ymnuktech.ru/ymnuk/yt-gen-app/lib"
|
|
"git.ymnuktech.ru/ymnuk/yt-gen-app/structs"
|
|
"github.com/creasty/defaults"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func main() {
|
|
lib.PrepareParams()
|
|
if lib.AppConfig.IsHelp {
|
|
flag.Usage()
|
|
os.Exit(0)
|
|
}
|
|
if lib.AppConfig.Filename == "" {
|
|
flag.Usage()
|
|
os.Exit(0)
|
|
}
|
|
var err error
|
|
if lib.AppConfig.Filename, err = filepath.Abs(lib.AppConfig.Filename); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
lib.Project = &structs.Project{}
|
|
if _, err = os.Stat(lib.AppConfig.Filename); os.IsNotExist(err) {
|
|
log.Fatal(`Метафайл не найден`)
|
|
} else if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
var buff []byte
|
|
|
|
if buff, err = os.ReadFile(lib.AppConfig.Filename); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if err = yaml.Unmarshal(buff, lib.Project); err == nil {
|
|
lib.AppConfig.IsYaml = true
|
|
} else if err = json.Unmarshal(buff, lib.Project); err == nil {
|
|
lib.AppConfig.IsJson = true
|
|
} else {
|
|
log.Fatal("Ошибка открытия файла")
|
|
}
|
|
|
|
dir := filepath.Dir(lib.AppConfig.Filename)
|
|
|
|
// Загрузка значений для таблиц БД из файлов
|
|
for i := range lib.Project.DB.Tables {
|
|
if lib.Project.DB.Tables[i].IncludeValues != "" {
|
|
var values []map[string]interface{}
|
|
if _, err = os.Stat(filepath.Join(dir, fmt.Sprintf("%s.yml", lib.Project.DB.Tables[i].IncludeValues))); os.IsNotExist(err) {
|
|
if _, err = os.Stat(filepath.Join(dir, fmt.Sprintf("%s.json", lib.Project.DB.Tables[i].IncludeValues))); os.IsNotExist(err) {
|
|
log.Fatalf("Data not found in table %s", lib.Project.DB.Tables[i].Name)
|
|
} else {
|
|
// Загружаем
|
|
if buff, err = os.ReadFile(filepath.Join(dir, fmt.Sprintf("%s.json", lib.Project.DB.Tables[i].IncludeValues))); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err = json.Unmarshal(buff, &values); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
} else {
|
|
// Загружаем
|
|
if buff, err = os.ReadFile(filepath.Join(dir, fmt.Sprintf("%s.yml", lib.Project.DB.Tables[i].IncludeValues))); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if err = yaml.Unmarshal(buff, &values); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
lib.Project.DB.Tables[i].Values = append(lib.Project.DB.Tables[i].Values, values...)
|
|
}
|
|
}
|
|
|
|
// Подготовка значений по умолчанию
|
|
if err = defaults.Set(lib.Project); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Подгтовка абсолютных путей
|
|
if lib.AppConfig.OutdirBackend != "" {
|
|
if lib.AppConfig.OutdirBackend, err = filepath.Abs(lib.AppConfig.OutdirBackend); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
if lib.AppConfig.OutdirFrontend != "" {
|
|
if lib.AppConfig.OutdirFrontend, err = filepath.Abs(lib.AppConfig.OutdirFrontend); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
lib.PrepareMetadata(lib.Project)
|
|
|
|
lib.Generate()
|
|
|
|
filename := filepath.Base(lib.AppConfig.Filename)
|
|
filenameWithoutExt := fileNameWithoutExtSliceNotation(filename)
|
|
|
|
// Сохраняем значения таблиц (если они есть) во внешние файлы
|
|
for i := range lib.Project.DB.Tables {
|
|
if len(lib.Project.DB.Tables[i].Values) == 0 {
|
|
lib.Project.DB.Tables[i].IncludeValues = ""
|
|
continue
|
|
}
|
|
if lib.Project.DB.Tables[i].IncludeValues == "" {
|
|
lib.Project.DB.Tables[i].IncludeValues = fmt.Sprintf("./%s", lib.Project.DB.Tables[i].Name)
|
|
}
|
|
buff, err = yaml.Marshal(lib.Project.DB.Tables[i].Values)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err = os.WriteFile(filepath.Join(dir, fmt.Sprintf("%s.yml", lib.Project.DB.Tables[i].IncludeValues)), buff, 0755); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
/*buff, err = json.Marshal(lib.Project.DB.Tables[i].Values)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}*/
|
|
|
|
var buffer bytes.Buffer
|
|
err := lib.PrettyEncode(lib.Project.DB.Tables[i].Values, &buffer)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
/*if err = os.WriteFile(filepath.Join(dir, fmt.Sprintf("%s.json", lib.Project.DB.Tables[i].IncludeValues)), buff, 0755); err != nil {
|
|
log.Fatal(err)
|
|
}*/
|
|
if err = os.WriteFile(filepath.Join(dir, fmt.Sprintf("%s.json", lib.Project.DB.Tables[i].IncludeValues)), buffer.Bytes(), 0755); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
lib.Project.DB.Tables[i].Values = nil
|
|
buff = nil
|
|
}
|
|
|
|
buff = nil
|
|
if buff, err = yaml.Marshal(&lib.Project); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err = os.WriteFile(filepath.Join(dir, fmt.Sprintf("%s.yml", filenameWithoutExt)), buff, 0755); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
//buff = nil
|
|
var buffer bytes.Buffer
|
|
err = lib.PrettyEncode(&lib.Project, &buffer)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
/*if buff, err = json.Marshal(&lib.Project); err != nil {
|
|
log.Fatal(err)
|
|
}*/
|
|
if err = os.WriteFile(filepath.Join(dir, fmt.Sprintf("%s.json", filenameWithoutExt)), buffer.Bytes(), 0755); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
}
|
|
|
|
func fileNameWithoutExtSliceNotation(fileName string) string {
|
|
return fileName[:len(fileName)-len(filepath.Ext(fileName))]
|
|
}
|