github.com/leowmjw/otto@v0.2.1-0.20160126165905-6400716cf085/builtin/app/custom/customization.go (about) 1 package custom 2 3 import ( 4 "path/filepath" 5 6 "github.com/hashicorp/otto/helper/compile" 7 "github.com/hashicorp/otto/helper/schema" 8 ) 9 10 type customizations struct { 11 Opts *compile.AppOptions 12 } 13 14 func (c *customizations) process(d *schema.FieldData) error { 15 if p, ok := d.GetOk("packer"); ok { 16 c.Opts.Bindata.Context["build_packer_path"] = p.(string) 17 c.Opts.Callbacks = append(c.Opts.Callbacks, c.compileCustomBuild(d)) 18 } 19 20 if tf, ok := d.GetOk("terraform"); ok { 21 c.Opts.Bindata.Context["deploy_terraform_path"] = tf.(string) 22 c.Opts.Callbacks = append(c.Opts.Callbacks, c.compileCustomDeploy(d)) 23 } 24 25 if p, ok := d.GetOk("dev_vagrantfile"); ok { 26 c.Opts.Bindata.Context["dev_vagrant_path"] = p.(string) 27 c.Opts.Callbacks = append(c.Opts.Callbacks, c.compileCustomDev(d)) 28 } 29 30 if _, ok := d.GetOk("dep_vagrantfile"); ok { 31 c.Opts.Callbacks = append(c.Opts.Callbacks, c.compileCustomDevDep(d)) 32 } 33 34 return nil 35 } 36 37 func (c *customizations) compileCustomBuild(d *schema.FieldData) compile.CompileCallback { 38 return func() error { 39 return c.Opts.Bindata.RenderAsset( 40 filepath.Join(c.Opts.Ctx.Dir, "build", "packer_path"), 41 "data/sentinels/packer_path.tpl") 42 } 43 } 44 45 func (c *customizations) compileCustomDeploy(d *schema.FieldData) compile.CompileCallback { 46 return func() error { 47 return c.Opts.Bindata.RenderAsset( 48 filepath.Join(c.Opts.Ctx.Dir, "deploy", "terraform_path"), 49 "data/sentinels/terraform_path.tpl") 50 } 51 } 52 53 func (c *customizations) compileCustomDev(d *schema.FieldData) compile.CompileCallback { 54 return func() error { 55 c.Opts.Bindata.RenderReal( 56 filepath.Join(c.Opts.Ctx.Dir, "dev", "Vagrantfile"), 57 c.Opts.Bindata.Context["dev_vagrant_path"].(string)) 58 return c.Opts.Bindata.RenderAsset( 59 filepath.Join(c.Opts.Ctx.Dir, "dev", "vagrantfile_path"), 60 "data/sentinels/vagrant_path.tpl") 61 } 62 } 63 64 func (c *customizations) compileCustomDevDep(d *schema.FieldData) compile.CompileCallback { 65 vf := d.Get("vagrantfile").(string) 66 67 return func() error { 68 if !filepath.IsAbs(vf) { 69 vf = filepath.Join(filepath.Dir(c.Opts.Ctx.Appfile.Path), vf) 70 } 71 72 data := c.Opts.Bindata 73 fragment := data.Context["fragment_path"].(string) 74 if err := data.RenderReal(fragment, vf); err != nil { 75 return err 76 } 77 78 return data.RenderAsset( 79 filepath.Join(c.Opts.Ctx.Dir, "dev", "Vagrantfile"), 80 "data/dev/Vagrantfile.tpl") 81 } 82 }