/
renderer.go
84 lines (73 loc) · 1.58 KB
/
renderer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package rendergt
import (
"context"
"fmt"
"os"
"os/signal"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
const (
DefaultFileExt = "tmpl"
DefaultValuesFile = "values.yaml"
DefaultOutputDir = "stdout"
DefaultDelims = "{{}}"
)
var (
globalConfig = &Config{}
)
type Config struct {
InputDir string
OutputDir string
FileExt string
Values []string
Delims string
fileChan chan *string
exit chan os.Signal
createdDir map[string]*struct{}
leftDelim string
rightDelim string
}
func Run(conf *Config) error {
globalConfig = conf
globalConfig.fileChan = make(chan *string, 1)
globalConfig.exit = make(chan os.Signal, 1)
globalConfig.createdDir = map[string]*struct{}{}
globalConfig.leftDelim = conf.Delims[:len(conf.Delims)/2]
globalConfig.rightDelim = conf.Delims[len(conf.Delims)/2:]
return runRenderer()
}
func runRenderer() error {
read := make(chan struct{}, 1)
done := make(chan struct{}, 1)
signal.Notify(globalConfig.exit, os.Interrupt, os.Kill)
g, ctx := errgroup.WithContext(context.Background())
ctx, cancel := context.WithCancel(ctx)
defer cancel()
g.Go(func() error {
defer close(read)
return getFileList(ctx)
})
g.Go(func() error {
defer close(done)
return renderTemplates(ctx)
})
for {
select {
case <-read:
<-done
return g.Wait()
case <-done:
log.Debugf("Aborting...")
cancel()
return g.Wait()
case <-globalConfig.exit:
log.Info("Exit signal detected...Closing...")
cancel()
select {
case <-done:
return fmt.Errorf("Exit signal detected...Closing")
}
}
}
}