Browse Source

Flesh out templates a bit

master
Chris Smith 5 years ago
parent
commit
85e2e7d312
2 changed files with 18 additions and 8 deletions
  1. 2
    1
      dotege.go
  2. 16
    7
      template_generator.go

+ 2
- 1
dotege.go View File

@@ -42,7 +42,8 @@ func main() {
42 42
 
43 43
 	templateGenerator := NewTemplateGenerator()
44 44
 	templateGenerator.AddTemplate(TemplateConfig{
45
-		Source: "./templates/domains.txt.tpl",
45
+		Source:      "./templates/domains.txt.tpl",
46
+		Destination: "domains.txt",
46 47
 	})
47 48
 
48 49
 	monitor := NewContainerMonitor(cli, containerChan, expiryChan)

+ 16
- 7
template_generator.go View File

@@ -2,7 +2,7 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"os"
5
+	"io/ioutil"
6 6
 	"path"
7 7
 	"sort"
8 8
 	"strings"
@@ -49,22 +49,31 @@ func (t *TemplateGenerator) AddTemplate(config TemplateConfig) {
49 49
 		panic(err)
50 50
 	}
51 51
 
52
+	buf, _ := ioutil.ReadFile(config.Destination)
52 53
 	t.templates = append(t.templates, &Template{
53 54
 		config:   config,
54
-		content:  "", // TODO: Read this in initially
55
+		content:  string(buf),
55 56
 		template: tmpl,
56 57
 	})
57 58
 }
58 59
 
59 60
 func (t *TemplateGenerator) Generate(context Context) {
60 61
 	for _, tmpl := range t.templates {
61
-		// TODO: Actually write to file :)
62
-		// TODO: Retrieve the output and check if it matches our cache
63
-		fmt.Printf("--- Writing %s to %s ---\n", tmpl.config.Source, tmpl.config.Destination)
64
-		err := tmpl.template.Execute(os.Stdout, context)
65
-		fmt.Printf("--- / writing %s ---\n", tmpl.config.Destination)
62
+		fmt.Printf("Checking %s\n", tmpl.config.Source)
63
+		builder := &strings.Builder{}
64
+		err := tmpl.template.Execute(builder, context)
66 65
 		if err != nil {
67 66
 			panic(err)
68 67
 		}
68
+		if tmpl.content != builder.String() {
69
+			fmt.Printf("--- %s updated, writing to %s ---\n", tmpl.config.Source, tmpl.config.Destination)
70
+			fmt.Printf("%s", builder.String())
71
+			fmt.Printf("--- / writing %s ---\n", tmpl.config.Destination)
72
+			tmpl.content = builder.String()
73
+			err = ioutil.WriteFile(tmpl.config.Destination, []byte(builder.String()), 0666)
74
+			if err != nil {
75
+				panic(err)
76
+			}
77
+		}
69 78
 	}
70 79
 }

Loading…
Cancel
Save