pdk 1.15.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +168 -10
  3. data/README.md +1 -1
  4. data/lib/pdk.rb +28 -19
  5. data/lib/pdk/answer_file.rb +2 -93
  6. data/lib/pdk/bolt.rb +19 -0
  7. data/lib/pdk/cli.rb +7 -6
  8. data/lib/pdk/cli/bundle.rb +5 -1
  9. data/lib/pdk/cli/config.rb +3 -1
  10. data/lib/pdk/cli/config/get.rb +3 -1
  11. data/lib/pdk/cli/convert.rb +7 -9
  12. data/lib/pdk/cli/exec/command.rb +45 -4
  13. data/lib/pdk/cli/exec_group.rb +78 -43
  14. data/lib/pdk/cli/get.rb +20 -0
  15. data/lib/pdk/cli/get/config.rb +24 -0
  16. data/lib/pdk/cli/new.rb +2 -0
  17. data/lib/pdk/cli/new/class.rb +2 -2
  18. data/lib/pdk/cli/new/defined_type.rb +2 -2
  19. data/lib/pdk/cli/new/fact.rb +29 -0
  20. data/lib/pdk/cli/new/function.rb +29 -0
  21. data/lib/pdk/cli/new/provider.rb +2 -2
  22. data/lib/pdk/cli/new/task.rb +2 -2
  23. data/lib/pdk/cli/new/test.rb +2 -2
  24. data/lib/pdk/cli/new/transport.rb +2 -2
  25. data/lib/pdk/cli/release/publish.rb +11 -1
  26. data/lib/pdk/cli/remove.rb +20 -0
  27. data/lib/pdk/cli/remove/config.rb +80 -0
  28. data/lib/pdk/cli/set.rb +20 -0
  29. data/lib/pdk/cli/set/config.rb +119 -0
  30. data/lib/pdk/cli/update.rb +6 -8
  31. data/lib/pdk/cli/util.rb +7 -3
  32. data/lib/pdk/cli/util/option_validator.rb +6 -0
  33. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  34. data/lib/pdk/cli/validate.rb +26 -44
  35. data/lib/pdk/config.rb +264 -7
  36. data/lib/pdk/config/ini_file.rb +183 -0
  37. data/lib/pdk/config/ini_file_setting.rb +39 -0
  38. data/lib/pdk/config/namespace.rb +25 -5
  39. data/lib/pdk/config/setting.rb +3 -2
  40. data/lib/pdk/context.rb +99 -0
  41. data/lib/pdk/context/control_repo.rb +60 -0
  42. data/lib/pdk/context/module.rb +28 -0
  43. data/lib/pdk/context/none.rb +22 -0
  44. data/lib/pdk/control_repo.rb +90 -0
  45. data/lib/pdk/generate/defined_type.rb +25 -32
  46. data/lib/pdk/generate/fact.rb +25 -0
  47. data/lib/pdk/generate/function.rb +48 -0
  48. data/lib/pdk/generate/module.rb +33 -20
  49. data/lib/pdk/generate/provider.rb +15 -64
  50. data/lib/pdk/generate/puppet_class.rb +25 -31
  51. data/lib/pdk/generate/puppet_object.rb +84 -188
  52. data/lib/pdk/generate/task.rb +28 -46
  53. data/lib/pdk/generate/transport.rb +20 -74
  54. data/lib/pdk/module.rb +1 -1
  55. data/lib/pdk/module/build.rb +23 -0
  56. data/lib/pdk/module/convert.rb +58 -34
  57. data/lib/pdk/module/metadata.rb +6 -2
  58. data/lib/pdk/module/release.rb +3 -9
  59. data/lib/pdk/module/update.rb +8 -12
  60. data/lib/pdk/module/update_manager.rb +7 -0
  61. data/lib/pdk/report.rb +3 -3
  62. data/lib/pdk/report/event.rb +8 -2
  63. data/lib/pdk/template.rb +59 -0
  64. data/lib/pdk/template/fetcher.rb +98 -0
  65. data/lib/pdk/template/fetcher/git.rb +85 -0
  66. data/lib/pdk/template/fetcher/local.rb +28 -0
  67. data/lib/pdk/template/renderer.rb +96 -0
  68. data/lib/pdk/template/renderer/v1.rb +25 -0
  69. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  70. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  71. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  72. data/lib/pdk/template/template_dir.rb +67 -0
  73. data/lib/pdk/tests/unit.rb +8 -1
  74. data/lib/pdk/util.rb +38 -39
  75. data/lib/pdk/util/bundler.rb +2 -1
  76. data/lib/pdk/util/changelog_generator.rb +11 -2
  77. data/lib/pdk/util/env.rb +28 -11
  78. data/lib/pdk/util/json_finder.rb +84 -0
  79. data/lib/pdk/util/puppet_strings.rb +3 -3
  80. data/lib/pdk/util/puppet_version.rb +2 -2
  81. data/lib/pdk/util/ruby_version.rb +11 -4
  82. data/lib/pdk/util/template_uri.rb +17 -14
  83. data/lib/pdk/util/vendored_file.rb +1 -2
  84. data/lib/pdk/validate.rb +79 -25
  85. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  86. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  87. data/lib/pdk/validate/external_command_validator.rb +208 -0
  88. data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
  89. data/lib/pdk/validate/invokable_validator.rb +220 -0
  90. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
  91. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
  92. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  93. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
  94. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  95. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
  96. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
  97. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
  98. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  99. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
  100. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
  101. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  102. data/lib/pdk/validate/validator.rb +118 -0
  103. data/lib/pdk/validate/validator_group.rb +104 -0
  104. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
  105. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  106. data/lib/pdk/version.rb +1 -1
  107. data/locales/pdk.pot +513 -329
  108. metadata +76 -32
  109. data/lib/pdk/module/template_dir.rb +0 -115
  110. data/lib/pdk/module/template_dir/base.rb +0 -268
  111. data/lib/pdk/module/template_dir/git.rb +0 -91
  112. data/lib/pdk/module/template_dir/local.rb +0 -21
  113. data/lib/pdk/template_file.rb +0 -96
  114. data/lib/pdk/validate/base_validator.rb +0 -215
  115. data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -82
  116. data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -111
  117. data/lib/pdk/validate/metadata_validator.rb +0 -26
  118. data/lib/pdk/validate/puppet/puppet_epp.rb +0 -135
  119. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
  120. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -135
  121. data/lib/pdk/validate/puppet_validator.rb +0 -26
  122. data/lib/pdk/validate/ruby/rubocop.rb +0 -72
  123. data/lib/pdk/validate/ruby_validator.rb +0 -26
  124. data/lib/pdk/validate/tasks/metadata_lint.rb +0 -130
  125. data/lib/pdk/validate/tasks/name.rb +0 -90
  126. data/lib/pdk/validate/tasks_validator.rb +0 -29
  127. data/lib/pdk/validate/yaml/syntax.rb +0 -125
  128. data/lib/pdk/validate/yaml_validator.rb +0 -28
