Skip to content

Commit 05727a4

Browse files
authored
Merge pull request #1 from nichtraunzer/feature/createmodule-script
run rubocop --auto-correct-all
2 parents 217163b + caf6501 commit 05727a4

File tree

5 files changed

+127
-85
lines changed

5 files changed

+127
-85
lines changed

.rubocop.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
AllCops:
2+
TargetRubyVersion: 2.6
3+
NewCops: enable
4+
5+
Metrics/BlockLength:
6+
Exclude:
7+
- "**/*"
8+
Naming/ClassAndModuleCamelCase:
9+
Description: Use CamelCase for classes and modules.
10+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#camelcase-classes
11+
Enabled: true
12+
Naming/MethodName:
13+
Description: Use the configured style when naming methods.
14+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-symbols-methods-vars
15+
Enabled: true
16+
EnforcedStyle: camelCase
17+
SupportedStyles:
18+
- snake_case
19+
- camelCase
20+
Naming/VariableName:
21+
Description: Use the configured style when naming variables.
22+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-symbols-methods-vars
23+
Enabled: true
24+
EnforcedStyle: camelCase
25+
SupportedStyles:
26+
- snake_case
27+
- camelCase

hooks/createbpmoduleoutputs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ for adir in ${FIXTURE_DIRS} ; do
3535
# Only select the blueprint name which refers to the module in ../../..
3636
MODULE_NAMES=$(terraform-config-inspect . --json| jq -r '.module_calls|.[]|select(.source=="../../..")|.name')
3737

38-
echo "# file created automatically" > "${TFMFILE}"
38+
printf "# This file has been created automatically.\n\n" > "${TFMFILE}"
3939

4040
#for modules in $(terraform-config-inspect . --json| jq -r '.module_calls|keys[]') ; do
4141

hooks/createi2o.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ fi
2828
TFDOCSHCL=$(terraform-docs tfvars json "${CWD}" | jq -r 'keys[] as $k | "\($k) = var.\($k)"' | indenttfo )
2929

