Project

hcl_parser

0.01
The project is in a healthy, maintained state
HCL Variables Parser
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
 Dependencies

Development

>= 0

Runtime

>= 0
 Project Readme

HclParser

Parse HCL files. The scope of this library is to only handle variables.tf and backend.tf files, as that's what's needed for Terraspace currently.

Usage

require "hcl_parser"
code =<<EOL
variable "project" {
  description = "The name of the GCP Project"
  default     = "test"
  type        = "string"
}
variable "name_prefix" {
  type        = "string"
}
EOL

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

Installation

gem 'hcl_parser'

Notes

  • Tried a few different Ruby HCL parsers: hcl-checker, hcl-rb, rhcl, ruby-hcl. They all seem to have one issue 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.