pdk 1.9.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +744 -711
  3. data/README.md +23 -21
  4. data/lib/pdk/answer_file.rb +3 -112
  5. data/lib/pdk/bolt.rb +20 -0
  6. data/lib/pdk/cli/build.rb +51 -54
  7. data/lib/pdk/cli/bundle.rb +33 -29
  8. data/lib/pdk/cli/console.rb +148 -0
  9. data/lib/pdk/cli/convert.rb +46 -37
  10. data/lib/pdk/cli/env.rb +51 -0
  11. data/lib/pdk/cli/errors.rb +4 -3
  12. data/lib/pdk/cli/exec/command.rb +285 -0
  13. data/lib/pdk/cli/exec/interactive_command.rb +109 -0
  14. data/lib/pdk/cli/exec.rb +32 -201
  15. data/lib/pdk/cli/exec_group.rb +79 -43
  16. data/lib/pdk/cli/get/config.rb +26 -0
  17. data/lib/pdk/cli/get.rb +22 -0
  18. data/lib/pdk/cli/new/class.rb +20 -22
  19. data/lib/pdk/cli/new/defined_type.rb +21 -21
  20. data/lib/pdk/cli/new/fact.rb +27 -0
  21. data/lib/pdk/cli/new/function.rb +27 -0
  22. data/lib/pdk/cli/new/module.rb +40 -29
  23. data/lib/pdk/cli/new/provider.rb +18 -18
  24. data/lib/pdk/cli/new/task.rb +23 -22
  25. data/lib/pdk/cli/new/test.rb +52 -0
  26. data/lib/pdk/cli/new/transport.rb +27 -0
  27. data/lib/pdk/cli/new.rb +15 -9
  28. data/lib/pdk/cli/release/prep.rb +39 -0
  29. data/lib/pdk/cli/release/publish.rb +46 -0
  30. data/lib/pdk/cli/release.rb +185 -0
  31. data/lib/pdk/cli/remove/config.rb +83 -0
  32. data/lib/pdk/cli/remove.rb +22 -0
  33. data/lib/pdk/cli/set/config.rb +121 -0
  34. data/lib/pdk/cli/set.rb +22 -0
  35. data/lib/pdk/cli/test/unit.rb +71 -69
  36. data/lib/pdk/cli/test.rb +9 -8
  37. data/lib/pdk/cli/update.rb +38 -21
  38. data/lib/pdk/cli/util/command_redirector.rb +13 -3
  39. data/lib/pdk/cli/util/interview.rb +25 -9
  40. data/lib/pdk/cli/util/option_normalizer.rb +6 -6
  41. data/lib/pdk/cli/util/option_validator.rb +19 -9
  42. data/lib/pdk/cli/util/spinner.rb +13 -0
  43. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  44. data/lib/pdk/cli/util.rb +105 -48
  45. data/lib/pdk/cli/validate.rb +96 -111
  46. data/lib/pdk/cli.rb +134 -87
  47. data/lib/pdk/config/errors.rb +5 -0
  48. data/lib/pdk/config/ini_file.rb +184 -0
  49. data/lib/pdk/config/ini_file_setting.rb +35 -0
  50. data/lib/pdk/config/json.rb +35 -0
  51. data/lib/pdk/config/json_schema_namespace.rb +137 -0
  52. data/lib/pdk/config/json_schema_setting.rb +51 -0
  53. data/lib/pdk/config/json_with_schema.rb +47 -0
  54. data/lib/pdk/config/namespace.rb +362 -0
  55. data/lib/pdk/config/setting.rb +134 -0
  56. data/lib/pdk/config/task_schema.json +116 -0
  57. data/lib/pdk/config/validator.rb +31 -0
  58. data/lib/pdk/config/yaml.rb +41 -0
  59. data/lib/pdk/config/yaml_with_schema.rb +51 -0
  60. data/lib/pdk/config.rb +304 -0
  61. data/lib/pdk/context/control_repo.rb +61 -0
  62. data/lib/pdk/context/module.rb +28 -0
  63. data/lib/pdk/context/none.rb +22 -0
  64. data/lib/pdk/context.rb +98 -0
  65. data/lib/pdk/control_repo.rb +89 -0
  66. data/lib/pdk/generate/defined_type.rb +27 -33
  67. data/lib/pdk/generate/fact.rb +26 -0
  68. data/lib/pdk/generate/function.rb +49 -0
  69. data/lib/pdk/generate/module.rb +160 -153
  70. data/lib/pdk/generate/provider.rb +16 -69
  71. data/lib/pdk/generate/puppet_class.rb +27 -32
  72. data/lib/pdk/generate/puppet_object.rb +100 -159
  73. data/lib/pdk/generate/task.rb +34 -51
  74. data/lib/pdk/generate/transport.rb +34 -0
  75. data/lib/pdk/generate.rb +21 -8
  76. data/lib/pdk/logger.rb +24 -6
  77. data/lib/pdk/module/build.rb +125 -37
  78. data/lib/pdk/module/convert.rb +146 -65
  79. data/lib/pdk/module/metadata.rb +72 -71
  80. data/lib/pdk/module/release.rb +255 -0
  81. data/lib/pdk/module/update.rb +48 -37
  82. data/lib/pdk/module/update_manager.rb +75 -39
  83. data/lib/pdk/module.rb +10 -2
  84. data/lib/pdk/monkey_patches.rb +268 -0
  85. data/lib/pdk/report/event.rb +36 -48
  86. data/lib/pdk/report.rb +35 -22
  87. data/lib/pdk/template/fetcher/git.rb +84 -0
  88. data/lib/pdk/template/fetcher/local.rb +29 -0
  89. data/lib/pdk/template/fetcher.rb +100 -0
  90. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +108 -0
  91. data/lib/pdk/template/renderer/v1/renderer.rb +131 -0
  92. data/lib/pdk/template/renderer/v1/template_file.rb +100 -0
  93. data/lib/pdk/template/renderer/v1.rb +25 -0
  94. data/lib/pdk/template/renderer.rb +97 -0
  95. data/lib/pdk/template/template_dir.rb +67 -0
  96. data/lib/pdk/template.rb +52 -0
  97. data/lib/pdk/tests/unit.rb +101 -51
  98. data/lib/pdk/util/bundler.rb +44 -42
  99. data/lib/pdk/util/changelog_generator.rb +138 -0
  100. data/lib/pdk/util/env.rb +48 -0
  101. data/lib/pdk/util/filesystem.rb +139 -2
  102. data/lib/pdk/util/git.rb +108 -8
  103. data/lib/pdk/util/json_finder.rb +86 -0
  104. data/lib/pdk/util/puppet_strings.rb +125 -0
  105. data/lib/pdk/util/puppet_version.rb +71 -87
  106. data/lib/pdk/util/ruby_version.rb +49 -25
  107. data/lib/pdk/util/template_uri.rb +283 -0
  108. data/lib/pdk/util/vendored_file.rb +34 -42
  109. data/lib/pdk/util/version.rb +11 -10
  110. data/lib/pdk/util/windows/api_types.rb +74 -44
  111. data/lib/pdk/util/windows/file.rb +31 -27
  112. data/lib/pdk/util/windows/process.rb +74 -0
  113. data/lib/pdk/util/windows/string.rb +19 -12
  114. data/lib/pdk/util/windows.rb +2 -0
  115. data/lib/pdk/util.rb +111 -124
  116. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  117. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  118. data/lib/pdk/validate/external_command_validator.rb +213 -0
  119. data/lib/pdk/validate/internal_ruby_validator.rb +101 -0
  120. data/lib/pdk/validate/invokable_validator.rb +238 -0
  121. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +84 -0
  122. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +76 -0
  123. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  124. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +131 -0
  125. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  126. data/lib/pdk/validate/puppet/puppet_plan_syntax_validator.rb +38 -0
  127. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +135 -0
  128. data/lib/pdk/validate/puppet/puppet_validator_group.rb +22 -0
  129. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +79 -0
  130. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  131. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +83 -0
  132. data/lib/pdk/validate/tasks/tasks_name_validator.rb +45 -0
  133. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  134. data/lib/pdk/validate/validator.rb +120 -0
  135. data/lib/pdk/validate/validator_group.rb +107 -0
  136. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +86 -0
  137. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  138. data/lib/pdk/validate.rb +86 -12
  139. data/lib/pdk/version.rb +2 -2
  140. data/lib/pdk.rb +60 -10
  141. metadata +138 -100
  142. data/lib/pdk/cli/module/build.rb +0 -14
  143. data/lib/pdk/cli/module/generate.rb +0 -45
  144. data/lib/pdk/cli/module.rb +0 -14
  145. data/lib/pdk/i18n.rb +0 -4
  146. data/lib/pdk/module/templatedir.rb +0 -321
  147. data/lib/pdk/template_file.rb +0 -95
  148. data/lib/pdk/validate/base_validator.rb +0 -215
  149. data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -86
  150. data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -109
  151. data/lib/pdk/validate/metadata_validator.rb +0 -30
  152. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -67
  153. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -112
  154. data/lib/pdk/validate/puppet_validator.rb +0 -30
  155. data/lib/pdk/validate/ruby/rubocop.rb +0 -77
  156. data/lib/pdk/validate/ruby_validator.rb +0 -29
  157. data/lib/pdk/validate/tasks/metadata_lint.rb +0 -126
  158. data/lib/pdk/validate/tasks/name.rb +0 -88
  159. data/lib/pdk/validate/tasks_validator.rb +0 -33
  160. data/lib/pdk/validate/yaml/syntax.rb +0 -109
  161. data/lib/pdk/validate/yaml_validator.rb +0 -31
  162. data/locales/config.yaml +0 -21
  163. data/locales/pdk.pot +0 -1291
