-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmutje.go
63 lines (50 loc) · 1.21 KB
/
smutje.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
package smutje
import (
"log"
"os"
"path/filepath"
"github.com/gfrey/smutje/parser"
"github.com/pkg/errors"
)
func ReadFile(filename string) (*Resource, error) {
astN, err := parser.Parse(filename)
if err != nil {
return nil, err
}
return convertToTarget(filepath.Dir(filename), astN)
}
func convertToTarget(path string, astN *parser.AstNode) (*Resource, error) {
switch astN.Type {
case parser.AstResource:
return NewResource(path, astN)
case parser.AstTemplate:
return nil, errors.Errorf("can't handle templates directly, use the include mechanism!")
default:
return nil, errors.Errorf("unexpected node seen: %s", astN.Type)
}
}
func Provision(res *Resource) error {
l := log.New(os.Stdout, "", log.Ldate|log.Ltime)
return provision(l, res)
}
func provision(l *log.Logger, res *Resource) error {
if err := res.Prepare(l); err != nil {
return err
}
if err := res.Generate(l); err != nil {
return err
}
return res.Provision(l)
}
func ProvisionHost(l *log.Logger, host, template string) error {
astN, err := parser.ParseString(host, template)
if err != nil {
return err
}
res, err := convertToTarget(host, astN)
if err != nil {
return err
}
res.address = host
return provision(l, res)
}