hcl_variables 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hcl_variables.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -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
@@ -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.
@@ -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.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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,13 @@
1
+ require "hcl_variables/version"
2
+
3
+ module HclVariables
4
+ class Error < StandardError; end
5
+
6
+ autoload :Parser, "hcl_variables/parser"
7
+
8
+ def load(code)
9
+ Parser.new(code).load
10
+ end
11
+
12
+ extend self
13
+ 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
@@ -0,0 +1,3 @@
1
+ module HclVariables
2
+ VERSION = "0.1.0"
3
+ 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: []