forked from hashicorp/otto
/
customization.go
82 lines (67 loc) · 2.29 KB
/
customization.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
package custom
import (
"path/filepath"
"github.com/hashicorp/otto/helper/compile"
"github.com/hashicorp/otto/helper/schema"
)
type customizations struct {
Opts *compile.AppOptions
}
func (c *customizations) process(d *schema.FieldData) error {
if p, ok := d.GetOk("packer"); ok {
c.Opts.Bindata.Context["build_packer_path"] = p.(string)
c.Opts.Callbacks = append(c.Opts.Callbacks, c.compileCustomBuild(d))
}
if tf, ok := d.GetOk("terraform"); ok {
c.Opts.Bindata.Context["deploy_terraform_path"] = tf.(string)
c.Opts.Callbacks = append(c.Opts.Callbacks, c.compileCustomDeploy(d))
}
if p, ok := d.GetOk("dev_vagrantfile"); ok {
c.Opts.Bindata.Context["dev_vagrant_path"] = p.(string)
c.Opts.Callbacks = append(c.Opts.Callbacks, c.compileCustomDev(d))
}
if _, ok := d.GetOk("dep_vagrantfile"); ok {
c.Opts.Callbacks = append(c.Opts.Callbacks, c.compileCustomDevDep(d))
}
return nil
}
func (c *customizations) compileCustomBuild(d *schema.FieldData) compile.CompileCallback {
return func() error {
return c.Opts.Bindata.RenderAsset(
filepath.Join(c.Opts.Ctx.Dir, "build", "packer_path"),
"data/sentinels/packer_path.tpl")
}
}
func (c *customizations) compileCustomDeploy(d *schema.FieldData) compile.CompileCallback {
return func() error {
return c.Opts.Bindata.RenderAsset(
filepath.Join(c.Opts.Ctx.Dir, "deploy", "terraform_path"),
"data/sentinels/terraform_path.tpl")
}
}
func (c *customizations) compileCustomDev(d *schema.FieldData) compile.CompileCallback {
return func() error {
c.Opts.Bindata.RenderReal(
filepath.Join(c.Opts.Ctx.Dir, "dev", "Vagrantfile"),
c.Opts.Bindata.Context["dev_vagrant_path"].(string))
return c.Opts.Bindata.RenderAsset(
filepath.Join(c.Opts.Ctx.Dir, "dev", "vagrantfile_path"),
"data/sentinels/vagrant_path.tpl")
}
}
func (c *customizations) compileCustomDevDep(d *schema.FieldData) compile.CompileCallback {
vf := d.Get("vagrantfile").(string)
return func() error {
if !filepath.IsAbs(vf) {
vf = filepath.Join(filepath.Dir(c.Opts.Ctx.Appfile.Path), vf)
}
data := c.Opts.Bindata
fragment := data.Context["fragment_path"].(string)
if err := data.RenderReal(fragment, vf); err != nil {
return err
}
return data.RenderAsset(
filepath.Join(c.Opts.Ctx.Dir, "dev", "Vagrantfile"),
"data/dev/Vagrantfile.tpl")
}
}