@@ -0,0 +1,82 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module CLI
5
+ module Util
6
+ module UpdateManagerPrinter
7
+ # Prints the summary for a PDK::Module::UpdateManager Object
8
+ # @param update_manager [PDK::Module::UpdateManager] The object to print a summary of
9
+ # @param options [Hash{Object => Object}] A list of options when printing
10
+ # @option options [Boolean] :tense Whether to use future (:future) or past (:past) tense when printing the summary ("Files to be added" versus "Files added"). Default is :future
11
+ #
12
+ # @return [void]
13
+ def self.print_summary(update_manager, options = {})
14
+ require 'pdk/report'
15
+
16
+ options = {
17
+ tense: :future
18
+ }.merge(options)
19
+
20
+ footer = false
21
+
22
+ summary(update_manager).each do |category, files|
23
+ next if files.empty?
24
+
25
+ PDK::Report.default_target.puts('')
26
+ PDK::Report.default_target.puts(generate_banner("Files #{options[:tense] == :future ? 'to be ' : ''}#{category}", 40))
27
+ PDK::Report.default_target.puts(files.map(&:to_s).join("\n"))
28
+ footer = true
29
+ end
30
+
31
+ if footer # rubocop:disable Style/GuardClause No.
32
+ PDK::Report.default_target.puts('')
33
+ PDK::Report.default_target.puts(generate_banner('', 40))
34
+ end
35
+ end
36
+
37
+ # :nocov: Tested as part of the public methods
38
+ # Returns a hash, summarizing the contents of the Update Manager object
39
+ # @param update_manager [PDK::Module::UpdateManager] The object to create a summary of
40
+ #
41
+ # @return [Hash{Symbol => Array[String]}] A hash of each category and the file paths in each category
42
+ def self.summary(update_manager)
43
+ summary = {}
44
+ update_manager.changes.each do |category, update_category|
45
+ if update_category.respond_to?(:keys)
46
+ updated_files = update_category.keys
47
+ else
48
+ begin
49
+ updated_files = update_category.map { |file| file[:path] }
50
+ rescue TypeError
51
+ updated_files = update_category.to_a
52
+ end
53
+ end
54
+
55
+ summary[category] = updated_files
56
+ end
57
+
58
+ summary
59
+ end
60
+ private_class_method :summary
61
+
62
+ # Creates a line of text, with `text` centered in the middle
63
+ # @param text [String] The text to put in the middle of the banner
64
+ # @param width [Integer] The width of the banner in characters. Default is 80
65
+ # @return [String] The generated banner
66
+ def self.generate_banner(text, width = 80)
67
+ padding = width - text.length
68
+ banner = ''
69
+ padding_char = '-'
70
+
71
+ (padding / 2.0).ceil.times { banner << padding_char }
72
+ banner << text
73
+ (padding / 2.0).floor.times { banner << padding_char }
74
+
75
+ banner
76
+ end
77
+ private_class_method :generate_banner
78
+ # :nocov:
79
+ end
80
+ end
81
+ end
82
+ end
data/lib/pdk/cli/util.rb CHANGED
@@ -1,6 +1,15 @@
1
+ require 'pdk'
2
+
1
3
  module PDK
