Jelajahi Sumber

Autopopulate generator table

Andrea Fazzi 5 tahun lalu
induk
melakukan
9a60f05f2d
6 mengubah file dengan 37 tambahan dan 8 penghapusan
  1. 1 1
      Makefile
  2. 22 0
      main.go
  3. 8 0
      orm/document.go
  4. 1 1
      orm/job.go
  5. 1 0
      orm/orm.go
  6. 4 6
      watch.sh

+ 1 - 1
Makefile

@@ -6,6 +6,6 @@ run:
 	echo "Uploading files to the cloud..."
 	sleep 15
 	curl -u admin:password http://localhost:8080/remote.php/webdav/Documents/list.tpl.md -X PUT --data-binary @"./generator/generators/list/list.tpl.md"
-	curl -u admin:password http://localhost:8080/remote.php/webdav/Documents/reference.docx -X PUT --data-binary @"./generator/generators/list/reference.docx"
+	curl -u admin:password http://localhost:8080/remote.php/webdav/Documents/reference.odt -X PUT --data-binary @"./generator/generators/list/reference.odt"
 
 all: run

+ 22 - 0
main.go

@@ -5,6 +5,7 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"path/filepath"
 	"time"
 
 	"github.com/gorilla/handlers"
@@ -66,6 +67,27 @@ func main() {
 	generator.Generators = make(map[string]generator.Generator)
 	generator.Generators["list"] = list.NewListGenerator(config.Config)
 
+	files, err := filepath.Glob("generator/generators/*")
+	log.Println("Found the following generators...")
+	gTypes := make([]*orm.GeneratorType, 0)
+	for _, g := range files {
+		gTypes = append(gTypes, &orm.GeneratorType{Name: filepath.Base(g)})
+	}
+	log.Println("Create generator_type table...")
+	for _, gt := range gTypes {
+		var genType orm.GeneratorType
+		if err := orm.DB().Where("name = ?", gt.Name).First(&genType).Error; err != nil {
+			panic(err)
+		}
+		log.Println(genType)
+		if genType.Name == "" {
+			err := orm.DB().Debug().Create(gt).Error
+			if err != nil {
+				panic(err)
+			}
+		}
+	}
+
 	log.Println("Starting cron jobs...")
 
 	c := cron.New()

+ 8 - 0
orm/document.go

@@ -11,6 +11,12 @@ import (
 	"gogs.carducci-dante.gov.it/karmen/core/renderer"
 )
 
+type GeneratorType struct {
+	gorm.Model
+
+	Name string
+}
+
 type Document struct {
 	gorm.Model
 
@@ -19,6 +25,8 @@ type Document struct {
 	OutputPath string
 	CloudPath  string
 
+	GeneratorTypeID string
+
 	Jobs []*Job
 
 	RunningJobs   []*Job

+ 1 - 1
orm/job.go

@@ -47,7 +47,7 @@ func GetFile(args map[string]string) (interface{}, error) {
 
 func GetJobs(args map[string]string) (interface{}, error) {
 	var jobs []*Job
-	if err := DB().Debug().Order("start DESC").Find(&jobs).Error; err != nil {
+	if err := DB().Preload("Document").Order("start DESC").Find(&jobs).Error; err != nil {
 		return nil, err
 	}
 	return jobs, nil

+ 1 - 0
orm/orm.go

@@ -80,6 +80,7 @@ func AutoMigrate() {
 		&Document{},
 		&Job{},
 		&File{},
+		&GeneratorType{},
 	).Error; err != nil {
 		panic(err)
 	}

+ 4 - 6
watch.sh

@@ -1,10 +1,8 @@
 #!/bin/bash
 
+echo "Executing Makefile..."
+make -k
+
 while inotifywait -r -e modify ./; do
-    docker-compose -f compose/karmen/docker-compose.yml down
-    docker-compose -f compose/karmen/docker-compose.yml up --build -d
-    echo "Uploading files to the cloud..."
-    sleep 15
-    curl -u admin:password http://localhost:8080/remote.php/webdav/Documents/list.tpl.md -X PUT --data-binary @"./generator/generators/list/list.tpl.md"
-    curl -u admin:password http://localhost:8080/remote.php/webdav/Documents/reference.odt -X PUT --data-binary @"./generator/generators/list/reference.odt"
+    make -k
 done