-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.go
79 lines (70 loc) · 1.63 KB
/
model.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
package gen
import (
"fmt"
"github.com/lorands/tymlate/config"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
"regexp"
)
type TemplateModel struct {
SourcePath string
TargetPath string
ConfigPath string
Config config.Config
StopIfTargetNotEmpty bool
}
func (tm *TemplateModel) isExcluded(source string) bool {
for _, exc := range tm.Config.Templates.Excludes {
regex := regexp.MustCompile(exc)
if regex.MatchString(source) {
return true
}
}
return false
}
func (tm *TemplateModel) isIncluded(source string) bool {
for _, incl := range tm.Config.Templates.Includes {
regex := regexp.MustCompile(incl)
if regex.MatchString(source) {
return true
}
}
return false
}
func NewTemplateModel(source string, target string, configPath string, stopIfNotEmpty bool) (error, *TemplateModel) {
if len(source) < 1 {
return fmt.Errorf("source must be set"), nil
}
if len(target) < 1 {
return fmt.Errorf("target must be set"), nil
}
if stopIfNotEmpty {
if _, err := os.Stat(target); os.IsExist(err) {
err := filepath.Walk(target, func(path string, info os.FileInfo, err error) error {
return fmt.Errorf("the target directory is not empty: %s", path)
})
if err != nil {
return err, nil
}
}
}
var model config.Config
if len(configPath) > 0 {
readFile, err := ioutil.ReadFile(configPath)
if err != nil {
panic(err)
}
if err = yaml.Unmarshal(readFile, &model); err != nil {
return err, nil
}
}
return nil, &TemplateModel{
SourcePath: source,
TargetPath: target,
ConfigPath: configPath,
Config: model,
StopIfTargetNotEmpty: stopIfNotEmpty,
}
}