2
4
  module CLI
3
5
  module Util
6
+ autoload :CommandRedirector, 'pdk/cli/util/command_redirector'
7
+ autoload :OptionNormalizer, 'pdk/cli/util/option_normalizer'
8
+ autoload :OptionValidator, 'pdk/cli/util/option_validator'
9
+ autoload :Interview, 'pdk/cli/util/interview'
10
+ autoload :Spinner, 'pdk/cli/util/spinner'
11
+ autoload :UpdateManagerPrinter, 'pdk/cli/util/update_manager_printer'
12
+
4
13
  # Ensures the calling code is being run from inside a module directory.
5
14
  #
6
15
  # @param opts [Hash] options to change the behavior of the check logic.
@@ -14,7 +23,7 @@ module PDK
14
23
  return unless PDK::Util.module_root.nil?
15
24
  return if opts[:check_module_layout] && PDK::Util.in_module_root?
16
25
 
17
- message = opts.fetch(:message, _('This command must be run from inside a valid module (no metadata.json found).'))
26
+ message = opts.fetch(:message, 'This command must be run from inside a valid module (no metadata.json found).')
18
27
  raise PDK::CLI::ExitWithError.new(message, opts)
19
28
  end
20
29
  module_function :ensure_in_module!
@@ -22,25 +31,28 @@ module PDK
22
31
  def spinner_opts_for_platform
23
32
  windows_opts = {
24
33
  success_mark: '*',
25
- error_mark: 'X',
34
+ error_mark: 'X'
26
35
  }
27
36
 
28
37
  return windows_opts if Gem.win_platform?
38
+
29
39
  {}
30
40
  end
31
41
  module_function :spinner_opts_for_platform
32
42
 
33
43
  def prompt_for_yes(question_text, opts = {})
44
+ require 'tty/prompt'
45
+
34
46
  prompt = opts[:prompt] || TTY::Prompt.new(help_color: :cyan)
35
- validator = proc { |value| [true, false].include?(value) || value =~ %r{\A(?:yes|y|no|n)\Z}i }
47
+ validator = proc { |value| [true, false].include?(value) || value =~ /\A(?:yes|y|no|n)\Z/i }
36
48
  response = nil
