-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (53 loc) · 1.5 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/xiroxasx/fastplate/internal/interpreter"
)
func parseFlags() (a interpreter.Options) {
flag.BoolVar(&a.Indent, "indent", false, "whether to retain indention or not")
flag.BoolVar(&a.NoStats, "no-stats", false, "do not print stats at the end of the execution")
flag.StringVar(&a.InPath, "in", "", "the root path")
flag.StringVar(&a.OutPath, "out", "", "the output path. If not used, in will be overwritten")
flag.BoolVar(&a.UseCRLF, "crlf", false, "whether to split lines by \\r\\n or \\n")
flag.Var(&a.VarFilePaths, "var", "the optional var file path.")
flag.Parse()
return
}
func init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
func main() {
if len(os.Args) == 1 {
return
}
opts := parseFlags()
if opts.OutPath == "" {
r := bufio.NewReader(os.Stdin)
fmt.Printf("Are you sure that you want to overwrite %s? [y/N] ", opts.InPath)
b, err := r.ReadByte()
if err != nil {
log.Fatal().Err(err).Msg("unable to read input")
}
if bytes.ToLower([]byte{b})[0] != 'y' {
log.Fatal().Err(err).Msg("canceled")
}
opts.OutPath = opts.InPath
}
if opts.InPath == "" {
log.Fatal().Msg("in path needs to be defined")
}
opts.InPath = filepath.Clean(opts.InPath)
opts.OutPath = filepath.Clean(opts.OutPath)
ip := interpreter.New(&opts)
err := ip.Start()
if err != nil {
log.Fatal().Err(err).Msg("error upon execution")
}
}