go读取terraform .tf文件内容

1 tf文件内容

$ cat string.tf 
variable "image_id" {
  type        = string
  description = "The id of the machine image (AMI) to use for the server."
  sensitive   = false
}

variable "other_id" {
  type        = string
  description = "The id of the machine image (AMI) to use for the server."
  sensitive   = true
}

2 实现代码

package main

import (
    "fmt"
    "log"
    "os"
    "encoding/json"

    "github.com/hashicorp/hcl/v2"
    "github.com/hashicorp/hcl/v2/gohcl"
    "github.com/hashicorp/hcl/v2/hclsyntax"
)

var (
    configFileSchema = &hcl.BodySchema{
        Blocks: []hcl.BlockHeaderSchema{
            {
                Type:       "variable",
                LabelNames: []string{"name"},
            },
        },
    }

    variableBlockSchema = &hcl.BodySchema{
        Attributes: []hcl.AttributeSchema{
            {
                Name: "description",
            },
            {
                Name: "type",
            },
            {
                Name: "sensitive",
            },
        },
    }
)

type Config struct {
    Variables []*Variable
}

type Variable struct {
    Name        string
    Description string
    Type        string
    Sensitive   bool
}

func main() {
    config := configFromFile("string.tf")
    conf,_ := json.Marshal(config)
    fmt.Printf("conf:%+v\n", string(conf))
    for _, v := range config.Variables {
        a,_ := json.Marshal(v)
        fmt.Printf("%+v\n", string(a))
    }
}

func configFromFile(filePath string) *Config {
    content, err := os.ReadFile(filePath) // go 1.16
    if err != nil {
        log.Fatal(err)
    }

    file, diags := hclsyntax.ParseConfig(content, filePath, hcl.Pos{Line: 1, Column: 1})
    if diags.HasErrors() {
        log.Fatal("ParseConfig", diags)
    }

    bodyCont, diags := file.Body.Content(configFileSchema)
    if diags.HasErrors() {
        log.Fatal("file content", diags)
    }

    res := &Config{}

    for _, block := range bodyCont.Blocks {
        v := &Variable{
            Name: block.Labels[0],
        }

        blockCont, diags := block.Body.Content(variableBlockSchema)
        if diags.HasErrors() {
            log.Fatal("block content", diags)
        }

        if attr, exists := blockCont.Attributes["description"]; exists {
            diags := gohcl.DecodeExpression(attr.Expr, nil, &v.Description)
            if diags.HasErrors() {
                log.Fatal("description attr", diags)
            }
        }

        if attr, exists := blockCont.Attributes["sensitive"]; exists {
            diags := gohcl.DecodeExpression(attr.Expr, nil, &v.Sensitive)
            if diags.HasErrors() {
                log.Fatal("sensitive attr", diags)
            }
        }

        if attr, exists := blockCont.Attributes["type"]; exists {
            v.Type = hcl.ExprAsKeyword(attr.Expr)
            if v.Type == "" {
                log.Fatal("type attr", "invalid value")
            }
        }

        res.Variables = append(res.Variables, v)
    }
    return res
}

3 执行结果

# go run read.go     
conf:{"Variables":[{"Name":"image_id","Description":"The id of the machine image (AMI) to use for the server.","Type":"string","Sensitive":false},{"Name":"other_id","Description":"The id of the machine image (AMI) to use for the server.","Type":"string","Sensitive":true}]}
{"Name":"image_id","Description":"The id of the machine image (AMI) to use for the server.","Type":"string","Sensitive":false}
{"Name":"other_id","Description":"The id of the machine image (AMI) to use for the server.","Type":"string","Sensitive":true}

相关推荐

  1. go读取terraform .tf文件内容

    2024-03-15 07:40:01       38 阅读
  2. Go 读取文件

    2024-03-15 07:40:01       32 阅读
  3. go语言】读取toml文件

    2024-03-15 07:40:01       60 阅读
  4. js读取本地 excel文件、txt文件内容

    2024-03-15 07:40:01       42 阅读
  5. Qt 快速读取文件最后一行内容

    2024-03-15 07:40:01       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 07:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 07:40:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 07:40:01       82 阅读
  4. Python语言-面向对象

    2024-03-15 07:40:01       91 阅读

热门阅读

  1. CFINet

    CFINet

    2024-03-15 07:40:01      40 阅读
  2. 用js实现斐波那契数列

    2024-03-15 07:40:01       35 阅读
  3. 最小二乘法

    2024-03-15 07:40:01       44 阅读
  4. 多线程(锁策略, synchronized 对应的锁策略)

    2024-03-15 07:40:01       44 阅读
  5. typescript 学习

    2024-03-15 07:40:01       47 阅读
  6. C++ 内存泄漏检测工具——Valgrind(Linux系统)

    2024-03-15 07:40:01       35 阅读
  7. Python分类汇总N张Excel表中的数据(附源码下载)

    2024-03-15 07:40:01       42 阅读
  8. 控制工程学 en

    2024-03-15 07:40:01       40 阅读
  9. C#进阶-ASP.NET常用控件总结

    2024-03-15 07:40:01       32 阅读
  10. vue:功能【xlsx】纯前端导出Excel

    2024-03-15 07:40:01       45 阅读
  11. 每日一题 第三期 洛谷 国王游戏

    2024-03-15 07:40:01       41 阅读
  12. 2062:【例1.3】电影票

    2024-03-15 07:40:01       39 阅读
  13. 基于单片机的步进电机升降速并行控制

    2024-03-15 07:40:01       37 阅读