hcl_variables 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +38 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hcl_variables.gemspec +28 -0
- data/lib/hcl_variables.rb +13 -0
- data/lib/hcl_variables/parser.rb +64 -0
- data/lib/hcl_variables/version.rb +3 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 61566319d2d1b0a2282872cc396000b04641112f2e48bb39d9639334f30a53ee
|
4
|
+
data.tar.gz: e96020c4a988c14fbf9be62d9a058d04c1a4291c73e9e2fc562d838f795c3185
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ad20f92693a1edd6fd99132b48d361f283b461901c41789dd11dad9fec9e30bae928c094fb2fa7776ada21a3ebf9b9b18d02cec700051c98abb1eb67846e0a3
|
7
|
+
data.tar.gz: 647ab5531e3649c817aa2b4a3fc314a5e2cd33d1326d825a54ee1280ac11dca189acc2f6dceb1920b7b33de6b01b6a78454b6685bebe8e4323cda26ee1db7443
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hcl_variables (0.1.0)
|
5
|
+
rhcl
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
deep_merge (1.2.1)
|
11
|
+
diff-lcs (1.3)
|
12
|
+
rake (12.3.3)
|
13
|
+
rhcl (0.1.0)
|
14
|
+
deep_merge
|
15
|
+
rspec (3.9.0)
|
16
|
+
rspec-core (~> 3.9.0)
|
17
|
+
rspec-expectations (~> 3.9.0)
|
18
|
+
rspec-mocks (~> 3.9.0)
|
19
|
+
rspec-core (3.9.2)
|
20
|
+
rspec-support (~> 3.9.3)
|
21
|
+
rspec-expectations (3.9.2)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.9.0)
|
24
|
+
rspec-mocks (3.9.1)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.9.0)
|
27
|
+
rspec-support (3.9.3)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
hcl_variables!
|
34
|
+
rake (~> 12.0)
|
35
|
+
rspec (~> 3.0)
|
36
|
+
|
37
|
+
BUNDLED WITH
|
38
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Tung Nguyen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# HclVariables
|
2
|
+
|
3
|
+
Parse HCL Variables files. The scope of this library is to only handle `variables.tf` file.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require "hcl_variables"
|
9
|
+
code =<<EOL
|
10
|
+
variable "project" {
|
11
|
+
description = "The name of the GCP Project"
|
12
|
+
default = "test"
|
13
|
+
type = "string"
|
14
|
+
}
|
15
|
+
variable "name_prefix" {
|
16
|
+
type = "string"
|
17
|
+
}
|
18
|
+
EOL
|
19
|
+
|
20
|
+
parser = HclVariables::Parser.new(code)
|
21
|
+
parser.load
|
22
|
+
# Returns =>
|
23
|
+
#
|
24
|
+
# {"variable"=>
|
25
|
+
# {"project"=>
|
26
|
+
# {"description"=>"The name of project",
|
27
|
+
# "default"=>"test",
|
28
|
+
# "type"=>"string"},
|
29
|
+
# "name_prefix"=>{"type"=>"string"}}}
|
30
|
+
```
|
31
|
+
|
32
|
+
## Installation
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
gem 'hcl_variables'
|
36
|
+
```
|
37
|
+
|
38
|
+
## Notes
|
39
|
+
|
40
|
+
* Tried a few different Ruby HCL parsers: [hcl-checker](https://github.com/mfcastellani/hcl-checker), [hcl-rb](https://github.com/Ruin0x11/hcl-rb), [rhcl](https://github.com/winebarrel/rhcl), [ruby-hcl](https://github.com/sikula/ruby-hcl). They all seem to have one issues or another.
|
41
|
+
* This library preprocesses the text fed to the parser to workaround the parser issues. It's a workaround.
|
42
|
+
* Able to handle simple variable types and most complex types.
|
43
|
+
* Not able to handle multi-line complex variable types. There's a spec to document this.
|
44
|
+
* Will have to fix one of these parsers or write a new one.
|
45
|
+
* Open to PRs to help.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hcl_variables"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/hcl_variables/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "hcl_variables"
|
5
|
+
spec.version = HclVariables::VERSION
|
6
|
+
spec.authors = ["Tung Nguyen"]
|
7
|
+
spec.email = ["tongueroo@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "HCL Variables Parser"
|
10
|
+
spec.homepage = "https://github.com/tongueroo/hcl_variables"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "rhcl"
|
26
|
+
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "rhcl"
|
2
|
+
|
3
|
+
module HclVariables
|
4
|
+
class Parser
|
5
|
+
def initialize(raw)
|
6
|
+
@raw = raw
|
7
|
+
end
|
8
|
+
|
9
|
+
def load
|
10
|
+
return {} if empty? # Rhcl parser cannot handle empty file
|
11
|
+
Rhcl.parse(code)
|
12
|
+
end
|
13
|
+
|
14
|
+
# The parser being used cannot handle unquoted literal type values and comments.
|
15
|
+
# Hacking it and updating the raw code as a workaround.
|
16
|
+
# May have to fix the parser or write a new parser.
|
17
|
+
def code
|
18
|
+
return @code if @code
|
19
|
+
@code = fix_quotes(@raw)
|
20
|
+
end
|
21
|
+
|
22
|
+
def empty?
|
23
|
+
text = remove_comments(code)
|
24
|
+
lines = text.split("\n")
|
25
|
+
lines.reject! { |l| l.strip.empty? }
|
26
|
+
lines.empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
COMMENT_REGEX = /^(^|\s)#/i
|
30
|
+
def remove_comments(raw)
|
31
|
+
lines = raw.split("\n")
|
32
|
+
# filter out commented lines
|
33
|
+
lines.reject! { |l| l =~ COMMENT_REGEX }
|
34
|
+
# filter out empty lines
|
35
|
+
lines.reject! { |l| l.strip.empty? }
|
36
|
+
lines.join("\n")
|
37
|
+
end
|
38
|
+
|
39
|
+
def fix_quotes(raw)
|
40
|
+
lines = raw.split("\n")
|
41
|
+
lines.map! do |l|
|
42
|
+
quote_line(l)
|
43
|
+
end
|
44
|
+
lines.join("\n")
|
45
|
+
end
|
46
|
+
|
47
|
+
def quote_line(l)
|
48
|
+
return "# " if l =~ COMMENT_REGEX # return empty comment so parser wont try to parse it
|
49
|
+
return l unless l =~ /type\s*=/ # just check for type
|
50
|
+
return l if l =~ /type\s*=\s*['"]([a-zA-Z0-9()]+)["']/ # check quotes in the type value
|
51
|
+
|
52
|
+
# Reaching here means there is probably a type value without quotes
|
53
|
+
# Try to capture unquoted value so we can add quotes
|
54
|
+
md = l.match(/type\s*=\s*([a-zA-Z0-9()]+)/)
|
55
|
+
if md
|
56
|
+
value = md[1]
|
57
|
+
%Q|type = "#{value}"|
|
58
|
+
else
|
59
|
+
# Example: type = "list(object({
|
60
|
+
l # unable to capture quotes, passthrough as fallback
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hcl_variables
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tung Nguyen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rhcl
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- tongueroo@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- Gemfile
|
51
|
+
- Gemfile.lock
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- hcl_variables.gemspec
|
58
|
+
- lib/hcl_variables.rb
|
59
|
+
- lib/hcl_variables/parser.rb
|
60
|
+
- lib/hcl_variables/version.rb
|
61
|
+
homepage: https://github.com/tongueroo/hcl_variables
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata:
|
65
|
+
homepage_uri: https://github.com/tongueroo/hcl_variables
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 2.3.0
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubygems_version: 3.1.2
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: HCL Variables Parser
|
85
|
+
test_files: []
|