37
49
 
38
50
  begin
39
51
  response = prompt.yes?(question_text) do |q|
40
52
  q.default opts[:default] unless opts[:default].nil?
41
- q.validate(validator, _('Answer "Y" to continue or "n" to cancel.'))
53
+ q.validate(validator, 'Answer "Y" to continue or "n" to cancel.')
42
54
  end
43
- rescue TTY::Prompt::Reader::InputInterrupt
55
+ rescue PDK::CLI::Util::Interview::READER::InputInterrupt
44
56
  PDK.logger.info opts[:cancel_message] if opts[:cancel_message]
45
57
  end
46
58
 
@@ -48,9 +60,32 @@ module PDK
48
60
  end
49
61
  module_function :prompt_for_yes
50
62
 
63
+ # Uses environment variables to detect if the current process is running in common
64
+ # Continuous Integration (CI) environments
65
+ # @return [Boolean] Whether the PDK is in a CI based environment
66
+ def ci_environment?
67
+ [
68
+ 'CI', # Generic
69
+ 'CONTINUOUS_INTEGRATION', # Generic
70
+ 'APPVEYOR_BUILD_FOLDER', # AppVeyor CI
71
+ 'GITLAB_CI', # GitLab CI
72
+ 'JENKINS_URL', # Jenkins
73
+ 'BUILD_DEFINITIONNAME', # Azure Pipelines
74
+ 'TEAMCITY_VERSION', # Team City
75
+ 'BAMBOO_BUILDKEY', # Bamboo
76
+ 'GOCD_SERVER_URL', # Go CD
77
+ 'TRAVIS', # Travis CI
78
+ 'GITHUB_WORKFLOW' # GitHub Actions
79
+ ].any? { |name| PDK::Util::Env.key?(name) }
80
+ end
81
+ module_function :ci_environment?
82
+
51
83
  def interactive?
84
+ require 'pdk/logger'
85
+
52
86
  return false if PDK.logger.debug?
53
- return !ENV['PDK_FRONTEND'].casecmp('noninteractive').zero? if ENV['PDK_FRONTEND']
87
+ return !PDK::Util::Env['PDK_FRONTEND'].casecmp('noninteractive').zero? if PDK::Util::Env['PDK_FRONTEND']
88
+ return false if ci_environment?
54
89
  return false unless $stderr.isatty
55
90
 
56
91
  true
@@ -62,66 +97,89 @@ module PDK
62
97
 
63
98
  # This means the module does not have a pdk-version tag in the metadata.json
64
99
  # and will require a pdk convert.
65
- raise PDK::CLI::ExitWithError, _('This module is not PDK compatible. Run `pdk convert` to make it compatible with your version of PDK.') if module_pdk_ver.nil?
100
+ raise PDK::CLI::ExitWithError, 'This module is not PDK compatible. Run `pdk convert` to make it compatible with your version of PDK.' if module_pdk_ver.nil?
66
101
 
67
102
  # This checks that the version of pdk in the module's metadata is older
68
103
  # than 1.3.1, which means the module will need to run pdk convert to the
69
104
  # new templates.
70
105
  if Gem::Version.new(module_pdk_ver) < Gem::Version.new('1.3.1')
71
- PDK.logger.warn _('This module template is out of date. Run `pdk convert` to make it compatible with your version of PDK.')
106
+ PDK.logger.warn 'This module template is out of date. Run `pdk convert` to make it compatible with your version of PDK.'
72
107
  # This checks if the version of the installed PDK is older than the
73
108
  # version in the module's metadata, and advises the user to upgrade to
74
109
  # their install of PDK.
75
110
  elsif Gem::Version.new(PDK::VERSION) < Gem::Version.new(module_pdk_ver)
76
- PDK.logger.warn _('This module is compatible with a newer version of PDK. Upgrade your version of PDK to ensure compatibility.')
111
+ PDK.logger.warn 'This module is compatible with a newer version of PDK. Upgrade your version of PDK to ensure compatibility.'
77
112
  # This checks if the version listed in the module's metadata is older
78
113
  # than the installed PDK, and advises the user to run pdk update.
79
114
  elsif Gem::Version.new(PDK::VERSION) > Gem::Version.new(module_pdk_ver)
80
- PDK.logger.warn _('This module is compatible with an older version of PDK. Run `pdk update` to update it to your version of PDK.')
115
+ message = 'This module is compatible with an older version of PDK.'
116
+ message = 'Module templates older than 3.0.0 may experience issues.' if Gem::Version.new(module_pdk_ver) < Gem::Version.new('3.0.0')
117
+
118
+ PDK.logger.warn "#{message} Run `pdk update` to update it to the latest version."
81
119
  end
82
120
  end
83
121
  module_function :module_version_check
84
122
 
