HclVariables

Parse HCL Variables files. The scope of this library is to only handle variables.tf file.

Usage

require "hcl_variables"
code ="variable \"project\" {\n  description = \"The name of the GCP Project\"\n  default     = \"test\"\n  type        = \"string\"\n}\nvariable \"name_prefix\" {\n  type        = \"string\"\n}\n"

parser = HclVariables::Parser.new(code)
parser.load
# Returns =>
#
# {"variable"=>
#   {"project"=>
#     {"description"=>"The name of project",
#      "default"=>"test",
#      "type"=>"string"},
#    "name_prefix"=>{"type"=>"string"}}}

Installation

gem 'hcl_variables'

Notes

  • Tried a few different Ruby HCL parsers: hcl-checker, hcl-rb, rhcl, ruby-hcl. They all seem to have one issues or another.
  • This library preprocesses the text fed to the parser to workaround the parser issues. It's a workaround.
  • Able to handle simple variable types and most complex types.
  • Not able to handle multi-line complex variable types. There's a spec to document this.
  • Will have to fix one of these parsers or write a new one.
  • Open to PRs to help.