Skip to content

Commit

Permalink
add a sicc compatibility mode
Browse files Browse the repository at this point in the history
We will remove this once we've transitioned all our repos.
This adds a flag, '-s' that will generate sicc-compatible output.

For example it will generate sicc.tf instead of fogg.tf.
  • Loading branch information
ryanking committed Jun 28, 2018
1 parent 6c55d1a commit bdb88de
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
39 changes: 29 additions & 10 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Apply(fs afero.Fs, configFile string, tmp *templates.T, siccMode bool) erro
}

func applyRepo(fs afero.Fs, p *plan.Plan, repoBox *packr.Box) error {
return applyTree(repoBox, fs, p)
return applyTree(repoBox, fs, p.SiccMode, p)
}

func applyGlobal(fs afero.Fs, p plan.Component, repoBox *packr.Box) error {
Expand All @@ -58,7 +58,7 @@ func applyGlobal(fs afero.Fs, p plan.Component, repoBox *packr.Box) error {
if e != nil {
return e
}
return applyTree(repoBox, afero.NewBasePathFs(fs, path), p)
return applyTree(repoBox, afero.NewBasePathFs(fs, path), p.SiccMode, p)
}

func applyAccounts(fs afero.Fs, p *plan.Plan, accountBox *packr.Box) (e error) {
Expand All @@ -68,7 +68,7 @@ func applyAccounts(fs afero.Fs, p *plan.Plan, accountBox *packr.Box) (e error) {
if e != nil {
return e
}
e = applyTree(accountBox, afero.NewBasePathFs(fs, path), accountPlan)
e = applyTree(accountBox, afero.NewBasePathFs(fs, path), accountPlan.SiccMode, accountPlan)
if e != nil {
return e
}
Expand All @@ -83,7 +83,7 @@ func applyEnvs(fs afero.Fs, p *plan.Plan, envBox *packr.Box, componentBox *packr
if e != nil {
return e
}
e := applyTree(envBox, afero.NewBasePathFs(fs, path), envPlan)
e := applyTree(envBox, afero.NewBasePathFs(fs, path), envPlan.SiccMode, envPlan)
if e != nil {
return e
}
Expand All @@ -93,7 +93,7 @@ func applyEnvs(fs afero.Fs, p *plan.Plan, envBox *packr.Box, componentBox *packr
if e != nil {
return e
}
e := applyTree(componentBox, afero.NewBasePathFs(fs, path), componentPlan)
e := applyTree(componentBox, afero.NewBasePathFs(fs, path), componentPlan.SiccMode, componentPlan)
if e != nil {
return e
}
Expand All @@ -102,26 +102,26 @@ func applyEnvs(fs afero.Fs, p *plan.Plan, envBox *packr.Box, componentBox *packr
return nil
}

func applyTree(source *packr.Box, dest afero.Fs, subst interface{}) (e error) {
func applyTree(source *packr.Box, dest afero.Fs, siccMode bool, subst interface{}) (e error) {
return source.Walk(func(path string, sourceFile packr.File) error {
extension := filepath.Ext(path)
basename := removeExtension(path)
target := getTargetPath(path, siccMode)
if extension == ".tmpl" {

err := applyTemplate(sourceFile, dest, basename, subst)
err := applyTemplate(sourceFile, dest, target, subst)
if err != nil {
return err
}

// if dest.endswith('.tf'):
// subprocess.call(['terraform', 'fmt', dest])
} else if extension == ".touch" {
touchFile(dest, basename)
touchFile(dest, target)
// if dest.endswith('.tf'):
// subprocess.call(['terraform', 'fmt', dest])

} else if extension == ".create" {
createFile(dest, basename, sourceFile)
createFile(dest, target, sourceFile)
// if dest.endswith('.tf'):
// subprocess.call(['terraform', 'fmt', dest])

Expand All @@ -132,6 +132,10 @@ func applyTree(source *packr.Box, dest afero.Fs, subst interface{}) (e error) {
return e
}
}
if !siccMode && target == "fogg.tf" {
log.Println("removing sicc.tf")
dest.Remove("sicc.tf")
}
return nil
})

Expand Down Expand Up @@ -178,3 +182,18 @@ func applyTemplate(sourceFile io.Reader, dest afero.Fs, path string, overrides i
t := util.OpenTemplate(sourceFile)
return t.Execute(writer, overrides)
}

func getTargetPath(path string, siccMode bool) string {
target := path
extension := filepath.Ext(path)

if extension == ".tmpl" || extension == ".touch" || extension == ".create" {
target = removeExtension(path)
}

if siccMode && filepath.Base(target) == "fogg.tf" {
target = filepath.Join(filepath.Dir(target), "sicc.tf")
}

return target
}
22 changes: 22 additions & 0 deletions apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ func TestApplySmokeTest(t *testing.T) {
assert.Nil(t, e)
}

func TestGetTargetPath(t *testing.T) {
data := []struct {
source string
siccOff string
siccOn string
}{
{"foo.tmpl", "foo", "foo"},
{"foo.tf.tmpl", "foo.tf", "foo.tf"},
{"fogg.tf", "fogg.tf", "sicc.tf"},
{"fogg.tf.tmpl", "fogg.tf", "sicc.tf"},
}
for _, test := range data {
t.Run(test.source, func(t *testing.T) {
off := getTargetPath(test.source, false)
on := getTargetPath(test.source, true)
assert.Equal(t, test.siccOff, off)
assert.Equal(t, test.siccOn, on)

})
}
}

func readFile(fs afero.Fs, path string) (string, error) {
f, e := fs.Open(path)
if e != nil {
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit bdb88de

Please sign in to comment.