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
data/lib/pdk/cli/exec.rb CHANGED
@@ -1,28 +1,39 @@
1
- require 'bundler'
2
- require 'childprocess'
3
- require 'tempfile'
4
- require 'tty-spinner'
5
- require 'tty-which'
6
-
7
- require 'pdk/util'
8
- require 'pdk/util/git'
9
- require 'pdk/util/ruby_version'
1
+ require 'pdk'
10
2
 
11
3
  module PDK
12
4
  module CLI
13
5
  module Exec
6
+ autoload :Command, 'pdk/cli/exec/command'
7
+ autoload :InteractiveCommand, 'pdk/cli/exec/interactive_command'
8
+
14
9
  def self.execute(*cmd)
10
+ require 'pdk/cli/exec/command'
11
+
15
12
  Command.new(*cmd).execute!
16
13
  end
17
14
 
18
15
  def self.execute_with_env(env, *cmd)
16
+ require 'pdk/cli/exec/command'
17
+
19
18
  Command.new(*cmd).tap { |c| c.environment = env }.execute!
20
19
  end
21
20
 
21
+ def self.execute_interactive(*cmd)
22
+ require 'pdk/cli/exec/interactive_command'
23
+
24
+ InteractiveCommand.new(*cmd).execute!
25
+ end
26
+
27
+ def self.execute_interactive_with_env(env, *cmd)
28
+ require 'pdk/cli/exec/interactive_command'
29
+
30
+ InteractiveCommand.new(*cmd).tap { |c| c.environment = env }.execute!
31
+ end
32
+
22
33
  def self.ensure_bin_present!(bin_path, bin_name)
23
- message = _('Unable to find `%{name}`. Check that it is installed and try again.') % {
24
- name: bin_name,
25
- }
34
+ require 'tty-which'
35
+
36
+ message = format('Unable to find `%{name}`. Check that it is installed and try again.', name: bin_name)
26
37
 
27
38
  raise PDK::CLI::FatalError, message unless TTY::Which.exist?(bin_path)
28
39
  end
@@ -34,6 +45,8 @@ module PDK
34
45
  end
35
46
 
36
47
  def self.bundle_bin
48
+ require 'pdk/util/ruby_version'
49
+
37
50
  bundle_bin = Gem.win_platform? ? 'bundle.bat' : 'bundle'
38
51
  vendored_bin_path = File.join('private', 'ruby', PDK::Util::RubyVersion.active_ruby_version, 'bin', bundle_bin)
39
52
 
@@ -41,206 +54,24 @@ module PDK
41
54
  end
42
55
 
43
56
  def self.try_vendored_bin(vendored_bin_path, fallback)
57
+ require 'pdk/util'
58
+
44
59
  unless PDK::Util.package_install?
45
- PDK.logger.debug(_("PDK package installation not found. Trying '%{fallback}' from the system PATH instead.") % {
46
- fallback: fallback,
47
- })
60
+ PDK.logger.debug(format("PDK package installation not found. Trying '%{fallback}' from the system PATH instead.", fallback: fallback))
48
61
  return fallback
49
62
  end
50
63
 
51
64
  vendored_bin_full_path = File.join(PDK::Util.pdk_package_basedir, vendored_bin_path)
52
65
 
53
- unless File.exist?(vendored_bin_full_path)
54
- PDK.logger.debug(_("Could not find '%{vendored_bin}' in PDK package. Trying '%{fallback}' from the system PATH instead.") % {
55
- fallback: fallback,
56
- vendored_bin: vendored_bin_full_path,
57
- })
66
+ require 'pdk/util/filesystem'
67
+ unless PDK::Util::Filesystem.exist?(vendored_bin_full_path)
68
+ PDK.logger.debug(format("Could not find '%{vendored_bin}' in PDK package. Trying '%{fallback}' from the system PATH instead.", fallback: fallback, vendored_bin: vendored_bin_full_path))
58
69
  return fallback
59
70
  end
60
71
 