85
- def puppet_from_opts_or_env(opts)
86
- use_puppet_dev = (opts || {})[:'puppet-dev'] || ENV['PDK_PUPPET_DEV']
87
- desired_puppet_version = (opts || {})[:'puppet-version'] || ENV['PDK_PUPPET_VERSION']
88
- desired_pe_version = (opts || {})[:'pe-version'] || ENV['PDK_PE_VERSION']
123
+ def check_for_deprecated_puppet(version)
124
+ return unless version.is_a?(Gem::Version)
125
+
126
+ deprecated_below = Gem::Version.new('7.0.0')
127
+ return unless version < deprecated_below
128
+
129
+ raise PDK::CLI::ExitWithError, "Support for Puppet versions older than #{deprecated_below} has been removed from PDK."
130
+ end
131
+ module_function :check_for_deprecated_puppet
132
+
133
+ # @param opts [Hash] - the pdk options to use, defaults to empty hash
134
+ # @option opts [String] :'puppet-dev' Use the puppet development version, default to PDK_PUPPET_DEV env
135
+ # @option opts [String] :'puppet-version' Puppet version to use, default to PDK_PUPPET_VERSION env
136
+ # @option opts [String] :'pe-version' PE Puppet version to use, default to PDK_PE_VERSION env
137
+ # @param logging_disabled [Boolean] - disable logging of PDK info
138
+ # @param context [PDK::Context::AbstractContext] - The context the PDK is running in
139
+ # @return [Hash] - return hash of { gemset: <>, ruby_version: 2.x.x }
140
+ def puppet_from_opts_or_env(opts, logging_disabled = false, context = PDK.context)
141
+ opts ||= {}
142
+ use_puppet_dev = opts.fetch(:'puppet-dev', PDK::Util::Env['PDK_PUPPET_DEV'])
143
+ desired_puppet_version = opts.fetch(:'puppet-version', PDK::Util::Env['PDK_PUPPET_VERSION'])
144
+ desired_pe_version = opts.fetch(:'pe-version', PDK::Util::Env['PDK_PE_VERSION'])
89
145
 
90
146
  begin
91
147
  puppet_env =
92
148
  if use_puppet_dev
149
+ PDK::Util::PuppetVersion.fetch_puppet_dev(run: :once)
93
150
  PDK::Util::PuppetVersion.puppet_dev_env
94
151
  elsif desired_puppet_version
95
152
  PDK::Util::PuppetVersion.find_gem_for(desired_puppet_version)
96
153
  elsif desired_pe_version
97
154
  PDK::Util::PuppetVersion.from_pe_version(desired_pe_version)
98
- else
155
+ elsif context.is_a?(PDK::Context::Module)
99
156
  PDK::Util::PuppetVersion.from_module_metadata || PDK::Util::PuppetVersion.latest_available
157
+ else
158
+ PDK::Util::PuppetVersion.latest_available
100
159
  end
101
160
  rescue ArgumentError => e
102
161
  raise PDK::CLI::ExitWithError, e.message
103
162
  end
104
163
 
105
164
  # Notify user of what Ruby version will be used.
106
- PDK.logger.info(_('Using Ruby %{version}') % {
107
- version: puppet_env[:ruby_version],
108
- })
165
+ PDK.logger.info(format('Using Ruby %{version}', version: puppet_env[:ruby_version])) unless logging_disabled
166
+
167
+ check_for_deprecated_puppet(puppet_env[:gem_version])
109
168
 
110
169
  gemset = { puppet: puppet_env[:gem_version].to_s }
111
170
 
112
171
  # Notify user of what gems are being activated.
113
- gemset.each do |gem, version|
114
- next if version.nil?
172
+ unless logging_disabled
173
+ gemset.each do |gem, version|
174
+ next if version.nil?
115
175
 
116
- PDK.logger.info(_('Using %{gem} %{version}') % {
117
- gem: gem.to_s.capitalize,
118
- version: version,
119
- })
176
+ PDK.logger.info(format('Using %{gem} %{version}', gem: gem.to_s.capitalize, version: version))
177
+ end
120
178
  end
121
179
 
122
180
  {
123
181
  gemset: gemset,
124
- ruby_version: puppet_env[:ruby_version],
182
+ ruby_version: puppet_env[:ruby_version]
125
183
  }
126
184
  end
127
185
  module_function :puppet_from_opts_or_env
@@ -129,24 +187,21 @@ module PDK
129
187
  def validate_puppet_version_opts(opts)
130
188
  puppet_ver_specs = []
131
189
  puppet_ver_specs << '--puppet-version option' if opts[:'puppet-version']
132
- puppet_ver_specs << 'PDK_PUPPET_VERSION environment variable' if ENV['PDK_PUPPET_VERSION'] && !ENV['PDK_PUPPET_VERSION'].empty?
190
+ puppet_ver_specs << 'PDK_PUPPET_VERSION environment variable' if PDK::Util::Env['PDK_PUPPET_VERSION'] && !PDK::Util::Env['PDK_PUPPET_VERSION'].empty?
133
191
 
134
192
  pe_ver_specs = []
135
193
  pe_ver_specs << '--pe-version option' if opts[:'pe-version']
