-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
86 lines (75 loc) · 1.91 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
package main
import (
"bytes"
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/qor/qor/utils"
)
func main() {
exitAfterCompile := flag.Bool("exit-after-compile", true, "Exit after compile templates")
flag.Parse()
args := flag.Args()
if len(args) == 0 {
fmt.Println("invalid argument")
os.Exit(1)
}
fmt.Println("Initalizing BindataFS...")
destPath := args[0]
funcMap := map[string]interface{}{
"package_path": func() string {
return destPath
},
"package_name": func() string {
return path.Base(destPath)
},
"exit_after_compile": func() bool {
return *exitAfterCompile
},
}
hasExists := false
for _, gopath := range utils.GOPATH() {
sourcePath := filepath.Join(gopath, "src/github.com/qor/bindatafs/templates")
_, err := os.Stat(sourcePath)
if err == nil {
hasExists = true
}
err = filepath.Walk(sourcePath, func(path string, info os.FileInfo, err error) error {
if err == nil {
var relativePath = strings.TrimPrefix(path, sourcePath)
if info.IsDir() {
err = os.MkdirAll(filepath.Join(destPath, relativePath), os.ModePerm)
} else if info.Mode().IsRegular() {
if source, err := ioutil.ReadFile(path); err == nil {
var tmpl *template.Template
if tmpl, err = template.New("").Funcs(funcMap).Parse(string(source)); err == nil {
var result = bytes.NewBufferString("")
if err = tmpl.Execute(result, ""); err != nil {
return err
}
source = result.Bytes()
} else {
return err
}
if err = ioutil.WriteFile(filepath.Join(destPath, strings.TrimSuffix(relativePath, ".template")), source, os.ModePerm); err != nil {
fmt.Println(err)
}
}
}
}
return err
})
if hasExists && err == nil {
fmt.Printf("copy from %s to %s\n", sourcePath, destPath)
break
}
if err != nil {
fmt.Println("failed to copy files:", err)
}
}
}