61
- PDK.logger.debug(_("Using '%{vendored_bin}' from PDK package.") % { vendored_bin: vendored_bin_full_path })
72
+ PDK.logger.debug format("Using '%{vendored_bin}' from PDK package.", vendored_bin: vendored_bin_full_path)
62
73
  vendored_bin_full_path
63
74
  end
64
-
65
- # TODO: decide how/when to connect stdin to child process for things like pry
66
- # TODO: need a way to set callbacks on new stdout/stderr data
67
- class Command
68
- attr_reader :argv
69
- attr_reader :context
70
- attr_accessor :timeout
71
- attr_accessor :environment
72
- attr_writer :exec_group
73
-
74
- def initialize(*argv)
75
- @argv = argv
76
-
77
- @process = ChildProcess.build(*@argv)
78
- @process.leader = true
79
-
80
- @stdout = Tempfile.new('stdout').tap { |io| io.sync = true }
81
- @stderr = Tempfile.new('stderr').tap { |io| io.sync = true }
82
-
83
- @process.io.stdout = @stdout
84
- @process.io.stderr = @stderr
85
-
86
- # Default to running things in the system context.
87
- @context = :system
88
-
89
- # Extra environment vars to add to base set.
90
- @environment = {}
91
-
92
- # Register the ExecGroup when running in parallel
93
- @exec_group = nil
94
- end
95
-
96
- def context=(new_context)
97
- unless [:system, :module, :pwd].include?(new_context)
98
- raise ArgumentError, _("Expected execution context to be :system or :module but got '%{context}'.") % { context: new_context }
99
- end
100
-
101
- @context = new_context
102
- end
103
-
104
- def register_spinner(spinner, opts = {})
105
- return unless PDK::CLI::Util.interactive?
106
- @success_message = opts.delete(:success)
107
- @failure_message = opts.delete(:failure)
108
-
109
- @spinner = spinner
110
- end
111
-
112
- def add_spinner(message, opts = {})
113
- return unless PDK::CLI::Util.interactive?
114
- @success_message = opts.delete(:success)
115
- @failure_message = opts.delete(:failure)
116
-
117
- @spinner = TTY::Spinner.new("[:spinner] #{message}", opts.merge(PDK::CLI::Util.spinner_opts_for_platform))
118
- end
119
-
120
- def update_environment(additional_env)
121
- @environment.merge!(additional_env)
122
- end
123
-
124
- def execute!
125
- # Start spinning if configured.
126
- @spinner.auto_spin if @spinner
127
-
128
- # Add custom env vars.
129
- @environment.each do |k, v|
130
- @process.environment[k] = v
131
- end
132
-
133
- @process.environment['BUNDLE_IGNORE_CONFIG'] = '1'
134
-
135
- if [:module, :pwd].include?(context)
136
- @process.environment['GEM_HOME'] = PDK::Util::RubyVersion.gem_home
137
- @process.environment['GEM_PATH'] = PDK::Util::RubyVersion.gem_path
138
-
139
- # Make sure invocation of Ruby prefers our private installation.
140
- package_binpath = PDK::Util.package_install? ? File.join(PDK::Util.pdk_package_basedir, 'bin') : nil
141
- @process.environment['PATH'] = [
142
- PDK::Util::RubyVersion.bin_path,
143
- File.join(@process.environment['GEM_HOME'], 'bin'),
144
- PDK::Util::RubyVersion.gem_paths_raw.map { |gem_path| File.join(gem_path, 'bin') },
145
- package_binpath,
146
- ENV['PATH'],
147
- PDK::Util.package_install? ? PDK::Util::Git.git_paths : nil,
148
- ].compact.flatten.join(File::PATH_SEPARATOR)
149
-
150
- mod_root = PDK::Util.module_root
151
-
152
- unless mod_root
153
- @spinner.error if @spinner
154
-
155
- raise PDK::CLI::FatalError, _('Current working directory is not part of a module. (No metadata.json was found.)')
156
- end
157
-
158
- if Dir.pwd == mod_root || context == :pwd
159
- run_process_in_clean_env!
160
- else
161
- Dir.chdir(mod_root) do
162
- run_process_in_clean_env!
163
- end
164
- end
165
- else
166
- run_process!
167
- end
168
-
169
- # Stop spinning when done (if configured).
170
- stop_spinner
171
-
172
- @stdout.rewind
173
- @stderr.rewind
174
-
175
- process_data = {
176
- stdout: @stdout.read,
177
- stderr: @stderr.read,
178
- exit_code: @process.exit_code,
179
- duration: @duration,
180
- }
181
-
182
- return process_data
183
- ensure
184
- @stdout.close
185
- @stderr.close
186
- end
187
-
188
- protected
189
-
190
- def stop_spinner
191
- return unless @spinner
192
-
193
- # If it is a single spinner, we need to send it a success/error message
194
- if @process.exit_code.zero?
195
- @spinner.success(@success_message || '')
196
- else
197
- @spinner.error(@failure_message || '')
198
- end
199
- end
200
-
201
- def run_process_in_clean_env!
202
- ::Bundler.with_clean_env do
203
- run_process!
204
- end
205
- end
206
-
207
- def run_process!
208
- command_string = argv.join(' ')
209
-
210
- PDK.logger.debug(_("Executing '%{command}'") % { command: command_string })
211
-
212
- if context == :module
213
- PDK.logger.debug(_('Command environment:'))
214
- @process.environment.each do |var, val|
215
- PDK.logger.debug(" #{var}: #{val}")
216
- end
217
- end
218
-
219
- start_time = Time.now
220
-
221
- begin
222
- @process.start
223
- rescue ChildProcess::LaunchError => e
224
- raise PDK::CLI::FatalError, _("Failed to execute '%{command}': %{message}") % { command: command_string, message: e.message }
225
- end
226
-
227
- if timeout
228
- begin
229
- @process.poll_for_exit(timeout)
230
- rescue ChildProcess::TimeoutError
231
- @process.stop # tries increasingly harsher methods to kill the process.
232
- end
233
- else
234
- # Wait indfinitely if no timeout set.
235
- @process.wait
236
- end
237
-
238
- @duration = Time.now - start_time
239
-
240
- PDK.logger.debug(_("Execution of '%{command}' complete (duration: %{duration_in_seconds}s; exit code: %{exit_code})") %
241
- { command: command_string, duration_in_seconds: @duration, exit_code: @process.exit_code })
242
- end
243
- end
244
75
  end
