-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
54 lines (44 loc) · 1.28 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/alecthomas/kong"
)
// nolint: gochecknoglobals
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
// nolint: lll
type CLI struct {
Version kong.VersionFlag `name:"version" help:"Show version information"`
TemplateFile string `env:"TEMPLE_TEMPLATE_FILE" arg:"" required:"" type:"existingfile" help:"The template file to use"`
DataFile *os.File `short:"d" name:"data" placeholder:"DATA-FILE" env:"TEMPLE_DATA_FILE" help:"A YAML or JSON file to use via the {{data.<foo>}} interface"`
HTML bool `short:"H" env:"TEMPLE_USE_HTML" help:"Use HTML templating instead of text templating"`
}
func (cli *CLI) Run() error {
var err error
funcMap, err := buildFuncMap(cli.DataFile)
if err != nil {
return err
}
if cli.HTML {
return doHTMLTemplate(cli.TemplateFile, funcMap, os.Stdout)
}
return doTextTemplate(cli.TemplateFile, funcMap, os.Stdout)
}
func main() {
cli := CLI{}
ctx := kong.Parse(&cli,
kong.Name("temple"),
kong.Description("A simple templating engine"),
kong.UsageOnError(),
kong.Vars{
"version": fmt.Sprintf("version\t%s\ncommit\t%s\nbuilt\t%s by %s", version, commit, date, builtBy),
},
)
err := ctx.Run(&cli)
ctx.FatalIfErrorf(err)
}