-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtplreader.go
84 lines (76 loc) · 1.79 KB
/
tplreader.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
package main
import (
"fmt"
"github.com/gobuffalo/packr/v2"
"github.com/jasonlabz/gentol/metadata"
"os"
"path/filepath"
)
var innerBox *packr.Box
// IsExist check file or directory
func IsExist(path string) bool {
_, err := os.Stat(path)
if err != nil {
return os.IsExist(err)
}
return true
}
// IsFile checks whether the path is a file,
// it returns false when it's a directory or does not exist.
func IsFile(f string) bool {
fi, e := os.Stat(f)
if e != nil {
return false
}
return !fi.IsDir()
}
// IsDir checks whether the path is a directory,
// it returns false when it's a file or does not exist.
func IsDir(f string) bool {
fi, e := os.Stat(f)
if e != nil {
return false
}
return fi.IsDir()
}
// LoadTemplate return template from template dir, falling back to the embedded templates
func LoadTemplate(filename string, templateDir string) (tpl *metadata.Template, err error) {
baseName := filepath.Base(filename)
if templateDir != "" {
fpath := filepath.Join("", filename)
var b []byte
b, err = os.ReadFile(fpath)
if err == nil {
absPath, err := filepath.Abs(fpath)
if err != nil {
absPath = fpath
}
tpl = &metadata.Template{Name: "file://" + absPath, Content: string(b)}
return tpl, nil
}
}
template, ok := metadata.LoadTpl(baseName)
if !ok {
return nil, fmt.Errorf("%s not found internally", baseName)
}
return template, nil
}
func init() {
//innerBox = packr.New("gentol", "./template")
//_, filename, _, ok := runtime.Caller(1)
//if ok {
// fmt.Println(filename)
//}
//files, err := metadata.ListDir("./template", "")
//if err != nil {
// panic(err)
//}
//for _, file := range files {
// baseName := filepath.Base(file)
// var b []byte
// b, err = os.ReadFile(file)
// if err == nil {
// metadata.StoreTpl(baseName, string(b))
// }
//}
}