Categorygithub.com/yardbirdsax/terraparse
modulepackage
0.1.0
Repository: https://github.com/yardbirdsax/terraparse.git
Documentation: pkg.go.dev

# README

terraparse

This repository contains a helper library for extracting high-level metadata about Terraform modules from their source code. It processes only a subset of the information Terraform itself would process; in return, it can be broadly compatible with modules written for many different versions of Terraform.

I forked this project from the Hashicorp project terraform-config-inspect.

$ go get github.com/yardbirdsax/terraparse

Usage

import "github.com/yardbirdsax/terraparse/"

// ...

module, diags := terraparse.LoadModule(dir)

// ...

Due to the Terraform v1.0 Compatibility Promises, this library should be able to parse Terraform configurations written in the language defined by Terraform v1.0. However, it may not immediately expose new additions to the language added during the v1.x series.

This library can also interpret valid Terraform configurations targeting Terraform v0.10 through v0.15, although the level of detail returned may be lower in older language versions.

Command Line Tool

The primary way to use this repository is as a Go library. As a convenience, it also contains a CLI tool called terraparse that allows viewing module information in either a Markdown-like format or JSON format. You can install the tool by running go install github.com/yardbirdsax/terraparse/cmd/terraparse.

$ terraparse path/to/module
# Module `path/to/module`

Provider Requirements:
* **null:** (any version)

## Input Variables
* `a` (default `"a default"`)
* `b` (required): The b variable

## Output Values
* `a`
* `b`: I am B

## Managed Resources
* `null_resource.a` from `null`
* `null_resource.b` from `null`
$ terraform-config-inspect --json path/to/module
{
  "path": "path/to/module",
  "variables": {
    "A": {
      "name": "A",
      "default": "A default",
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 1
      }
    },
    "B": {
      "name": "B",
      "description": "The B variable",
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 5
      }
    }
  },
  "outputs": {
    "A": {
      "name": "A",
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 9
      }
    },
    "B": {
      "name": "B",
      "description": "I am B",
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 13
      }
    }
  },
  "required_providers": {
    "null": []
  },
  "managed_resources": {
    "null_resource.A": {
      "mode": "managed",
      "type": "null_resource",
      "name": "A",
      "provider": {
        "name": "null"
      },
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 18
      }
    },
    "null_resource.B": {
      "mode": "managed",
      "type": "null_resource",
      "name": "B",
      "provider": {
        "name": "null"
      },
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 19
      }
    }
  },
  "data_resources": {},
  "module_calls": {}
}

Contributing

As with its upstream inspiration, this project allows parsing a limited set of Terraform dialects. While I will try to keep it in sync with additions or changes to that, there is no expectation that I will do this on any particular schedule.

Bug fixes are welcome so long as they include test coverage proving they work. If you would like to contribute an enhancement or extend the language support of this library, I would suggest opening an issue first for discussion.

# Packages

No description provided by the author

# Functions

IsModuleDir checks if the given path contains terraform configuration files.
IsModuleDirOnFilesystem checks if the given path in the given FS contains Terraform configuration files.
LoadModule reads the directory at the given path and attempts to interpret it as a Terraform module.
LoadModuleFromFile reads given file, interprets it and stores in given Module This is useful for any caller which does tokenization/parsing on its own e.g.
LoadModuleFromFilesystem reads the directory at the given path in the given FS and attempts to interpret it as a Terraform module.
NewAttributes contructs as map of Attributes from the raw HCL attributes.
NewAttributesFromBody constructs a map of Attributes from an HCL body object.
NewModule creates new Module representing Terraform module at the given path.
NewOsFs provides a basic implementation of FS for an OS filesystem.
No description provided by the author
WrapFS wraps a standard library filesystem implementation so that it implements this package's own (slightly-incompatible) interface [FS].

# Constants

No description provided by the author
DiagError indicates a problem that prevented proper processing of the configuration.
DiagWarning indicates a problem that the user may wish to consider but that did not prevent proper processing of the configuration.
No description provided by the author
No description provided by the author

# Structs

Attribute represents a single HCL attribute attached to a block.
Diagnostic describes a problem (error or warning) encountered during configuration loading.
Module is the top-level type representing a parsed and processed Terraform module.
ModuleCall represents a "module" block within a module.
Output represents a single output from a Terraform module.
ProviderConfig represents a provider block in the configuration.
ProviderRef is a reference to a provider configuration within a module.
No description provided by the author
Resource represents a single "resource" or "data" block within a module.
SourcePos is a pointer to a particular location in a source file.
Variable represents a single variable from a Terraform module.

# Interfaces

File represents an open file in [FS].
FS is an interface used by [LoadModuleFromFilesystem].

# Type aliases

Attributes is a map of Attribute objects by the name of the attribute.
Diagnostics represents a sequence of diagnostics.
DiagSeverity describes the severity of a Diagnostic.
ResourceMode represents the "mode" of a resource, which is used to distinguish between managed resources ("resource" blocks in config) and data resources ("data" blocks in config).