245
76
  end
246
77
  end
@@ -1,66 +1,102 @@
1
- require 'tty-spinner'
2
- require 'tty-which'
3
-
4
- require 'pdk/util'
1
+ require 'pdk'
5
2
 
6
3
  module PDK
7
4
  module CLI
8
5
  class ExecGroup
9
- def initialize(message, opts = {})
10
- @options = opts.merge(PDK::CLI::Util.spinner_opts_for_platform)
11
-
12
- if PDK::CLI::Util.interactive?
13
- @spinner = if parallel?
14
- TTY::Spinner::Multi.new("[:spinner] #{message}", @options)
15
- else
16
- TTY::Spinner.new("[:spinner] #{message}", @options)
17
- end
18
- @spinner.auto_spin
6
+ # Execution Group (ExecGroup) factory.
7
+ #
8
+ # @param message [String] A name or message for this group. Provided for backwards compatibility during refactor
9
+ #
10
+ # @param create_options [Hash] A hash options used during creation of the ExecGroup. This are not passed to the new object
11
+ # @option create_options :parallel [Boolean] Whether the group should be executed in Parallel (True) or Serial (False)
12
+ #
13
+ # @param group_opts [Hash] A hash of options used to configure the execution group. Provided for backwards compatibility during refactor
14
+ #
15
+ # @return [ExecGroup]
16
+ def self.create(message, create_options = {}, group_opts = {})
17
+ if create_options[:parallel]
18
+ ParallelExecGroup.new(message, group_opts)
19
+ else
20
+ SerialExecGroup.new(message, group_opts)
19
21
  end
