-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
123 lines (100 loc) · 2.52 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/mh-cbon/template-compiler/compiler"
)
// VERSION is the current program version.
var VERSION = "0.0.0"
func main() {
var err error
var versionPtr = flag.Bool("version", false, "Show version")
var help = flag.Bool("help", false, "Show help")
var shelp = flag.Bool("h", false, "Show help")
var keep = flag.Bool("keep", false, "Keep program generator")
var print = flag.Bool("print", false, "Keep program generator")
var varNamePtr = flag.String("var", "", "Name of the compiled.Registry variable to use")
var wdirPtr = flag.String("wdir", "", "Working directory")
flag.Parse()
if *versionPtr {
showVersion()
return
}
if *help || *shelp {
showHelp()
return
}
wdir := *wdirPtr
varName := *varNamePtr
if varName == "" {
varName = "compiledTemplates"
}
if wdir == "" {
wdir, err = eludeWorkingDirectory(wdir)
panicOnErr(err)
}
w, _ := os.Getwd()
file := filepath.Join(w, os.Getenv("GOFILE"))
prog, err := compiler.GenerateProgramBootstrapFromFile(
file,
varName,
)
panicOnErr(err)
if *print {
fmt.Println(prog)
}
if *keep {
fmt.Printf("Program written at %v\n", wdir+"/main.go")
}
err = ioutil.WriteFile(wdir+"/main.go", []byte(prog), os.ModePerm)
panicOnErr(err)
err2 := invokeProgram(wdir)
if *keep == false {
os.RemoveAll(wdir)
}
if err2 != nil {
os.Exit(1)
}
}
func showVersion() {
fmt.Println(`template-compiler - ` + VERSION)
}
func showHelp() {
showVersion()
fmt.Println(`
-help | -h Show this help.
-version Show program version.
-keep Keep bootstrap program compiler.
-print Print bootstrap program compiler.
-var The variable name of the configuration in your program
default: compiledTemplates
-wdir The working directory where the bootstrap program is written
default: $GOPATH/src/template-compilerxx/
Examples
template-compiler -h
template-compiler -version
template-compiler -keep -var theVarName
template-compiler -keep -var theVarName -wdir /tmp
`)
}
func panicOnErr(err error) {
if err != nil {
panic(err)
}
}
// eludeWorkingDirectory returns a working directory
// within GOPATH
func eludeWorkingDirectory(wdir string) (string, error) {
GoPath := os.Getenv("GOPATH") + "/src/"
return ioutil.TempDir(GoPath, "template-compiler")
}
func invokeProgram(wdir string) error {
c := exec.Command("go", []string{"run", wdir + "/main.go"}...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}