@@ -44,7 +44,7 @@ module PDK
44
44
  # If there are missing dependencies after updating the lockfile, let `bundle install`
45
45
  # go out and get them. If the specified puppet gem version points to a remote location
46
46
  # or local filepath, then run bundle install as well.
47
- if !bundle.installed? || (gem_overrides[:puppet] && gem_overrides[:puppet].start_with?('file://', 'git://', 'https://'))
47
+ if !bundle.installed?(gem_overrides) || (gem_overrides[:puppet] && gem_overrides[:puppet].start_with?('file://', 'git://', 'https://'))
48
48
  bundle.install!(gem_overrides)
49
49
  end
50
50
 
@@ -210,6 +210,7 @@ module PDK
210
210
  end
211
211
 
212
212
  def binstubs!(gems)
213
+ raise PDK::CLI::FatalError, _('Unable to install requested binstubs as the Gemfile is missing') if gemfile.nil?
213
214
  binstub_dir = File.join(File.dirname(gemfile), 'bin')
214
215
  return true if gems.all? { |gem| PDK::Util::Filesystem.file?(File.join(binstub_dir, gem)) }
215
216
 
@@ -5,11 +5,20 @@ module PDK
5
5
  module ChangelogGenerator
6
6
  # Taken from the version regex in https://forgeapi.puppet.com/schemas/module.json
