-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
executable file
·92 lines (81 loc) · 1.83 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
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
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"os"
"github.com/hay-kot/gotmpl/app/commands"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var (
// Build information. Populated at build-time via -ldflags flag.
version = "dev"
commit = "HEAD"
date = "now"
)
func build() string {
short := commit
if len(commit) > 7 {
short = commit[:7]
}
return fmt.Sprintf("%s (%s) %s", version, short, date)
}
func main() {
ctrl := &commands.Controller{}
app := &cli.App{
Name: "gotmpl",
Usage: "Tiny CLI template engine for generating files quickly",
Version: build(),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Usage: "log level (debug, info, warn, error, fatal, panic)",
Value: "panic",
},
},
Before: func(ctx *cli.Context) error {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
switch ctx.String("log-level") {
case "debug":
log.Level(zerolog.DebugLevel)
case "info":
log.Level(zerolog.InfoLevel)
case "warn":
log.Level(zerolog.WarnLevel)
case "error":
log.Level(zerolog.ErrorLevel)
case "fatal":
log.Level(zerolog.FatalLevel)
default:
log.Level(zerolog.PanicLevel)
}
return nil
},
Commands: []*cli.Command{
{
Name: "render",
Usage: "render template",
Action: ctrl.Render,
Flags: []cli.Flag{
&cli.PathFlag{
Name: "template",
Aliases: []string{"tmpl", "t"},
Usage: "template file",
Required: true,
Destination: &ctrl.Template,
},
&cli.PathFlag{
Name: "data",
Aliases: []string{"d"},
Usage: "data file",
Required: true,
Destination: &ctrl.DataFile,
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal().Err(err).Msg("failed to run gotmpl")
}
}