Skip to content

Commit

Permalink
Merge pull request #48 from ekrako/template-generate-error-reporting
Browse files Browse the repository at this point in the history
fix(template): log  fail to compose template
  • Loading branch information
itamar-marom authored Oct 4, 2023
2 parents 7af8506 + cadb10a commit e91c4d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ config
bin/*
.idea
*.DS_Store
.DS_Store
.DS_Store
internal/services/drivers/version_control/ext-temp-git-test/*
internal/services/drivers/version_control/int-temp-git-test/*
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,19 @@ terra-crust terraform-all --destination-path="." --source-path=".../modules"
### Description
From version v0.1.0 TerraCrust will support fetching remote modules
and composite them into the root module that is being created.
From version v0.1.5 TerraCrust will support fetching remote modules
with git cli and not only with internal go git client.
so save credentials can be used, instead of generating tokens.
### How to use
in order to activate this feature all you have to do is to set `fetch-remote` to true like so:
```terra-crust terraform-all --main-template-path=./terraform/main.tmpl --destination-path="." --source-path=".../modules" fetch-remote=true```
``` bash
terra-crust terraform-all --main-template-path=./terraform/main.tmpl --destination-path="." --source-path=".../modules" --fetch-remote=true
```
or with an external git client with the flag `--ext-git` to true like so:
``` bash
terra-crust terraform-all --main-template-path=./terraform/main.tmpl --destination-path="." --source-path=".../modules" --ext-git=true
```

When activating `FetchRemote` you must use Custom main template, terracrust will look for all sources
that are from git it will look for this pattern:
1. `"git::REPOSITORY.git/PATH/MODULE"`
Expand All @@ -85,8 +95,9 @@ it will also support versioning/ branch reference so feel free to add them ,
more examples could be found under `examples/templates`.

Output Example for this module:
```"git::https://github.com/terraform-aws-modules/terraform-aws-iam.git/modules/iam-account"```
```
`"git::https://github.com/terraform-aws-modules/terraform-aws-iam.git/modules/iam-account"`

```terraform
module "iam-account" {
source = "git::https://github.com/terraform-aws-modules/terraform-aws-iam.git/modules/iam-account"
Expand Down Expand Up @@ -116,7 +127,7 @@ The main.tmpl exposing Template API that includes for now 2 functions:
2. GetDefaults - Will expose the optional variables - without needing to fill them up

For example:
```
```terraform
module "iam-account" {
source = "git::https://github.com/terraform-aws-modules/terraform-aws-iam.git/modules/iam-account"
Expand Down
10 changes: 8 additions & 2 deletions internal/services/template_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,22 @@ func (th *TemplateHandler) WriteTemplateToFile(fileName, templatePath, destinati
}

buf := new(bytes.Buffer)
if tmpl.Execute(buf, out) != nil {
if err = tmpl.Execute(buf, out); err != nil {
th.logger.Error("Failed executing template", err.Error())
return err
}

filePath := fmt.Sprintf("%s/%s", destinationPath, fileName)
if err = os.Remove(filePath); (err != nil) && (!errors.Is(err, os.ErrNotExist)) {
th.logger.Error("Failed removing file", err.Error())
return err
}

file, _ := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
th.logger.Error("Failed opening file", err.Error())
return err
}

defer file.Close()

Expand Down

0 comments on commit e91c4d1

Please sign in to comment.