20
-
21
- @threads_or_procs = []
22
- @exit_codes = []
23
22
  end
24
23
 
25
- def parallel?
26
- @options[:parallel].nil? ? true : @options[:parallel]
24
+ # Base class for an Exection Group
25
+ #
26
+ # @param message [String] A name or message for this group. Provided for backwards compatibility during refactor
27
+ #
28
+ # @param opts [Hash] A hash of options used to configure the execution group. Provided for backwards compatibility during refactor
29
+ #
30
+ # @api private
31
+ def initialize(_message, opts = {})
32
+ @options = opts
27
33
  end
28
34
 
35
+ # Register something to execute as a group
36
+ #
37
+ # @param block [Block] A block of ruby to execute
38
+ #
39
+ # @api private
29
40
  def register(&block)
30
- raise PDK::CLI::FatalError, 'No block registered' unless block_given?
41
+ raise PDK::CLI::FatalError, 'No block registered' unless block
42
+ end
43
+
44
+ # The return code of running all registered blocks
45
+ #
46
+ # @return [int] The highest exit code from the blocks
47
+ #
48
+ # @abstract
49
+ def exit_code; end
50
+ end
31
51
 
32
- @threads_or_procs << if parallel?
33
- Thread.new do
34
- GettextSetup.initialize(File.absolute_path('../../../locales', File.dirname(__FILE__)))
35
- GettextSetup.negotiate_locale!(GettextSetup.candidate_locales)
36
- @exit_codes << yield
37
- end
38
- else
39
- block
40
- end
52
+ # Executes registered blocks in serial
53
+ #
54
+ # @see PDK::CLI::ExecGroup
55
+ class SerialExecGroup < ExecGroup
56
+ def initialize(message, opts = {})
57
+ super(message, opts)
58
+ @procs = []
41
59
  end
42
60
 
43
- def add_spinner(message, opts = {})
44
- return unless PDK::CLI::Util.interactive?
45
- @spinner.register("[:spinner] #{message}", @options.merge(opts).merge(PDK::CLI::Util.spinner_opts_for_platform))
61
+ def register(&block)
62
+ super(&block)
63
+
64
+ @procs << block
46
65
  end
47
66
 
48
67
  def exit_code
49
- if parallel?
50
- @threads_or_procs.each(&:join)
51
- else
52
- @exit_codes = @threads_or_procs.map(&:call)
53
- end
68
+ exit_codes = @procs.map(&:call)
69
+ exit_codes.nil? ? 0 : exit_codes.max
70
+ end
71
+ end
72
+
73
+ # Executes registered blocks in parallel using Ruby threads
74
+ #
75
+ # @see PDK::CLI::ExecGroup
76
+ class ParallelExecGroup < ExecGroup
77
+ def initialize(message, opts = {})
78
+ super(message, opts)
79
+ @threads = []
80
+ @exit_codes = []
81
+ end
54
82
 
55
- exit_code = @exit_codes.max
83
+ def register(&block)
84
+ super(&block)
56
85
 
57
- if exit_code.zero? && @spinner
58
- @spinner.success
59
- elsif @spinner
60
- @spinner.error
86
+ # TODO: This executes the thread immediately, whereas the SerialExecGroup executes only when exit_code
87
+ # is called. Need to change this so it uses a kind of ThreadPool to limit to number on concurrent jobs
88
+ # and only starts on the call to exit_code
89
+ # e.g. max_threads = No. of CPUs
90
+ @threads << Thread.new do
91
+ @exit_codes << yield
61
92
  end
93
+ end
94
+
95
+ def exit_code
96
+ @threads.each(&:join)
97
+ return 0 if @exit_codes.empty?
62
98
 
63
- exit_code
99
+ @exit_codes.max
64
100
  end
65
101
  end
66
102
  end
