terraform-generator icon indicating copy to clipboard operation
terraform-generator copied to clipboard

terraform value from tfg.generate().tf is not pretty

Open luonmincuoi872 opened this issue 2 years ago • 6 comments

Describe the bug Terraform value from tfg.generate().tf is not pretty

To reproduce

const tfg = new TerraformGenerator();
tfg.provider('aws', {
  region: 'ap-southeast-1'
});

tfg.generate().tf return bellow

terraform {
required_providers {
aws = "3.3.0"
}
}

provider "aws" {
region = "ap-southeast-1"
}

Expected behavior

terraform {
  required_providers {
    aws = "3.3.0"
  }
}

provider "aws" {
  region = "ap-southeast-1"
}

luonmincuoi872 avatar Aug 31 '22 06:08 luonmincuoi872

vote up!

lengcheng111 avatar Aug 31 '22 06:08 lengcheng111

can I understand your use case of using .generate method?

ahzhezhe avatar Aug 31 '22 09:08 ahzhezhe

I init data return from generate method into monaco editor as i checked monaco editor not support format hcl code (and some other language like java ...) Is it posible to format terraform code in generate method?

luonmincuoi872 avatar Aug 31 '22 09:08 luonmincuoi872

There is no plan to format it using javascript at the moment, it is mentioned in README limitation section too. I might revisit this if I have time.

The reason why I didn't do it is because I usually write it into a file, the .write() method has an options to format the file using terraform fmt.

I am not familiar with monaco editor, could you explore whether it is possible to run terraform fmt?

ahzhezhe avatar Aug 31 '22 10:08 ahzhezhe

Actually, my project is use reactjs and there is an error when i used .write() method. fs.existssync is not a function I have not find the sollution yet. i dont know how is the content of generated terraform file.

luonmincuoi872 avatar Aug 31 '22 10:08 luonmincuoi872

It is suppose to be used in backend Nodejs. Using with react is not something I thought about and catered for.

I will update here if I manage to make .generate produce prettified text.

ahzhezhe avatar Aug 31 '22 11:08 ahzhezhe