136
- pe_ver_specs << 'PDK_PE_VERSION environment variable' if ENV['PDK_PE_VERSION'] && !ENV['PDK_PE_VERSION'].empty?
194
+ pe_ver_specs << 'PDK_PE_VERSION environment variable' if PDK::Util::Env['PDK_PE_VERSION'] && !PDK::Util::Env['PDK_PE_VERSION'].empty?
137
195
 
138
196
  puppet_dev_specs = []
139
197
  puppet_dev_specs << '--puppet-dev flag' if opts[:'puppet-dev']
140
- puppet_dev_specs << 'PDK_PUPPET_DEV environment variable' if ENV['PDK_PUPPET_DEV'] && !ENV['PDK_PUPPET_DEV'].empty?
198
+ puppet_dev_specs << 'PDK_PUPPET_DEV environment variable' if PDK::Util::Env['PDK_PUPPET_DEV'] && !PDK::Util::Env['PDK_PUPPET_DEV'].empty?
141
199
 
142
200
  puppet_dev_specs.each do |pup_dev_spec|
143
201
  [puppet_ver_specs, pe_ver_specs].each do |offending|
144
202
  next if offending.empty?
145
203
 
146
- raise PDK::CLI::ExitWithError, _('You cannot specify a %{first} and %{second} at the same time') % {
147
- first: pup_dev_spec,
148
- second: offending.first,
149
- }
204
+ raise PDK::CLI::ExitWithError, format('You cannot specify a %{first} and %{second} at the same time.', first: pup_dev_spec, second: offending.first)
150
205
  end
151
206
  end
152
207
 
@@ -155,39 +210,41 @@ module PDK
155
210
 
156
211
  offending = [pup_ver_spec, pe_ver_specs[0]].sort
157
212
 
158
- raise PDK::CLI::ExitWithError, _('You cannot specify a %{first} and %{second} at the same time.') % {
159
- first: offending[0],
160
- second: offending[1],
161
- }
213
+ raise PDK::CLI::ExitWithError, format('You cannot specify a %{first} and %{second} at the same time.', first: offending[0], second: offending[1])
214
+ end
215
+
216
+ # We want to mark setting the PE as deprecated.
217
+ if opts[:'pe-version'] || PDK::Util::Env['PDK_PE_VERSION']
218
+ PDK.logger.warn('Specifying a Puppet Enterprise version is now deprecated and will be removed in a future version. Please use --puppet-version or PDK_PUPPET_VERSION instead.')
162
219
  end
163
220
 
164
221
  if puppet_dev_specs.size == 2
165
222
  warning_str = 'Puppet dev flag from command line: "--puppet-dev" '
166
223
  warning_str += 'overrides value from environment: "PDK_PUPPET_DEV=true". You should not specify both.'
167
224
 
168
- PDK.logger.warn(_(warning_str) % {
169
- pup_ver_opt: opts[:'puppet-dev'],
170
- pup_ver_env: ENV['PDK_PUPPET_DEV'],
171
- })
225
+ PDK.logger.warn(format(warning_str, pup_ver_opt: opts[:'puppet-dev'], pup_ver_env: PDK::Util::Env['PDK_PUPPET_DEV']))
172
226
  elsif puppet_ver_specs.size == 2
173
227
  warning_str = 'Puppet version option from command line: "--puppet-version=%{pup_ver_opt}" '
174
228
  warning_str += 'overrides value from environment: "PDK_PUPPET_VERSION=%{pup_ver_env}". You should not specify both.'
175
229
 
176
- PDK.logger.warn(_(warning_str) % {
177
- pup_ver_opt: opts[:'puppet-version'],
178
- pup_ver_env: ENV['PDK_PUPPET_VERSION'],
179
- })
230
+ PDK.logger.warn(format(warning_str, pup_ver_opt: opts[:'puppet-version'], pup_ver_env: PDK::Util::Env['PDK_PUPPET_VERSION']))
180
231
  elsif pe_ver_specs.size == 2
181
232
  warning_str = 'Puppet Enterprise version option from command line: "--pe-version=%{pe_ver_opt}" '
182
233
  warning_str += 'overrides value from environment: "PDK_PE_VERSION=%{pe_ver_env}". You should not specify both.'
183
234
 
184
- PDK.logger.warn(_(warning_str) % {
185
- pup_ver_opt: opts[:'pe-version'],
186
- pup_ver_env: ENV['PDK_PE_VERSION'],
187
- })
235
+ PDK.logger.warn(format(warning_str, pe_ver_opt: opts[:'pe-version'], pe_ver_env: PDK::Util::Env['PDK_PE_VERSION']))
188
236
  end
189
237
  end
190
238
  module_function :validate_puppet_version_opts
239
+
240
+ def validate_template_opts(opts)
241
+ raise PDK::CLI::ExitWithError, '--template-ref requires --template-url to also be specified.' if opts[:'template-ref'] && opts[:'template-url'].nil?
242
+
243
+ return unless opts[:'template-url']&.include?('#')
244
+
245
+ raise PDK::CLI::ExitWithError, '--template-url may not be used to specify paths containing #\'s.'
246
+ end
247
+ module_function :validate_template_opts
191
248
  end
