我想要对Terraform的HCL文件进行格式化

首先

我正在进行实践Terraform在AWS上的系统设计和最佳实践,但在敲写Terraform代码时,我想到了”每次都要对齐格式好麻烦啊…有没有什么好的方法呢…”,所以进行了一番调查。

我想摆脱在疯狂连续按下半角空格键时的虚无感。

环境

Mac OS X 10.14.1 x86_64 的中文释义是「苹果操作系统X 10.14.1 x86_64」。

$ terraform -version
Terraform v0.12.29
+ provider.aws v3.0.0

有一个公式被准备好了…

很快就找到了。
只需输入“terraform fmt”。
https://www.terraform.io/docs/commands/index.html

$ terraform
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    login              Obtain and save credentials for a remote host
    logout             Remove locally-stored credentials for a remote host
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management

尝试使用

未能完成格式化。我稍微弄得有点凌乱。

data "aws_iam_policy_document" "allow_describe_regions" {
  statement {
    effect = "Allow"
    actions = ["ec2:DescribeRegions"]
 resources = ["*"]
}
}


resource "aws_iam_policy"   "example" {
  name = "takkii1010-policy"
  policy = data.aws_iam_policy_document.allow_describe_regions.json
}

执行terraform fmt

$ terraform fmt
main.tf

结果。

虽然他没有调整那些多余的换行,但是它变得很漂亮。

data "aws_iam_policy_document" "allow_describe_regions" {
  statement {
    effect    = "Allow"
    actions   = ["ec2:DescribeRegions"]
    resources = ["*"]
  }
}


resource "aws_iam_policy" "example" {
  name   = "takkii1010-policy"
  policy = data.aws_iam_policy_document.allow_describe_regions.json
}

其他的方法

我在IntelliJ IDEA和Atom上尝试了一下插件,发现也有。好像其他编辑器也有各种各样的插件供选择,可以根据个人喜好来选择,没问题。

对于IntelliJ IDEA而言

HashiCorp Terraform / HCL 语言支持插件。请访问以下链接获取插件:
https://plugins.jetbrains.com/plugin/7808-hashicorp-terraform–hcl-language-support/

当我使用 Alt+Cmd+L 进行格式化时,出现了一些意想不到的结果。

data "aws_iam_policy_document" "allow_describe_regions" {
  statement {
    effect = "Allow"
    actions = [
      "ec2:DescribeRegions"]
    resources = [
      "*"]
  }
}


resource "aws_iam_policy" "example" {
  name = "takkii1010-policy"
  policy = data.aws_iam_policy_document.allow_describe_regions.json
}

原子

文件保存时自动应用格式化程序非常方便,很实用。

由于使用了这个插件,所以执行的是terraform fmt命令,结果与官方相同。由于是在保存时执行,所以不会立即转换为格式化后的状态。需要稍等片刻。

data "aws_iam_policy_document" "allow_describe_regions" {
  statement {
    effect    = "Allow"
    actions   = ["ec2:DescribeRegions"]
    resources = ["*"]
  }
}


resource "aws_iam_policy" "example" {
  name   = "takkii1010-policy"
  policy = data.aws_iam_policy_document.allow_describe_regions.json
}

由于我使用Atom进行编写,所以这个插件看起来挺不错的。

顺便说一句,据说Qiita的语法高亮功能不支持Terraform的HCL格式。

广告
将在 10 秒后关闭
bannerAds