156 lines
4.2 KiB
Go
156 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
cfg "github.com/jesusperez/cfgsrv"
|
|
cvdata "github.com/jesusperez/cvdata"
|
|
utils "github.com/jesusperez/datautils"
|
|
)
|
|
|
|
func createRootModels(config *cfg.Config) error {
|
|
modelsFullPath := fmt.Sprintf("%s/%s",config.DataPath,config.DataModelsRoot)
|
|
var models []cvdata.ModelType
|
|
err := filepath.Walk(config.DataPath, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return err
|
|
}
|
|
if info.IsDir() {
|
|
if _, err := os.Stat(fmt.Sprintf("%s/core.yaml",path)); err == nil {
|
|
if !strings.Contains(path,"lang") {
|
|
name := filepath.Base(path)
|
|
modelPath := strings.Replace(path,config.DataPath,"",-1)
|
|
models = append(models, cvdata.ModelType{
|
|
Id: name,
|
|
Title: name,
|
|
Path: fmt.Sprintf("~/%s",modelPath),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("Error create %s: %v\n",modelsFullPath, err)
|
|
return err
|
|
}
|
|
var rootModels cvdata.Models
|
|
rootModels.Models = models
|
|
data, err := yaml.Marshal(rootModels)
|
|
if err != nil {
|
|
fmt.Printf("Error create yaml models data: %v\n", err)
|
|
return err
|
|
}
|
|
dataModelsPath := fmt.Sprintf("%s/%s",config.DataPath,config.DataModelsRoot)
|
|
if res := utils.WriteData(string(data),dataModelsPath) ; res != nil {
|
|
fmt.Printf("Error write %s: %v\n", dataModelsPath, err)
|
|
return res
|
|
}
|
|
data, err = json.Marshal(rootModels)
|
|
if err != nil {
|
|
fmt.Printf("Error create json models data: %v\n", err)
|
|
return err
|
|
}
|
|
dataModelsPath = fmt.Sprintf("%s/%s",config.DataDistPath,strings.Replace(config.DataModelsRoot,".yaml",".json",-1))
|
|
if res := utils.WriteData(string(data),dataModelsPath) ; res != nil {
|
|
fmt.Printf("Error write %s: %v\n", dataModelsPath, err)
|
|
return res
|
|
}
|
|
fmt.Printf("Root models write from %s\n", modelsFullPath)
|
|
return nil
|
|
}
|
|
func genJsonModels(config *cfg.Config) error {
|
|
if config.UseRepo && config.RepoPath != "" {
|
|
var errgit error
|
|
var cmd *exec.Cmd
|
|
if cmd,errgit = utils.GitPull(config.RepoPath,config.RepoName,config.BackgGit,config.QuietGit); errgit != nil {
|
|
fmt.Printf("Error pull %s (%s): %v\n", config.RepoPath, config.RepoName,errgit)
|
|
if config.BackgGit {
|
|
if errgit = cmd.Wait(); errgit != nil {
|
|
fmt.Printf("Error pull %s (%s): %v\n", config.RepoPath, config.RepoName,errgit)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
err := filepath.Walk(config.DataPath, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return err
|
|
}
|
|
if info.IsDir() {
|
|
if _, err := os.Stat(fmt.Sprintf("%s/core.yaml",path)); err == nil {
|
|
if !strings.Contains(path,"lang") {
|
|
dir := filepath.Base(path)
|
|
exclude := false
|
|
for _,itm := range config.GenExcludeList {
|
|
if itm == dir {
|
|
exclude = true
|
|
break
|
|
}
|
|
}
|
|
if !exclude {
|
|
if errjson := makeModelJson(config, dir, config.DataDistPath, true, false); errjson != nil {
|
|
fmt.Printf("Error write cv %s: %v\n", dir, errjson)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
return err
|
|
}
|
|
func makeModelJson(cfg *cfg.Config, modelName string, genPath string, quiet bool, useRepo bool) error {
|
|
cvdata, error := cvdata.LoadCVData(fmt.Sprintf("%s/%s",cfg.DataPath,modelName), cfg, useRepo)
|
|
if error != nil {
|
|
fmt.Printf("Error generating data %v\n", error)
|
|
}
|
|
data, err := json.Marshal(cvdata)
|
|
if err != nil {
|
|
fmt.Printf("Error Parse cv %s: %v\n", modelName, err)
|
|
return err
|
|
} else {
|
|
err := utils.CheckDirPath(fmt.Sprintf("%s",genPath))
|
|
if err == nil {
|
|
path := fmt.Sprintf("%s/%s.json",genPath,modelName)
|
|
if res := utils.WriteData(string(data),path) ; res != nil {
|
|
fmt.Printf("Error write cv %s: %v\n", modelName, err)
|
|
return err
|
|
} else {
|
|
if quiet {
|
|
fmt.Printf("CV %s writed to: %s\n", modelName,path)
|
|
}
|
|
}
|
|
} else {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func gendata(runFlags RunFlags) int {
|
|
cfg,err := loadDataConfig(runFlags)
|
|
//fmt.Printf("Loaded config: %#v\n", cfg)
|
|
if err != nil {
|
|
return 1
|
|
}
|
|
errModel := createRootModels(cfg)
|
|
if errModel != nil {
|
|
return 1
|
|
}
|
|
errModel = makeModelJson(cfg, runFlags.modelName, runFlags.genPath,true,true)
|
|
if errModel != nil {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|