192
249
  end
193
250
  end
@@ -1,124 +1,109 @@
1
- require 'pdk/util/bundler'
2
-
3
- module PDK::CLI
4
- @validate_cmd = @base_cmd.define_command do
5
- name 'validate'
6
- usage _('validate [validators] [options] [targets]')
7
- summary _('Run static analysis tests.')
8
- description _(
9
- "Run metadata, YAML, Puppet, Ruby, or Tasks validation.\n\n" \
10
- '[validators] is an optional comma-separated list of validators to use. ' \
11
- 'If not specified, all validators are used. ' \
12
- "Note that when using PowerShell, the list of validators must be enclosed in single quotes.\n\n" \
13
- '[targets] is an optional space-separated list of files or directories to be validated. ' \
14
- 'If not specified, validators are run against all applicable files in the module.',
15
- )
16
-
17
- PDK::CLI.puppet_version_options(self)
18
- PDK::CLI.puppet_dev_option(self)
19
- flag nil, :list, _('List all available validators.')
20
- flag :a, 'auto-correct', _('Automatically correct problems where possible.')
21
- flag nil, :parallel, _('Run validations in parallel.')
22
-
23
- run do |opts, args, _cmd|
24
- if args == ['help']
25
- PDK::CLI.run(['validate', '--help'])
26
- exit 0
27
- end
28
-
29
- validator_names = PDK::Validate.validators.map { |v| v.name }
30
- validators = PDK::Validate.validators
31
- targets = []
32
-
33
- if opts[:list]
34
- PDK.logger.info(_('Available validators: %{validator_names}') % { validator_names: validator_names.join(', ') })
35
- exit 0
36
- end
37
-
38
- PDK::CLI::Util.validate_puppet_version_opts(opts)
39
-
40
- PDK::CLI::Util.ensure_in_module!(
41
- message: _('Code validation can only be run from inside a valid module directory'),
42
- log_level: :info,
43
- )
44
-
45
- PDK::CLI::Util.module_version_check
46
-
47
- if args[0]
48
- # This may be a single validator, a list of validators, or a target.
49
- if Util::OptionValidator.comma_separated_list?(args[0])
50
- # This is a comma separated list. Treat each item as a validator.
51
-
52
- vals = Util::OptionNormalizer.comma_separated_list_to_array(args[0])
53
- validators = PDK::Validate.validators.select { |v| vals.include?(v.name) }
54
-
55
- invalid = vals.reject { |v| validator_names.include?(v) }
56
- invalid.each do |v|
57
- PDK.logger.warn(_("Unknown validator '%{v}'. Available validators: %{validators}.") % { v: v, validators: validator_names.join(', ') })
58
- end
59
- else
60
- # This is a single item. Check if it's a known validator, or otherwise treat it as a target.
61
- val = PDK::Validate.validators.find { |v| args[0] == v.name }
62
- if val
63
- validators = [val]
64
- else
65
- targets = [args[0]]
66
- # We now know that no validators were passed, so let the user know we're using all of them by default.
67
- PDK.logger.info(_('Running all available validators...'))
68
- end
1
+ module PDK
2
+ module CLI
3
+ @validate_cmd = @base_cmd.define_command do
4
+ name 'validate'
5
+ usage 'validate [validators] [options] [targets]'
6
+ summary 'Run static analysis tests.'
7
+ description "Run metadata, YAML, Puppet, Ruby, or Tasks validation.\n\n" \
8
+ '[validators] is an optional comma-separated list of validators to use. ' \
9
+ 'If not specified, all validators are used. ' \
10
+ "Note that when using PowerShell, the list of validators must be enclosed in single quotes.\n\n" \
11
+ '[targets] is an optional space-separated list of files or directories to be validated. ' \
12
+ 'If not specified, validators are run against all applicable files in the module.'
13
+
14
+ PDK::CLI.puppet_version_options(self)
15
+ PDK::CLI.puppet_dev_option(self)
16
+ flag nil, :list, 'List all available validators.'
17
+ flag :a, 'auto-correct', 'Automatically correct problems where possible.'
18
+ flag nil, :parallel, 'Run validations in parallel.'
19
+
20
+ run do |opts, args, _cmd|
21
+ # Write the context information to the debug log
22
+ PDK.context.to_debug_log
23
+
24
+ if args == ['help']
25
+ PDK::CLI.run(['validate', '--help'])
26
+ exit 0
69
27
  end
70
- else
71
- PDK.logger.info(_('Running all available validators...'))
72
- end
73
-
74
- # Subsequent arguments are targets.
75
- targets.concat(args[1..-1]) if args.length > 1
76
-
77
- report = PDK::Report.new
78
- report_formats = if opts[:format]
79
- PDK::CLI::Util::OptionNormalizer.report_formats(opts[:format])
80
- else
81
- [{
82
- method: PDK::Report.default_format,
83
- target: PDK::Report.default_target,
84
- }]
85
- end
86
28
 
