162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
# File 'lib/pdk/generate/module.rb', line 162
def self.module_interview(metadata, opts = {})
require 'pdk/module/metadata'
require 'pdk/cli/util/interview'
questions = [
{
name: 'module_name',
question: _('If you have a name for your module, add it here.'),
help: _('This is the name that will be associated with your module, it should be relevant to the modules content.'),
required: true,
validate_pattern: %r{\A[a-z][a-z0-9_]*\Z}i,
validate_message: _('Module names must begin with a lowercase letter and can only include lowercase letters, numbers, and underscores.'),
},
{
name: 'forge_username',
question: _('If you have a Puppet Forge username, add it here.'),
help: _('We can use this to upload your module to the Forge when it\'s complete.'),
required: true,
validate_pattern: %r{\A[a-z0-9]+\Z}i,
validate_message: _('Forge usernames can only contain lowercase letters and numbers'),
default: opts[:username],
},
{
name: 'version',
question: _('What version is this module?'),
help: _('Puppet uses Semantic Versioning (semver.org) to version modules.'),
required: true,
validate_pattern: %r{\A[0-9]+\.[0-9]+\.[0-9]+},
validate_message: _('Semantic Version numbers must be in the form MAJOR.MINOR.PATCH'),
default: metadata.data['version'],
forge_only: true,
},
{
name: 'author',
question: _('Who wrote this module?'),
help: _('This is used to credit the module\'s author.'),
required: true,
default: metadata.data['author'],
},
{
name: 'license',
question: _('What license does this module code fall under?'),
help: _('This should be an identifier from https://spdx.org/licenses/. Common values are "Apache-2.0", "MIT", or "proprietary".'),
required: true,
default: metadata.data['license'],
},
{
name: 'operatingsystem_support',
question: _('What operating systems does this module support?'),
help: _('Use the up and down keys to move between the choices, space to select and enter to continue.'),
required: true,
type: :multi_select,
choices: PDK::Module::Metadata::OPERATING_SYSTEMS,
default: PDK::Module::Metadata::DEFAULT_OPERATING_SYSTEMS.map do |os_name|
PDK::Module::Metadata::OPERATING_SYSTEMS.keys.index(os_name) + 1
end,
},
{
name: 'summary',
question: _('Summarize the purpose of this module in a single sentence.'),
help: _('This helps other Puppet users understand what the module does.'),
required: true,
default: metadata.data['summary'],
forge_only: true,
},
{
name: 'source',
question: _('If there is a source code repository for this module, enter the URL here.'),
help: _('Skip this if no repository exists yet. You can update this later in the metadata.json.'),
required: true,
default: metadata.data['source'],
forge_only: true,
},
{
name: 'project_page',
question: _('If there is a URL where others can learn more about this module, enter it here.'),
help: _('Optional. You can update this later in the metadata.json.'),
default: metadata.data['project_page'],
forge_only: true,
},
{
name: 'issues_url',
question: _('If there is a public issue tracker for this module, enter its URL here.'),
help: _('Optional. You can update this later in the metadata.json.'),
default: metadata.data['issues_url'],
forge_only: true,
},
]
prompt = TTY::Prompt.new(help_color: :cyan)
interview = PDK::CLI::Util::Interview.new(prompt)
if opts[:only_ask]
questions.reject! do |question|
if %w[module_name forge_username].include?(question[:name])
metadata.data['name'] && metadata.data['name'] =~ %r{\A[a-z0-9]+-[a-z][a-z0-9_]*\Z}i
else
!opts[:only_ask].include?(question[:name])
end
end
else
questions.reject! { |q| q[:name] == 'module_name' } if opts.key?(:module_name)
questions.reject! { |q| q[:name] == 'license' } if opts.key?(:license)
questions.reject! { |q| q[:forge_only] } unless opts[:'full-interview']
end
interview.add_questions(questions)
if File.file?('metadata.json')
puts _(
"\nWe need to update the metadata.json file for this module, so we\'re going to ask you %{count} " \
"questions.\n",
) % {
count: interview.num_questions,
}
else
puts _(
"\nWe need to create the metadata.json file for this module, so we\'re going to ask you %{count} " \
"questions.\n",
) % {
count: interview.num_questions,
}
end
puts _(
'If the question is not applicable to this module, accept the default option ' \
'shown after each question. You can modify any answers at any time by manually updating ' \
"the metadata.json file.\n\n",
)
answers = interview.run
if answers.nil?
PDK.logger.info _('No answers given, interview cancelled.')
exit 0
end
unless answers['forge_username'].nil?
opts[:username] = answers['forge_username']
unless answers['module_name'].nil?
opts[:module_name] = answers['module_name']
answers.delete('module_name')
end
answers['name'] = "#{opts[:username]}-" + (opts[:module_name])
answers.delete('forge_username')
end
answers['license'] = opts[:license] if opts.key?(:license)
answers['operatingsystem_support'].flatten! if answers.key?('operatingsystem_support')
metadata.update!(answers)
if opts[:prompt].nil? || opts[:prompt]
require 'pdk/cli/util'
continue = PDK::CLI::Util.prompt_for_yes(
_('Metadata will be generated based on this information, continue?'),
prompt: prompt,
cancel_message: _('Interview cancelled; exiting.'),
)
unless continue
PDK.logger.info _('Process cancelled; exiting.')
exit 0
end
end
require 'pdk/answer_file'
PDK.answers.update!(
{
'forge_username' => opts[:username],
'author' => answers['author'],
'license' => answers['license'],
}.delete_if { |_key, value| value.nil? },
)
end
|