3030
cat > "${TFHFILE}" <<EOF
31+
# This file has been created automatically.
32+
3133
output "inputs2outputs" {
3234
description = "all inputs passed to outputs"
3335
value = [{

hooks/createstackmoduleoutputs.rb

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
24
#
35
# createstackmoduleoutputs.rb
46
# Author: erhard.wais@boehringer-ingelheim.com
@@ -16,24 +18,38 @@
1618
require 'json'
1719
require 'open3'
1820

19-
INSPECAWS = ""
20-
INSPECAWSTAG = ""
21-
CAPTUREFROMSTACK = 'terraform-config-inspect --json'
22-
ENVNAME = 'KITCHEN_SUITE_NAME'
23-
OUTPUTSTF = './stackmodulesoutputs.tf'
24-
BANNER = "# This file has been created automatically.\n\n"
25-
BANNER2 = "name: stackdefault\n" + "supports:\n" + " - platform: aws\n" + "depends:\n" + " - name: inspec-aws\n" + " git: https://github.com/inspec/inspec-aws\n" + " tag: v1.33.0\n" + "# Begin - blueprint inspec profiles\n"
26-
27-
stdout, stderr, status = Open3.capture3(CAPTUREFROMSTACK)
28-
29-
myEnv = ENV.fetch(ENVNAME,"default")
30-
moduleNames = Hash.new
31-
moduleSources = Hash.new
21+
CAPTUREFROMSTACK = 'terraform-config-inspect --json'
22+
ENVNAME = 'KITCHEN_SUITE_NAME'
23+
OUTPUTSTF = './stackmodulesoutputs.tf'
24+
BANNER = "# This file has been created automatically.\n\n"
25+
INSPECYMLTMPLFILE = './test/integration/default/inspec.yml.tmpl'
26+
INSPECYMLTMPLSTR = <<~MYYML
27+
---
28+
name: stackdefault
29+
supports:
30+
- platform: aws
31+
depends:
32+
- name: inspec-aws
33+
git: https://github.com/inspec/inspec-aws
34+
tag: v1.33.0
35+
MYYML
36+
37+
INSPECYMLHEAD = if File.exist?(INSPECYMLTMPLFILE)
38+
File.read(INSPECYMLTMPLFILE)
39+
else
40+
INSPECYMLTMPLSTR
41+
end
42+
43+
stdout, _stderr, _status = Open3.capture3(CAPTUREFROMSTACK)
44+
45+
myEnv = ENV.fetch(ENVNAME, 'default')
46+
moduleNames = {}
47+
moduleSources = {}
3248
uniqueBP = Hash.new(0)
3349

34-
CAPTUREFROMFIXTURE = "terraform-config-inspect ./test/fixtures/#{myEnv} --json"
50+
CAPTUREFROMFIXTURE = "terraform-config-inspect ./test/fixtures/#{myEnv} --json"
3551

36-
stdoutfixture, stderrfixture, statusfixture = Open3.capture3(CAPTUREFROMFIXTURE)
52+
stdoutfixture, _stderrfixture, _statusfixture = Open3.capture3(CAPTUREFROMFIXTURE)
3753

3854
stackName = File.basename(Dir.getwd)
3955
outputTF = File.open(OUTPUTSTF, 'w')
@@ -45,107 +61,106 @@
4561
modoutTF.write(BANNER)
4662
allBPsRB.write(BANNER)
4763
inspecYML.write(BANNER)
48-
inspecYML.write(BANNER2)
64+
inspecYML.write(INSPECYMLHEAD)
4965

5066
# get module section from main.json
5167
allModules = JSON.parse(stdout)['module_calls']
5268
allModulesfixtures = JSON.parse(stdoutfixture)['module_calls']
5369

54-
fixturemodulename = ""
55-
allModulesfixtures.each do |singleModule|
56-
fixturemodulename = singleModule[0]
70+
fixturemodulename = ''
71+
allModulesfixtures.each do |singlemodule|
72+
fixturemodulename = singlemodule[0]
5773
end
5874

5975
# for each module
60-
allModules.each do |singleModule|
61-
76+
allModules.each do |singlemodule|
6277
# get modulename and attributes
63-
name = singleModule[0]
64-
attribute = singleModule[1]
78+
name = singlemodule[0]
79+
attribute = singlemodule[1]
6580

6681
moduleOut = name
6782
moduleValue = "#{name}.*"
6883
fileName = attribute['source']
69-
moduleBP = File.basename(fileName[0..fileName.index('git?ref')], ".*")
84+
moduleBP = File.basename(fileName[0..fileName.index('git?ref')], '.*')
7085

7186
# create helper hashes to track which module uses which BP and how often
7287
moduleNames[moduleOut] = moduleBP
7388
moduleSources[moduleBP] = fileName
7489
uniqueBP[moduleBP] += 1
7590

76-
# create outputX.tf
77-
outputTF.write("output \"module_#{moduleOut.gsub(/-/,"_")}\" {\n")
78-
outputTF.write(" value = module.#{moduleValue}\n}\n")
91+
# create outputX.tf
92+
outputTF.write("output \"module_#{moduleOut.gsub(/-/, '_')}\" {\n")
93+
outputTF.write(" value = module.#{moduleValue}\n}\n")
7994
end
8095

8196
# create moduleoutputs.tf
82-
stackOut = stackName.gsub(/-/,"_")
83-
stackValue = "module.#{stackOut}_default"
97+
stackOut = stackName.gsub(/-/, '_')
98+
# FIXME#stackValue = "module.#{stackOut}_default"
8499

85100
modoutTF.write("output \"module_#{stackOut}\" {\n")
86101
modoutTF.write(" value = module.#{fixturemodulename}.*\n}\n")
87102

88103
uniqueBP.each do |name, count|
89-
duplicates = Array.new
104+
duplicates = []
90105

91-
moduleNames.select { |k, v| v == name }.keys.each {|dup| duplicates << dup}
106+
moduleNames.select { |_k, v| v == name }.each_key { |dup| duplicates << dup }
92107

93108
if count == 1
94-
moduleValue = "module.#{fixturemodulename}.module_#{duplicates[0].gsub(/-/,"_")}.*"
109+
moduleValue = "module.#{fixturemodulename}.module_#{duplicates[0].gsub(/-/, '_')}.*"
95110
moduleV2 = "module.#{duplicates[0]}.*"
96111
else
97-
moduleValue = "concat("
98-
duplicates.each_index {|i| moduleValue += "module.#{fixturemodulename}.module_#{duplicates[i].gsub(/-/,"_")}.*,"}
99-
moduleValue = moduleValue[0...-1] + ")"
112+
moduleValue = 'concat('
113+
duplicates.each_index { |i| moduleValue += "module.#{fixturemodulename}.module_#{duplicates[i].gsub(/-/, '_')}.*," }
114+
moduleValue = "#{moduleValue[0...-1]})"
100115

101-
moduleV2 = "concat("
102-
duplicates.each_index {|i| moduleV2 += "module.#{duplicates[i]}.*,"}
103-
moduleV2 = moduleV2[0...-1] + ")"
116+
moduleV2 = 'concat('
117+
duplicates.each_index { |i| moduleV2 += "module.#{duplicates[i]}.*," }
118+
moduleV2 = "#{moduleV2[0...-1]})"
104119
end
105120

106-
moduleOut = name.gsub(/-/,"_")
107-
modoutTF.write( "output \"module_#{moduleOut}\" {\n")
108-
modoutTF.write( " value = #{moduleValue}\n}\n")
121+
moduleOut = name.gsub(/-/, '_')
122+
modoutTF.write("output \"module_#{moduleOut}\" {\n")
123+
modoutTF.write(" value = #{moduleValue}\n}\n")
109124

110125
# write stackmoduleoutputs.tf
111-
outputTF.write("output \"module_#{moduleOut.gsub(/-/,"_")}\" {\n")
126+
outputTF.write("output \"module_#{moduleOut.gsub(/-/, '_')}\" {\n")
112127
outputTF.write(" value = #{moduleV2}\n}\n")
113128

114129
# read the source/filename from the helper hash
115-
fileName = moduleSources.select { |k, v| k == name }[name]
130+
fileName = moduleSources.select { |k, _v| k == name }[name]
116131

117132
# check if is repository and if it is a BI blueprint
118133
hasGit = fileName.index('git::')
119134
hasGitRef = fileName.index('.git?ref')
120135
isBP = fileName.index('blueprint')
121136

122-
if isBP
123-
# write blueprints.rb
124-
allBPsRB.write("include_controls '#{moduleOut}'\n")
125-
126-
# write inspec.yml
127-
inspecYML.write(" - name: #{moduleOut}\n")
128-
129-
if hasGit
130-
# puts " ... Tag #{fileName[(hasGitRef + 9)..]} "
131-
# puts " ... url #{fileName[(hasGit + 5)..(hasGitRef + 3)]} "
132-
# add +9 = length of .git?ref + 1
133-
inspecYML.write(" git: #{fileName[(hasGit + 5)..(hasGitRef + 3)]}\n")
134-
inspecYML.write(" tag: #{fileName[(hasGitRef + 9)..]}\n")
135-
inspecYML.write(" relative_path: test/integration/#{myEnv}\n")
136-
else
137-
inspecYML.write(" path: ../../../#{fileName}\n")
138-
end
137+
next unless isBP
138+
139+
# write blueprints.rb
140+
allBPsRB.write("include_controls '#{moduleOut}'\n")
141+
142+
# write inspec.yml
143+
inspecYML.write(" - name: #{moduleOut}\n")
144+
145+
if hasGit
146+
# puts " ... Tag #{fileName[(hasGitRef + 9)..]} "
147+
# puts " ... url #{fileName[(hasGit + 5)..(hasGitRef + 3)]} "
148+
# add +9 = length of .git?ref + 1
149+
inspecYML.write(" git: #{fileName[(hasGit + 5)..(hasGitRef + 3)]}\n")
150+
inspecYML.write(" tag: #{fileName[(hasGitRef + 9)..]}\n")
151+
inspecYML.write(" relative_path: test/integration/#{myEnv}\n")
152+
else
153+
inspecYML.write(" path: ../../../#{fileName}\n")
139154
end
140155
end
141156

142-
outputTF.close unless outputTF.nil?
143-
modoutTF.close unless modoutTF.nil?
144-
allBPsRB.close unless allBPsRB.nil?
157+
outputTF&.close
158+
modoutTF&.close
159+
allBPsRB&.close
145160
inspecYML.close unless allBPsRB.nil?
146161

147162
# pretty format modified tf files, so that a cyclic execution of terraform fmt is prohibited.
148-
TFFMT = "terraform fmt"
163+
TFFMT = 'terraform fmt'
149164
stdouttffmt, stderrtffmt, statustffmt = Open3.capture3(TFFMT)
150165
pp stdouttffmt
151166
pp stderrtffmt

ods-pre-commit-hooks-ruby.gemspec

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
# -*- encoding: utf-8 -*-
1+
# frozen_string_literal: true
22

33
Gem::Specification.new do |s|
4-
s.name = %q{ods-pre-commit-hooks-ruby}
5-
s.version = "0.0.1"
4+
s.required_ruby_version = '~> 2.6'
5+
s.name = 'ods-pre-commit-hooks-ruby'
6+
s.version = '0.0.2'
67

7-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8-
s.authors = ["Josef Hartmann / Erhard Wais"]
9-
s.email = %q{dontneedmoreemail@domain.com}
10-
s.files = Dir["./hooks/*.rb"]
8+
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
9+
s.authors = ['Josef Hartmann / Erhard Wais']
10+
s.email = 'dontneedmoreemail@domain.com'
11+
s.files = Dir['./hooks/*.rb']
1112
s.bindir = 'hooks'
12-
s.executables = s.files.grep(%r{^./hooks/}) { |f| File.basename(f) }
13-
s.require_paths = ["hooks"]
14-
s.extra_rdoc_files = ["README.md"]
15-
s.homepage = %q{https://github.com/nichtraunzer/ods-pre-commit-hooks}
16-
s.rdoc_options = ["--main", "README.md"]
17-
s.require_paths = ["lib"]
18-
s.rubygems_version = %q{1.3.7}
13+
s.executables = s.files.grep(%r{^./hooks/}) { |f| File.basename(f) }
14+
s.require_paths = ['hooks']
15+
s.extra_rdoc_files = ['README.md']
16+
s.homepage = 'https://github.com/nichtraunzer/ods-pre-commit-hooks'
17+
s.rdoc_options = ['--main', 'README.md']
18+
s.rubygems_version = '1.3.7'
1919
s.license = 'Apache-2.0'
20-
s.summary = "ruby pre-commit hooks for the ods ecosystem."
21-
s.description = "A git pre-commit hook written in ruby."
20+
s.summary = 'ruby pre-commit hooks for the ods ecosystem.'
21+
s.description = 'A git pre-commit hook written in ruby.'
2222

2323
s.add_development_dependency('benchmark-ips', '~> 0.1')
2424
s.add_development_dependency('minitest', '~> 5.0')
2525
s.add_development_dependency('minitest-reporters', '~> 1.0')
2626
s.add_development_dependency('rake', '~> 10.0')
27-
s.add_development_dependency('rubocop', '~> 0.49')
27+
s.add_development_dependency('rubocop', '~> 1.9')
2828

29-
if s.respond_to? :specification_version then
30-
s.specification_version = 3
31-
end
29+
s.specification_version = 3 if s.respond_to? :specification_version
3230
end

0 commit comments

Comments
 (0)