@@ -0,0 +1,26 @@
1
+ module PDK
2
+ module CLI
3
+ @get_config_cmd = @get_cmd.define_command do
4
+ name 'config'
5
+ usage 'config [name]'
6
+ summary 'Retrieve the configuration for <name>. If not specified, retrieve all configuration settings'
7
+
8
+ run do |_opts, args, _cmd|
9
+ item_name = args[0]
10
+ resolved_config = PDK.config.resolve(item_name)
11
+ # If the user wanted to know a setting but it doesn't exist, raise an error
12
+ if resolved_config.empty? && !item_name.nil?
13
+ PDK.logger.error(format("Configuration item '%{name}' does not exist", name: item_name))
14
+ exit 1
15
+ end
16
+ # If the user requested a setting and it's the only one resolved, then just output the value
17
+ if resolved_config.count == 1 && resolved_config.keys[0] == item_name
18
+ puts format('%{value}', value: resolved_config.values[0])
19
+ exit 0
20
+ end
21
+ # Otherwise just output everything
22
+ resolved_config.keys.sort.each { |key| puts format('%{name}=%{value}', name: key, value: resolved_config[key]) }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ module PDK
2
+ module CLI
3
+ @get_cmd = @base_cmd.define_command do
4
+ name 'get'
5
+ usage 'get [subcommand] [options]'
6
+ summary 'Retrieve information about the PDK or current project.'
7
+ default_subcommand 'help'
8
+
9
+ run do |_opts, args, _cmd|
10
+ if args == ['help']
11
+ PDK::CLI.run(['get', '--help'])
12
+ exit 0
13
+ end
14
+
15
+ PDK::CLI.run(['get', 'help']) if args.empty?
16
+ end
17
+ end
18
+ @get_cmd.add_command Cri::Command.new_basic_help
19
+ end
20
+ end
21
+
22
+ require 'pdk/cli/get/config'
@@ -1,32 +1,30 @@
1
- module PDK::CLI
2
- @new_class_cmd = @new_cmd.define_command do
3
- name 'class'
4
- usage _('class [options] <class_name>')
5
- summary _('Create a new class named <class_name> using given options')
1
+ module PDK
2
+ module CLI
3
+ @new_class_cmd = @new_cmd.define_command do
4
+ name 'class'
5
+ usage 'class [options] <class_name>'
6
+ summary 'Create a new class named <class_name> using given options'
6
7
 
7
- PDK::CLI.template_url_option(self)
8
+ run do |opts, args, _cmd|
9
+ require 'pdk/generate/puppet_class'
8
10
 
9
- run do |opts, args, _cmd|
10
- require 'pdk/generate/puppet_class'
11
+ PDK::CLI::Util.ensure_in_module!(
12
+ message: 'Classes can only be created from inside a valid module directory.',
13
+ log_level: :info
14
+ )
11
15
 
12
- PDK::CLI::Util.ensure_in_module!(
13
- message: _('Classes can only be created from inside a valid module directory.'),
14
- log_level: :info,
15
- )
16
+ class_name = args[0]
16
17
 
17
- class_name = args[0]
18
- module_dir = Dir.pwd
18
+ if class_name.nil? || class_name.empty?
19
+ puts command.help
20
+ exit 1
21
+ end
19
22
 
20
- if class_name.nil? || class_name.empty?
21
- puts command.help
22
- exit 1
23
- end
23
+ raise PDK::CLI::ExitWithError, format("'%{name}' is not a valid class name", name: class_name) unless Util::OptionValidator.valid_class_name?(class_name)
24
24
 
25
- unless Util::OptionValidator.valid_class_name?(class_name)
26
- raise PDK::CLI::ExitWithError, _("'%{name}' is not a valid class name") % { name: class_name }
25
+ updates = PDK::Generate::PuppetClass.new(PDK.context, class_name, opts).run
26
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
27
27
  end
28
-
29
- PDK::Generate::PuppetClass.new(module_dir, class_name, opts).run
30
28
  end
31
29
  end
32
30
  end