7
7
  VERSION_REGEX = %r{^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$}
8
+ GEM = 'github_changelog_generator'.freeze
8
9
 
9
10
  # Raises if the github_changelog_generator is not available
10
11
  def self.github_changelog_generator_available!
11
- require 'bundler'
12
- raise PDK::CLI::ExitWithError, _('Unable to generate the changelog as the github_changelog_generator gem is not installed') unless Bundler.rubygems.find_name('github_changelog_generator').any?
12
+ check_command = PDK::CLI::Exec::InteractiveCommand.new(PDK::CLI::Exec.bundle_bin, 'show', 'github_changelog_generator')
13
+ check_command.context = :module
14
+
15
+ result = check_command.execute!
16
+
17
+ return if result[:exit_code].zero?
18
+
19
+ raise PDK::CLI::ExitWithError, _(
20
+ 'Unable to generate the changelog as the %{gem} gem is not included in this module\'s Gemfile',
21
+ ) % { gem: GEM }
13
22
  end
14
23
 
15
24
  # Runs the Changelog Generator gem (in the module's context) to automatically create a CHANGLELOG.MD file
data/lib/pdk/util/env.rb CHANGED
@@ -4,25 +4,42 @@ require 'forwardable'
4
4
  module PDK
5
5
  module Util
6
6
  class Env
7
- class << self
7
+ class WindowsEnv
8
8
  extend Forwardable
9
9
 
10
- def_delegators :env_hash, :[], :key?, :fetch, :select, :reject
10
+ # Note, these delegators may not have case insensitive keys
11
+ def_delegators :env_hash, :fetch, :select, :reject
11
12
 
12
13
  def []=(key, value)
13
- if Gem.win_platform?
14
- PDK::Util::Windows::Process.set_environment_variable(key, value)
15
- else
16
- ENV[key] = value
14
+ PDK::Util::Windows::Process.set_environment_variable(key, value)
15
+ end
16
+
17
+ def key?(key)
18
+ !env_hash.keys.find { |item| key.casecmp(item).zero? }.nil?
19
+ end
20
+
21
+ def [](key)
22
+ env_hash.each do |item, value|
23
+ next unless key.casecmp(item).zero?
24
+ return value
17
25
  end
26
+ nil
18
27
  end
19
28
 
29
+ private
30
+
20
31
  def env_hash
21
- if Gem.win_platform?
22
- PDK::Util::Windows::Process.environment_hash
23
- else
24
- ENV
25
- end
32
+ PDK::Util::Windows::Process.environment_hash
33
+ end
34
+ end
35
+
36
+ class << self
37
+ extend Forwardable
38
+
39
+ def_delegators :implementation, :key?, :[], :[]=, :fetch, :select, :reject
40
+
41
+ def implementation
42
+ @implementation ||= Gem.win_platform? ? WindowsEnv.new : ENV
26
43
  end
27
44
  end
28
45
  end
