Skip to content

Commit

Permalink
maps
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeyheath committed Apr 29, 2024
1 parent 2ee374a commit 0928565
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 54 deletions.
32 changes: 15 additions & 17 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,23 @@ func applyRepo(fs afero.Fs, p *plan.Plan, repoTemplates, commonTemplates fs.FS)
}

func applyExtraTemplates(fs afero.Fs, p plan.ComponentCommon, commonBox fs.FS, path string) error {
for _, templateCfg := range p.ExtraTemplates {
for filename, template := range templateCfg.Files {
target := getTargetPath(path, filename)
_, err := fs.Stat(target)
if err == nil && !templateCfg.Overwrite {
// file exists and we don't want to overwrite
continue
}
for filename, templateCfg := range p.ExtraTemplates {
target := getTargetPath(path, filename)
_, err := fs.Stat(target)
if err == nil && !templateCfg.Overwrite {
// file exists and we don't want to overwrite
continue
}

err = applyTemplate(strings.NewReader(template), commonBox, fs, target, p)
if err != nil {
return errs.WrapUser(err, "applying extra templates")
}
err = applyTemplate(strings.NewReader(templateCfg.Content), commonBox, fs, target, p)
if err != nil {
return errs.WrapUser(err, "applying extra templates")
}

if filepath.Ext(filename) == ".tf" {
err = fmtHcl(fs, target, true)
if err != nil {
return errs.WrapUser(err, "formating HCL of extra templates")
}
if filepath.Ext(filename) == ".tf" {
err = fmtHcl(fs, target, true)
if err != nil {
return errs.WrapUser(err, "formating HCL of extra templates")
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions config/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ type TFE struct {
}

type ExtraTemplate struct {
Overwrite bool
Files map[string]string
Overwrite *bool
Content *string
}

type Common struct {
Backend *Backend `yaml:"backend,omitempty"`
ExtraVars map[string]string `yaml:"extra_vars,omitempty"`
Owner *string `yaml:"owner,omitempty"`
Project *string `yaml:"project,omitempty"`
Providers *Providers `yaml:"providers,omitempty"`
DependsOn *DependsOn `yaml:"depends_on,omitempty"`
TerraformVersion *string `yaml:"terraform_version,omitempty"`
Tools *Tools `yaml:"tools,omitempty"`
NeedsAWSAccountsVariable *bool `yaml:"needs_aws_accounts_variable,omitempty"`
ExtraTemplates *[]ExtraTemplate `yaml:"extra_templates,omitempty"`
Backend *Backend `yaml:"backend,omitempty"`
ExtraVars map[string]string `yaml:"extra_vars,omitempty"`
Owner *string `yaml:"owner,omitempty"`
Project *string `yaml:"project,omitempty"`
Providers *Providers `yaml:"providers,omitempty"`
DependsOn *DependsOn `yaml:"depends_on,omitempty"`
TerraformVersion *string `yaml:"terraform_version,omitempty"`
Tools *Tools `yaml:"tools,omitempty"`
NeedsAWSAccountsVariable *bool `yaml:"needs_aws_accounts_variable,omitempty"`
ExtraTemplates *map[string]ExtraTemplate `yaml:"extra_templates,omitempty"`
}

type Defaults struct {
Expand Down
43 changes: 26 additions & 17 deletions config/v2/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,26 +290,35 @@ func ResolveAWSAccountsNeeded(commons ...Common) bool {
return *accountsNeeded
}

func ExtraTemplatesGetter(comm Common) *[]ExtraTemplate {
if comm.ExtraTemplates != nil {
templates := []ExtraTemplate{}
for _, v := range *comm.ExtraTemplates {
templates = append(templates, ExtraTemplate{
Overwrite: v.Overwrite,
Files: v.Files,
})
func ResolveExtraTemplates(commons ...Common) map[string]ExtraTemplate {
templates := map[string]ExtraTemplate{}
for _, common := range commons {
if common.ExtraTemplates == nil {
continue
}
return &templates
}
return nil
}

func ResolveExtraTemplates(commons ...Common) []ExtraTemplate {
templates := lastNonNil(ExtraTemplatesGetter, commons...)
if templates == nil {
return []ExtraTemplate{}
for filename, cfg := range *common.ExtraTemplates {
if _, exists := templates[filename]; !exists {
templates[filename] = cfg
continue
}

prevTempl := ExtraTemplate{
Overwrite: templates[filename].Overwrite,
Content: templates[filename].Content,
}

if cfg.Overwrite != nil {
prevTempl.Overwrite = cfg.Overwrite
}
if cfg.Content != nil {
prevTempl.Content = cfg.Content
}
templates[filename] = prevTempl
}
}
return *templates

return templates
}

func ResolveSnowflakeProvider(commons ...Common) *SnowflakeProvider {
Expand Down
24 changes: 16 additions & 8 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Common struct {

type ExtraTemplate struct {
Overwrite bool
Files map[string]string
Content string
}

// ComponentCommon represents common fields for components
Expand All @@ -59,7 +59,7 @@ type ComponentCommon struct {
ProviderConfiguration ProviderConfiguration `yaml:"providers_configuration"`
ProviderVersions map[string]ProviderVersion `yaml:"provider_versions"`
NeedsAWSAccountsVariable bool `yaml:"needs_aws_accounts_variable"`
ExtraTemplates []ExtraTemplate `yaml:"extra_templates"`
ExtraTemplates map[string]ExtraTemplate `yaml:"extra_templates"`

TfLint TfLint `yaml:"tf_lint"`

Expand Down Expand Up @@ -1260,12 +1260,20 @@ func resolveComponentCommon(commons ...v2.Common) ComponentCommon {
}
}

extraTemplates := []ExtraTemplate{}
for _, v := range v2.ResolveExtraTemplates(commons...) {
extraTemplates = append(extraTemplates, ExtraTemplate{
Files: v.Files,
Overwrite: v.Overwrite,
})
extraTemplates := map[string]ExtraTemplate{}
for k, v := range v2.ResolveExtraTemplates(commons...) {
resolvedTempl := ExtraTemplate{}

if v.Content != nil {
resolvedTempl.Content = *v.Content
}
if v.Overwrite != nil {
resolvedTempl.Overwrite = *v.Overwrite
}
extraTemplates[k] = ExtraTemplate{
Overwrite: resolvedTempl.Overwrite,
Content: resolvedTempl.Content,
}
}

return ComponentCommon{
Expand Down

0 comments on commit 0928565

Please sign in to comment.