summaryrefslogtreecommitdiff
path: root/internal/tmpl/tmpl.go
blob: 12baa7055266fd0f026cf085f9687b98eb6c37ce (plain)
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
// Package tmpl embeds every file Skiff writes out to the host - jail
// config, rc.d script, nginx vhost, git hook - so the whole tool ships as
// one binary with nothing extra to install alongside it.
package tmpl

import (
	"bytes"
	_ "embed"
	"text/template"
)

//go:embed jail.conf.tmpl
var jailConf string

//go:embed rc.d.tmpl
var rcScript string

//go:embed nginx-app.conf.tmpl
var nginxApp string

//go:embed post-receive.tmpl
var postReceive string

func Render(text string, data any) (string, error) {
	t, err := template.New("t").Parse(text)
	if err != nil {
		return "", err
	}
	var buf bytes.Buffer
	if err := t.Execute(&buf, data); err != nil {
		return "", err
	}
	return buf.String(), nil
}

type JailConfData struct {
	Name     string
	JailRoot string
	IP       string
	Iface    string
}

func JailConf(d JailConfData) (string, error) { return Render(jailConf, d) }

type RCData struct{ Name string }

func RCScript(d RCData) (string, error) { return Render(rcScript, d) }

type NginxAppData struct {
	Name       string
	IP         string
	Port       int
	ServerName string
}

func NginxApp(d NginxAppData) (string, error) { return Render(nginxApp, d) }

type PostReceiveData struct{ Name string }

func PostReceive(d PostReceiveData) (string, error) { return Render(postReceive, d) }