@@ -0,0 +1,84 @@
1
+ module PDK
2
+ module Util
3
+ # Processes a given string, looking for JSON objects and parsing them.
4
+ #
5
+ # @example A string with a JSON object and some junk characters
6
+ # PDK::Util::JSONFinder.new('foo{"bar":1}').objects
7
+ # => [{ 'bar' => 1 }]
8
+ #
9
+ # @example A string with mulitple JSON objects
10
+ # PDK::Util::JSONFinder.new('foo{"bar":1}baz{"gronk":2}').objects
11
+ # => [{ 'bar' => 1 }, { 'gronk' => 2 }]
12
+ class JSONFinder
13
+ # Creates a new instance of PDK::Util::JSONFinder.
14
+ #
15
+ # @param string [String] the string to find JSON objects inside of.
16
+ #
17
+ # @return [PDK::Util::JSONFinder] a new PDK::Util::JSONFinder object.
18
+ def initialize(string)
19
+ require 'strscan'
20
+
21
+ @scanner = StringScanner.new(string)
22
+ end
23
+
24
+ # Returns the parsed JSON objects from the string.
25
+ #
26
+ # @return [Array[Hash]] the parsed JSON objects present in the string.
27
+ def objects
28
+ return @objects unless @objects.nil?
29
+
30
+ require 'json'
31
+
32
+ until @scanner.eos?
33
+ @scanner.getch until @scanner.peek(1) == '{' || @scanner.eos?
34
+
35
+ (@objects ||= []) << begin
36
+ JSON.parse(read_object(true) || '')
37
+ rescue JSON::ParserError
38
+ nil
39
+ end
40
+ end
41
+
42
+ @objects = @objects.compact
43
+ end
44
+
45
+ private
46
+
47
+ # Recursively process the string to extract a complete JSON object.
48
+ #
49
+ # @param new_object [Boolean] Set to true if processing a new object to
50
+ # capture the opening brace. Set to false if being called recursively
51
+ # where the opening brace has already been captured.
52
+ #
53
+ # @return [String] The matched substring containing a JSON object.
54
+ def read_object(new_object = false)
55
+ matched_text = new_object ? @scanner.getch : ''
56
+
57
+ until @scanner.eos?
58
+ text = @scanner.scan_until(%r{(?:(?<!\\)"|\{|\})})
59
+ unless text
60
+ @scanner.terminate
61
+ return nil
62
+ end
63
+ matched_text += text
64
+
65
+ case @scanner.matched
66
+ when '}'
67
+ break
68
+ when '"'
69
+ text = @scanner.scan_until(%r{(?<!\\)"})
70
+ unless text
71
+ @scanner.terminate
72
+ return nil
73
+ end
74
+ matched_text += text
75
+ else
76
+ matched_text += read_object
77
+ end
78
+ end
79
+
80
+ matched_text
81
+ end
82
+ end
83
+ end
84
+ end
@@ -92,12 +92,12 @@ module PDK
92
92
  require 'pdk/generate'
93
93
 
94
94
  generators = PDK::Generate.generators.select do |gen|
95
- gen.respond_to?(:puppet_strings_type) && !gen.puppet_strings_type.nil?
95
+ gen.const_defined?(:PUPPET_STRINGS_TYPE) && !gen::PUPPET_STRINGS_TYPE.nil?
96
96
  end
97
97
 
98
98
  known_objects = generate_hash
99
99
 
100
- generators.map { |gen| [gen, known_objects[gen.puppet_strings_type]] }.reject do |_, obj|
100
+ generators.map { |gen| [gen, known_objects[gen::PUPPET_STRINGS_TYPE]] }.reject do |_, obj|
101
101
  obj.nil? || obj.empty?
102
102
  end
103
103
  end
@@ -117,7 +117,7 @@ module PDK
117
117
  require 'pdk/generate'
118
118
 
119
119
  PDK::Generate.generators.find do |gen|
120
- gen.respond_to?(:puppet_strings_type) && gen.puppet_strings_type == type
120
+ gen.const_defined?(:PUPPET_STRINGS_TYPE) && gen::PUPPET_STRINGS_TYPE == type
121
121
  end
122
122
  end
123
123
  end
@@ -17,7 +17,7 @@ module PDK
17
17
 
18
18
  PE_VERSIONS_URL = 'https://forgeapi.puppet.com/private/versions/pe'.freeze
19
19
  DEFAULT_PUPPET_DEV_URL = 'https://github.com/puppetlabs/puppet'.freeze
20
- DEFAULT_PUPPET_DEV_BRANCH = 'master'.freeze
20
+ DEFAULT_PUPPET_DEV_BRANCH = 'main'.freeze
21
21
 
22
22
  def puppet_dev_env
23
23
  require 'pdk/util/ruby_version'
@@ -80,7 +80,7 @@ module PDK
80
80
  end
81
81
 
82
82
  # Reset local repo to latest
83
- reset_result = PDK::Util::Git.git('-C', puppet_dev_path, 'reset', '--hard', 'origin/master')
83
+ reset_result = PDK::Util::Git.git('-C', puppet_dev_path, 'reset', '--hard', "origin/#{DEFAULT_PUPPET_DEV_BRANCH}")
84
84
 
85
85
  @puppet_dev_fetched = true
86
86
  return if reset_result[:exit_code].zero?
@@ -60,7 +60,11 @@ module PDK
60
60
  #
61
61
  # PDK::Util::PuppetVersion.find_gem_for('5.5.10')[:ruby_version]
62
62
  #
63
- PDK::Util::PuppetVersion.latest_available[:ruby_version]
63
+ # For using the latest puppet gem:
64
+ # PDK::Util::PuppetVersion.latest_available[:ruby_version]
65
+ #
66
+ # Temporarily lock to Ruby 2.5.x as default until 2.7.x ecosystem is sorted
67
+ versions.keys.detect { |v| v =~ %r{^2\.5} }
64
68
  else
65
69
  # TODO: may not be a safe assumption that highest available version should be default
66
70
  # WARNING Do NOT use PDK::Util::PuppetVersion.*** methods as it can recurse into this
@@ -115,9 +119,7 @@ module PDK
115
119
  else
116
120
  # This allows the subprocess to find the 'bundler' gem, which isn't
117
121
  # in GEM_HOME for gem installs.
118
- # TODO: There must be a better way to do this than shelling out to
119
- # gem... Perhaps can be replaced with "Gem.path"?
120
- [File.absolute_path(File.join(`gem which bundler`, '..', '..', '..', '..'))]
122
+ [File.absolute_path(File.join(bundler_basedir, '..', '..', '..'))]
121
123
  end
122
124
  end
123
125
 
@@ -128,6 +130,7 @@ module PDK
128
130
  def gem_home
129
131
  require 'pdk/util'
130
132
 
133
+ # TODO: bundle install --path is deprecated
131
134
  # `bundle install --path` ignores all "system" installed gems and
132
135
  # causes unnecessary package installs. `bundle install` (without
133
136
  # --path) installs into GEM_HOME, which by default is non-user
@@ -167,6 +170,10 @@ module PDK
167
170
  def versions
168
171
  self.class.versions
169
172
  end
173
+
174
+ def bundler_basedir
175
+ Gem::Specification.latest_specs.find { |spec| spec.name.eql?('bundler') }.base_dir
176
+ end
170
177
  end
171
178
  end
172
179
  end
@@ -26,13 +26,13 @@ module PDK
26
26
  #
27
27
  # file:///c:/foo (git clone location)
28
28
  # c:/foo (shell paths)
29
- # file:///c:/foo#master (only for metadata)
30
- # c:/foo#master (only for metadata)
29
+ # file:///c:/foo#main (only for metadata)
30
+ # c:/foo#main (only for metadata)
31
31
  #
32
32
  # non output formats:
33
33
  #
34
34
  # /c:/foo (internal use only)
35
- # /c:/foo#master (internal use only)
35
+ # /c:/foo#main (internal use only)
36
36
  #
37
37
  def initialize(opts_or_uri)
38
38
  require 'addressable'
@@ -94,6 +94,10 @@ module PDK
94
94
  bare_uri == PDK::Util::TemplateURI.bare_uri(PDK::Util::TemplateURI.default_template_addressable_uri)
95
95
  end
96
96
 
97
+ def default_ref?
98
+ uri_fragment == self.class.default_template_ref(self)
99
+ end
100
+
97
101
  def puppetlabs_template?
98
102
  self.class.packaged_template?(bare_uri) || bare_uri == PDK_TEMPLATE_URL
99
103
  end
@@ -188,7 +192,6 @@ module PDK
188
192
  # directory, and :allow_fallback contains a Boolean that specifies if
189
193
  # the lookup process should proceed to the next template directory if
190
194
  # the template file is not in this template directory.
191
- #
192
195
  def self.templates(opts)
193
196
  require 'pdk/answer_file'
194
197
  require 'pdk/util'
@@ -214,10 +217,11 @@ module PDK
214
217
  else
215
218
  nil
216
219
  end
217
- answers_uri = if [PACKAGED_TEMPLATE_KEYWORD, DEPRECATED_TEMPLATE_URL].include?(PDK.answers['template-url'])
220
+ default_template_url = PDK.config.get_within_scopes('module_defaults.template-url')
221
+ answers_uri = if [PACKAGED_TEMPLATE_KEYWORD, DEPRECATED_TEMPLATE_URL].include?(default_template_url)
218
222
  Addressable::URI.parse(default_template_uri)
219
- elsif PDK.answers['template-url']
220
- new(uri_safe(PDK.answers['template-url'])).uri
223
+ elsif default_template_url
224
+ new(uri_safe(default_template_url)).uri
221
225
  else
222
226
  nil
223
227
  end
@@ -237,11 +241,11 @@ module PDK
237
241
  require 'pdk/util'
238
242
  require 'pdk/version'
239
243
 
240
- return 'master' if PDK::Util.development_mode?
244
+ return 'main' if PDK::Util.development_mode?
241
245
  return PDK::TEMPLATE_REF if uri.nil?
242
246
 
243
247
  uri = new(uri) unless uri.is_a?(self)
244
- uri.default? ? PDK::TEMPLATE_REF : 'master'
248
+ uri.default? ? PDK::TEMPLATE_REF : 'main'
245
249
  end
246
250
 
247
251
  # @returns Addressable::URI
@@ -256,21 +260,20 @@ module PDK
256
260
  found_template[:uri]
257
261
  end
258
262
 
259
- def self.valid_template?(template)
263
+ def self.valid_template?(template, context = PDK.context)
260
264
  require 'addressable'
261
265
 
262
266
  return false if template.nil? || !template.is_a?(Hash)
263
267
  return false if template[:uri].nil? || !template[:uri].is_a?(Addressable::URI)
264
268
 
265
269
  return true if PDK::Util::Git.repo?(bare_uri(template[:uri]))
266
-
267
270
  path = human_readable(template[:uri].path)
268
271
  if PDK::Util::Filesystem.directory?(path)
269
272
  # We know that it's not a git repository, but it's a valid path on disk
270
273
  begin
271
- PDK::Module::TemplateDir.validate_module_template!(path)
272
- return true
273
- rescue ArgumentError
274
+ renderer = PDK::Template::Renderer.instance(path, template[:uri], context)
275
+ return !renderer.nil?
276
+ rescue StandardError
274
277
  nil
275
278
  end
276
279
  end
@@ -55,8 +55,7 @@ module PDK
55
55
  uri = URI.parse(url)
56
56
  http = Net::HTTP.new(uri.host, uri.port)
57
57
  http.use_ssl = true
58
- # TODO: Get rid of this, possible workaround:
59
- # https://github.com/glennsarti/dev-tools/blob/master/RubyCerts.ps1
58
+ # TODO: Get rid of this
60
59
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if Gem.win_platform?
61
60
  request = Net::HTTP::Get.new(uri.request_uri)
62
61
  response = http.request(request)
data/lib/pdk/validate.rb CHANGED
@@ -2,37 +2,91 @@ require 'pdk'
2
2
 
3
3
  module PDK
4
4
  module Validate
5
- # TODO: Fix validator namespacing
6
- autoload :BaseValidator, 'pdk/validate/base_validator'
7
- autoload :MetadataJSONLint, 'pdk/validate/metadata/metadata_json_lint'
8
- autoload :MetadataSyntax, 'pdk/validate/metadata/metadata_syntax'
9
- autoload :MetadataValidator, 'pdk/validate/metadata_validator'
10
- autoload :PuppetEPP, 'pdk/validate/puppet/puppet_epp'
11
- autoload :PuppetLint, 'pdk/validate/puppet/puppet_lint'
12
- autoload :PuppetSyntax, 'pdk/validate/puppet/puppet_syntax'
13
- autoload :PuppetValidator, 'pdk/validate/puppet_validator'
14
- autoload :Rubocop, 'pdk/validate/ruby/rubocop'
15
- autoload :RubyValidator, 'pdk/validate/ruby_validator'
16
- autoload :TasksValidator, 'pdk/validate/tasks_validator'
17
- autoload :YAMLValidator, 'pdk/validate/yaml_validator'
5
+ autoload :ExternalCommandValidator, 'pdk/validate/external_command_validator'
6
+ autoload :InternalRubyValidator, 'pdk/validate/internal_ruby_validator'
7
+ autoload :InvokableValidator, 'pdk/validate/invokable_validator'
8
+ autoload :Validator, 'pdk/validate/validator'
9
+ autoload :ValidatorGroup, 'pdk/validate/validator_group'
18
10
 
19
- class Tasks
20
- autoload :Name, 'pdk/validate/tasks/name'
21
- autoload :MetadataLint, 'pdk/validate/tasks/metadata_lint'
11
+ module ControlRepo
12
+ autoload :ControlRepoValidatorGroup, 'pdk/validate/control_repo/control_repo_validator_group'
13
+ autoload :EnvironmentConfValidator, 'pdk/validate/control_repo/environment_conf_validator'
22
14
  end
23
15
 
24
- class YAML
25
- autoload :Syntax, 'pdk/validate/yaml/syntax'
16
+ module Metadata
17
+ autoload :MetadataJSONLintValidator, 'pdk/validate/metadata/metadata_json_lint_validator'
18
+ autoload :MetadataSyntaxValidator, 'pdk/validate/metadata/metadata_syntax_validator'
19
+ autoload :MetadataValidatorGroup, 'pdk/validate/metadata/metadata_validator_group'
20
+ end
21
+
22
+ module Puppet
23
+ autoload :PuppetEPPValidator, 'pdk/validate/puppet/puppet_epp_validator'
24
+ autoload :PuppetLintValidator, 'pdk/validate/puppet/puppet_lint_validator'
25
+ autoload :PuppetSyntaxValidator, 'pdk/validate/puppet/puppet_syntax_validator'
26
+ autoload :PuppetValidatorGroup, 'pdk/validate/puppet/puppet_validator_group'
27
+ end
28
+
29
+ module Ruby
30
+ autoload :RubyRubocopValidator, 'pdk/validate/ruby/ruby_rubocop_validator'
31
+ autoload :RubyValidatorGroup, 'pdk/validate/ruby/ruby_validator_group'
32
+ end
33
+
34
+ module Tasks
35
+ autoload :TasksMetadataLintValidator, 'pdk/validate/tasks/tasks_metadata_lint_validator'
36
+ autoload :TasksNameValidator, 'pdk/validate/tasks/tasks_name_validator'
37
+ autoload :TasksValidatorGroup, 'pdk/validate/tasks/tasks_validator_group'
38
+ end
39
+
40
+ module YAML
41
+ autoload :YAMLSyntaxValidator, 'pdk/validate/yaml/yaml_syntax_validator'
42
+ autoload :YAMLValidatorGroup, 'pdk/validate/yaml/yaml_validator_group'
26
43
  end
27
44
 
28
45
  def self.validators
29
- @validators ||= [
30
- MetadataValidator,
31
- YAMLValidator,
32
- PuppetValidator,
33
- RubyValidator,
34
- TasksValidator,
35
- ].freeze
46
+ validator_hash.values
47
+ end
48
+
49
+ def self.validator_names
50
+ validator_hash.keys
51
+ end
52
+
53
+ # @api private
54
+ def self.validator_hash
55
+ # TODO: This isn't the most performant... But with only 6 items, it's fine
56
+ @validator_hash ||= [
57
+ ControlRepo::ControlRepoValidatorGroup,
58
+ Metadata::MetadataValidatorGroup,
59
+ Puppet::PuppetValidatorGroup,
60
+ Ruby::RubyValidatorGroup,
61
+ Tasks::TasksValidatorGroup,
62
+ YAML::YAMLValidatorGroup,
63
+ ].map { |klass| [klass.new.name, klass] }.to_h.freeze
64
+ end
65
+
66
+ def self.invoke_validators_by_name(context, names, parallel = false, options = {})
67
+ instances = names.select { |name| validator_names.include?(name) }
68
+ .map { |name| validator_hash[name].new(context, options) }
69
+ .select { |instance| instance.valid_in_context? }
70
+ .each { |instance| instance.prepare_invoke! }
71
+ report = PDK::Report.new
72
+
73
+ # Nothing to validate then nothing to do.
74
+ return [0, report] if instances.empty?
75
+
76
+ require 'pdk/cli/exec_group'
77
+ exec_group = PDK::CLI::ExecGroup.create(
78
+ _('Validating module using %{num_of_threads} threads' % { num_of_threads: instances.count }),
79
+ { parallel: parallel },
80
+ options,
81
+ )
82
+
83
+ instances.each do |validator|
84
+ exec_group.register do
85
+ validator.invoke(report)
86
+ end
87
+ end
88
+
89
+ [exec_group.exit_code, report]
36
90
  end
37
91
 
38
92
  class ParseOutputError < StandardError; end