@@ -1,30 +1,30 @@
1
- module PDK::CLI
2
- @new_define_cmd = @new_cmd.define_command do
3
- name 'defined_type'
4
- usage _('defined_type [options] <name>')
5
- summary _('Create a new defined type named <name> using given options')
1
+ module PDK
2
+ module CLI
3
+ @new_define_cmd = @new_cmd.define_command do
4
+ name 'defined_type'
5
+ usage 'defined_type [options] <name>'
6
+ summary 'Create a new defined type named <name> using given options'
6
7
 
7
- PDK::CLI.template_url_option(self)
8
+ run do |opts, args, _cmd|
9
+ PDK::CLI::Util.ensure_in_module!(
10
+ message: 'Defined types can only be created from inside a valid module directory.',
11
+ log_level: :info
12
+ )
8
13
 
9
- run do |opts, args, _cmd|
10
- PDK::CLI::Util.ensure_in_module!(
11
- message: _('Defined types can only be created from inside a valid module directory.'),
12
- log_level: :info,
13
- )
14
+ defined_type_name = args[0]
14
15
 
15
- defined_type_name = args[0]
16
- module_dir = Dir.pwd
16
+ if defined_type_name.nil? || defined_type_name.empty?
17
+ puts command.help
18
+ exit 1
19
+ end
17
20
 
18
- if defined_type_name.nil? || defined_type_name.empty?
19
- puts command.help
20
- exit 1
21
- end
21
+ raise PDK::CLI::ExitWithError, format("'%{name}' is not a valid defined type name", name: defined_type_name) unless Util::OptionValidator.valid_defined_type_name?(defined_type_name)
22
22
 
23
- unless Util::OptionValidator.valid_defined_type_name?(defined_type_name)
24
- raise PDK::CLI::ExitWithError, _("'%{name}' is not a valid defined type name") % { name: defined_type_name }
25
- end
23
+ require 'pdk/generate/defined_type'
26
24
 
27
- PDK::Generate::DefinedType.new(module_dir, defined_type_name, opts).run
25
+ updates = PDK::Generate::DefinedType.new(PDK.context, defined_type_name, opts).run
26
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
27
+ end
28
28
  end
29
29
  end
30
30
  end
@@ -0,0 +1,27 @@
1
+ module PDK
2
+ module CLI
3
+ @new_fact_cmd = @new_cmd.define_command do
4
+ name 'fact'
5
+ usage 'fact [options] <name>'
6
+ summary 'Create a new custom fact named <name> using given options'
7
+
8
+ run do |opts, args, _cmd|
9
+ PDK::CLI::Util.ensure_in_module!
10
+
11
+ fact_name = args[0]
12
+
13
+ if fact_name.nil? || fact_name.empty?
14
+ puts command.help
15
+ exit 1
16
+ end
17
+
18
+ raise PDK::CLI::ExitWithError, format("'%{name}' is not a valid fact name", name: fact_name) unless Util::OptionValidator.valid_fact_name?(fact_name)
19
+
20
+ require 'pdk/generate/fact'
21
+
22
+ updates = PDK::Generate::Fact.new(PDK.context, fact_name, opts).run
23
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module PDK
2
+ module CLI
3
+ @new_function_cmd = @new_cmd.define_command do
4
+ name 'function'
5
+ usage 'function [options] <name>'
6
+ summary 'Create a new function named <name> using given options'
7
+ option :t, :type, 'The function type, (native or v4)', argument: :required, default: 'native'
8
+
9
+ run do |opts, args, _cmd|
10
+ PDK::CLI::Util.ensure_in_module!
11
+
12
+ function_name = args[0]
13
+
14
+ if function_name.nil? || function_name.empty?
15
+ puts command.help
16
+ exit 1
17
+ end
18
+
19
+ raise PDK::CLI::ExitWithError, format("'%{name}' is not a valid function name", name: function_name) unless Util::OptionValidator.valid_function_name?(function_name)
20
+
21
+ require 'pdk/generate/function'
22
+ updates = PDK::Generate::Function.new(PDK.context, function_name, opts).run
23
+ PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past)
24
+ end
25
+ end
26
+ end
27
+ end