87
- options = targets.empty? ? {} : { targets: targets }
88
- options[:auto_correct] = true if opts.key?(:'auto-correct')
29
+ require 'pdk/validate'
89
30
 
90
- # Ensure that the bundled gems are up to date and correct Ruby is activated before running any validations.
91
- puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts)
92
- PDK::Util::PuppetVersion.fetch_puppet_dev if opts.key?(:'puppet-dev')
93
- PDK::Util::RubyVersion.use(puppet_env[:ruby_version])
94
-
95
- options.merge!(puppet_env[:gemset])
96
-
97
- PDK::Util::Bundler.ensure_bundle!(puppet_env[:gemset])
31
+ if opts[:list]
32
+ PDK.logger.info(format('Available validators: %{validator_names}', validator_names: PDK::Validate.validator_names.join(', ')))
33
+ exit 0
34
+ end
98
35
 
99
- exit_code = 0
100
- if opts[:parallel]
101
- exec_group = PDK::CLI::ExecGroup.new(_('Validating module using %{num_of_threads} threads' % { num_of_threads: validators.count }), opts)
36
+ PDK::CLI::Util.validate_puppet_version_opts(opts)
37
+ unless PDK.feature_flag?('controlrepo') || PDK.context.is_a?(PDK::Context::Module)
38
+ raise PDK::CLI::ExitWithError.new('Code validation can only be run from inside a valid module directory', log_level: :error)
39
+ end
102
40
 
103
- validators.each do |validator|
104
- exec_group.register do
105
- validator.invoke(report, options.merge(exec_group: exec_group))
41
+ PDK::CLI::Util.module_version_check if PDK.context.is_a?(PDK::Context::Module)
42
+
43
+ # Set the ruby version we're going to use early. Must be set before the validators are created.
44
+ # Note that this is a bit of code-smell and should be fixed
45
+ puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts)
46
+ PDK::Util::RubyVersion.use(puppet_env[:ruby_version])
47
+
48
+ targets = []
49
+ validators_to_run = nil
50
+ if args[0]
51
+ # This may be a single validator, a list of validators, or a target.
52
+ if Util::OptionValidator.comma_separated_list?(args[0])
53
+ # This is a comma separated list. Treat each item as a validator.
54
+ vals = Util::OptionNormalizer.comma_separated_list_to_array(args[0])
55
+ validators_to_run = PDK::Validate.validator_names.select { |name| vals.include?(name) }
56
+
57
+ vals.reject { |v| PDK::Validate.validator_names.include?(v) }
58
+ .each do |v|
59
+ PDK.logger.warn(format("Unknown validator '%{v}'. Available validators: %{validators}.", v: v, validators: PDK::Validate.validator_names.join(', ')))
60
+ end
61
+ else
62
+ # This is a single item. Check if it's a known validator, or otherwise treat it as a target.
63
+ val = PDK::Validate.validator_names.find { |name| args[0] == name }
64
+ if val
65
+ validators_to_run = [val]
66
+ else
67
+ targets = [args[0]]
68
+ # We now know that no validators were passed, so let the user know we're using all of them by default.
69
+ PDK.logger.info('Running all available validators...')
70
+ end
106
71
  end
72
+ else
73
+ PDK.logger.info('Running all available validators...')
107
74
  end
108
-
109
- exit_code = exec_group.exit_code
110
- else
111
- validators.each do |validator|
112
- validator_exit_code = validator.invoke(report, options.dup)
113
- exit_code = validator_exit_code if validator_exit_code != 0
75
+ validators_to_run = PDK::Validate.validator_names if validators_to_run.nil?
76
+
77
+ # Subsequent arguments are targets.
78
+ targets.concat(args.to_a[1..]) if args.length > 1
79
+
80
+ report = PDK::Report.new
81
+ report_formats = if opts[:format]
82
+ PDK::CLI::Util::OptionNormalizer.report_formats(opts[:format])
83
+ else
84
+ [{
85
+ method: PDK::Report.default_format,
86
+ target: PDK::Report.default_target
87
+ }]
88
+ end
89
+
90
+ options = targets.empty? ? {} : { targets: targets }
91
+ options[:auto_correct] = true if opts[:'auto-correct']
92
+ options.merge!(puppet_env[:gemset])
93
+
94
+ # Ensure that the bundled gems are up to date and correct Ruby is activated before running any validations.
95
+ # Note that if no Gemfile exists, then ensure_bundle! will log a debug message and exit gracefully
96
+ require 'pdk/util/bundler'
97
+ PDK::Util::Bundler.ensure_bundle!(puppet_env[:gemset])
98
+
99
+ exit_code, report = PDK::Validate.invoke_validators_by_name(PDK.context, validators_to_run, opts.fetch(:parallel, false), options)
100
+
101
+ report_formats.each do |format|
102
+ report.send(format[:method], format[:target])
114
103
  end
115
- end
116
104
 
117
- report_formats.each do |format|
118
- report.send(format[:method], format[:target])
105
+ exit exit_code
119
106
  end
120
-
121
- exit exit_code
122
107
  end
123
108
  end
124
109
  end