-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (49 loc) · 1.21 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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
type templateVars map[string]string
var vars = make(templateVars)
var templateFile string
var outputFile string
func (tv templateVars) String() string {
return fmt.Sprintf("%+v", map[string]string(tv))
}
func init() {
flag.StringVar(&templateFile, "template", "", "path for template file")
flag.StringVar(&templateFile, "t", "", "path for template file (shortand)")
flag.StringVar(&outputFile, "out", "", "path for template file")
flag.StringVar(&outputFile, "o", "", "path for template file (shortand)")
flag.Var(&vars, "var", "blah")
}
func (tv templateVars) Set(value string) error {
if strings.Contains(value, "=") {
splitted := strings.Split(value, "=")
tv[splitted[0]] = splitted[1]
return nil
}
tv[value] = ""
return nil
}
func main() {
flag.Parse()
t, err := getTemplate(templateFile)
if err != nil {
log.Fatalf("Error parsing template: %s", err)
}
writer := os.Stdout
if outputFile != "" {
writer, err = os.Create(outputFile)
if err != nil {
log.Fatalf("Failed to create file %s: %s", outputFile, err)
}
}
defer writer.Close()
if err = t.Execute(writer, vars); err != nil {
log.Fatalf("Failed execute: %s", err)
}
}