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
 
42
 
43
 	templateGenerator := NewTemplateGenerator()
43
 	templateGenerator := NewTemplateGenerator()
44
 	templateGenerator.AddTemplate(TemplateConfig{
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
 	monitor := NewContainerMonitor(cli, containerChan, expiryChan)
49
 	monitor := NewContainerMonitor(cli, containerChan, expiryChan)

+ 16
- 7
template_generator.go View File

2
 
2
 
3
 import (
3
 import (
4
 	"fmt"
4
 	"fmt"
5
-	"os"
5
+	"io/ioutil"
6
 	"path"
6
 	"path"
7
 	"sort"
7
 	"sort"
8
 	"strings"
8
 	"strings"
49
 		panic(err)
49
 		panic(err)
50
 	}
50
 	}
51
 
51
 
52
+	buf, _ := ioutil.ReadFile(config.Destination)
52
 	t.templates = append(t.templates, &Template{
53
 	t.templates = append(t.templates, &Template{
53
 		config:   config,
54
 		config:   config,
54
-		content:  "", // TODO: Read this in initially
55
+		content:  string(buf),
55
 		template: tmpl,
56
 		template: tmpl,
56
 	})
57
 	})
57
 }
58
 }
58
 
59
 
59
 func (t *TemplateGenerator) Generate(context Context) {
60
 func (t *TemplateGenerator) Generate(context Context) {
60
 	for _, tmpl := range t.templates {
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
 		if err != nil {
65
 		if err != nil {
67
 			panic(err)
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