From 2fda60d969f7fedafc67316865974786a59e5098 Mon Sep 17 00:00:00 2001 From: Eran Krakovsky Date: Tue, 3 Oct 2023 15:52:08 +0300 Subject: [PATCH 1/2] fix(template): log fail to compose template --- README.md | 19 +++++++++++++++---- .../ext-temp-git-test/temp_clone_path/zones | 1 + .../int-temp-git-test/temp_clone_path/zones | 1 + internal/services/template_handler.go | 11 +++++++++-- 4 files changed, 26 insertions(+), 6 deletions(-) create mode 160000 internal/services/drivers/version_control/ext-temp-git-test/temp_clone_path/zones create mode 160000 internal/services/drivers/version_control/int-temp-git-test/temp_clone_path/zones diff --git a/README.md b/README.md index 421649f..b64549c 100644 --- a/README.md +++ b/README.md @@ -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"` @@ -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" @@ -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" diff --git a/internal/services/drivers/version_control/ext-temp-git-test/temp_clone_path/zones b/internal/services/drivers/version_control/ext-temp-git-test/temp_clone_path/zones new file mode 160000 index 0000000..d67ac59 --- /dev/null +++ b/internal/services/drivers/version_control/ext-temp-git-test/temp_clone_path/zones @@ -0,0 +1 @@ +Subproject commit d67ac59473bf3eb2628c30b2f26cb743dd3580ed diff --git a/internal/services/drivers/version_control/int-temp-git-test/temp_clone_path/zones b/internal/services/drivers/version_control/int-temp-git-test/temp_clone_path/zones new file mode 160000 index 0000000..d67ac59 --- /dev/null +++ b/internal/services/drivers/version_control/int-temp-git-test/temp_clone_path/zones @@ -0,0 +1 @@ +Subproject commit d67ac59473bf3eb2628c30b2f26cb743dd3580ed diff --git a/internal/services/template_handler.go b/internal/services/template_handler.go index eaf2149..87efb23 100644 --- a/internal/services/template_handler.go +++ b/internal/services/template_handler.go @@ -83,16 +83,23 @@ func (th *TemplateHandler) WriteTemplateToFile(fileName, templatePath, destinati } buf := new(bytes.Buffer) - if tmpl.Execute(buf, out) != nil { + err = tmpl.Execute(buf, out) + if 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() From cadb10a2e97d6e93cdede30aa7eaa232f6951813 Mon Sep 17 00:00:00 2001 From: Eran Krakovsky Date: Tue, 3 Oct 2023 17:02:30 +0300 Subject: [PATCH 2/2] fix(readme): add dashe to fetch-remote --- .gitignore | 4 +++- README.md | 2 +- .../version_control/ext-temp-git-test/temp_clone_path/zones | 1 - .../version_control/int-temp-git-test/temp_clone_path/zones | 1 - internal/services/template_handler.go | 3 +-- 5 files changed, 5 insertions(+), 6 deletions(-) delete mode 160000 internal/services/drivers/version_control/ext-temp-git-test/temp_clone_path/zones delete mode 160000 internal/services/drivers/version_control/int-temp-git-test/temp_clone_path/zones diff --git a/.gitignore b/.gitignore index 3252f45..ffba26b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ config bin/* .idea *.DS_Store -.DS_Store \ No newline at end of file +.DS_Store +internal/services/drivers/version_control/ext-temp-git-test/* +internal/services/drivers/version_control/int-temp-git-test/* diff --git a/README.md b/README.md index b64549c..fa5d6c1 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ 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: ``` bash -terra-crust terraform-all --main-template-path=./terraform/main.tmpl --destination-path="." --source-path=".../modules" fetch-remote=true +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 diff --git a/internal/services/drivers/version_control/ext-temp-git-test/temp_clone_path/zones b/internal/services/drivers/version_control/ext-temp-git-test/temp_clone_path/zones deleted file mode 160000 index d67ac59..0000000 --- a/internal/services/drivers/version_control/ext-temp-git-test/temp_clone_path/zones +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d67ac59473bf3eb2628c30b2f26cb743dd3580ed diff --git a/internal/services/drivers/version_control/int-temp-git-test/temp_clone_path/zones b/internal/services/drivers/version_control/int-temp-git-test/temp_clone_path/zones deleted file mode 160000 index d67ac59..0000000 --- a/internal/services/drivers/version_control/int-temp-git-test/temp_clone_path/zones +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d67ac59473bf3eb2628c30b2f26cb743dd3580ed diff --git a/internal/services/template_handler.go b/internal/services/template_handler.go index 87efb23..34ab85d 100644 --- a/internal/services/template_handler.go +++ b/internal/services/template_handler.go @@ -83,8 +83,7 @@ func (th *TemplateHandler) WriteTemplateToFile(fileName, templatePath, destinati } buf := new(bytes.Buffer) - err = tmpl.Execute(buf, out) - if err != nil { + if err = tmpl.Execute(buf, out); err != nil { th.logger.Error("Failed executing template", err.Error()) return err }