pdk 1.14.1 → 1.18.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +155 -2
- data/lib/pdk.rb +28 -19
- data/lib/pdk/answer_file.rb +2 -95
- data/lib/pdk/bolt.rb +19 -0
- data/lib/pdk/cli.rb +4 -5
- data/lib/pdk/cli/bundle.rb +5 -1
- data/lib/pdk/cli/config.rb +3 -1
- data/lib/pdk/cli/config/get.rb +3 -1
- data/lib/pdk/cli/console.rb +1 -1
- data/lib/pdk/cli/convert.rb +16 -10
- data/lib/pdk/cli/exec.rb +2 -1
- data/lib/pdk/cli/exec/command.rb +45 -4
- data/lib/pdk/cli/exec_group.rb +78 -43
- data/lib/pdk/cli/get.rb +20 -0
- data/lib/pdk/cli/get/config.rb +24 -0
- data/lib/pdk/cli/module/build.rb +1 -1
- data/lib/pdk/cli/module/generate.rb +1 -1
- data/lib/pdk/cli/new/class.rb +2 -2
- data/lib/pdk/cli/new/defined_type.rb +2 -2
- data/lib/pdk/cli/new/provider.rb +2 -2
- data/lib/pdk/cli/new/task.rb +2 -2
- data/lib/pdk/cli/new/test.rb +2 -2
- data/lib/pdk/cli/new/transport.rb +2 -2
- data/lib/pdk/cli/release.rb +192 -0
- data/lib/pdk/cli/release/prep.rb +39 -0
- data/lib/pdk/cli/release/publish.rb +40 -0
- data/lib/pdk/cli/remove.rb +20 -0
- data/lib/pdk/cli/remove/config.rb +80 -0
- data/lib/pdk/cli/set.rb +20 -0
- data/lib/pdk/cli/set/config.rb +119 -0
- data/lib/pdk/cli/update.rb +18 -8
- data/lib/pdk/cli/util.rb +7 -3
- data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
- data/lib/pdk/cli/validate.rb +26 -44
- data/lib/pdk/config.rb +265 -8
- data/lib/pdk/config/ini_file.rb +183 -0
- data/lib/pdk/config/ini_file_setting.rb +39 -0
- data/lib/pdk/config/namespace.rb +26 -6
- data/lib/pdk/config/setting.rb +3 -2
- data/lib/pdk/context.rb +99 -0
- data/lib/pdk/context/control_repo.rb +60 -0
- data/lib/pdk/context/module.rb +28 -0
- data/lib/pdk/context/none.rb +22 -0
- data/lib/pdk/control_repo.rb +90 -0
- data/lib/pdk/generate.rb +1 -0
- data/lib/pdk/generate/defined_type.rb +25 -32
- data/lib/pdk/generate/module.rb +42 -35
- data/lib/pdk/generate/provider.rb +16 -65
- data/lib/pdk/generate/puppet_class.rb +25 -31
- data/lib/pdk/generate/puppet_object.rb +84 -189
- data/lib/pdk/generate/resource_api_object.rb +55 -0
- data/lib/pdk/generate/task.rb +28 -46
- data/lib/pdk/generate/transport.rb +21 -75
- data/lib/pdk/module.rb +1 -1
- data/lib/pdk/module/build.rb +38 -25
- data/lib/pdk/module/convert.rb +61 -42
- data/lib/pdk/module/metadata.rb +1 -3
- data/lib/pdk/module/release.rb +254 -0
- data/lib/pdk/module/update.rb +24 -16
- data/lib/pdk/module/update_manager.rb +8 -1
- data/lib/pdk/report.rb +18 -12
- data/lib/pdk/report/event.rb +6 -3
- data/lib/pdk/template.rb +59 -0
- data/lib/pdk/template/fetcher.rb +98 -0
- data/lib/pdk/template/fetcher/git.rb +85 -0
- data/lib/pdk/template/fetcher/local.rb +28 -0
- data/lib/pdk/template/renderer.rb +96 -0
- data/lib/pdk/template/renderer/v1.rb +25 -0
- data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
- data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
- data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
- data/lib/pdk/template/template_dir.rb +67 -0
- data/lib/pdk/tests/unit.rb +5 -0
- data/lib/pdk/util.rb +55 -45
- data/lib/pdk/util/bundler.rb +9 -9
- data/lib/pdk/util/changelog_generator.rb +120 -0
- data/lib/pdk/util/env.rb +28 -11
- data/lib/pdk/util/filesystem.rb +62 -2
- data/lib/pdk/util/git.rb +60 -8
- data/lib/pdk/util/json_finder.rb +84 -0
- data/lib/pdk/util/puppet_strings.rb +3 -3
- data/lib/pdk/util/puppet_version.rb +4 -5
- data/lib/pdk/util/ruby_version.rb +9 -6
- data/lib/pdk/util/template_uri.rb +60 -48
- data/lib/pdk/util/version.rb +4 -4
- data/lib/pdk/validate.rb +79 -25
- data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
- data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
- data/lib/pdk/validate/external_command_validator.rb +208 -0
- data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
- data/lib/pdk/validate/invokable_validator.rb +215 -0
- data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
- data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
- data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
- data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
- data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
- data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
- data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
- data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
- data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
- data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
- data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
- data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
- data/lib/pdk/validate/validator.rb +118 -0
- data/lib/pdk/validate/validator_group.rb +104 -0
- data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
- data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
- data/lib/pdk/version.rb +1 -1
- data/locales/pdk.pot +755 -319
- metadata +66 -24
- data/lib/pdk/module/templatedir.rb +0 -391
- data/lib/pdk/template_file.rb +0 -96
- data/lib/pdk/validate/base_validator.rb +0 -215
- data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -82
- data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -111
- data/lib/pdk/validate/metadata_validator.rb +0 -26
- data/lib/pdk/validate/puppet/puppet_epp.rb +0 -137
- data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
- data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -137
- data/lib/pdk/validate/puppet_validator.rb +0 -26
- data/lib/pdk/validate/ruby/rubocop.rb +0 -72
- data/lib/pdk/validate/ruby_validator.rb +0 -26
- data/lib/pdk/validate/tasks/metadata_lint.rb +0 -130
- data/lib/pdk/validate/tasks/name.rb +0 -90
- data/lib/pdk/validate/tasks_validator.rb +0 -29
- data/lib/pdk/validate/yaml/syntax.rb +0 -125
- data/lib/pdk/validate/yaml_validator.rb +0 -28
@@ -0,0 +1,20 @@
|
|
1
|
+
module PDK::CLI
|
2
|
+
@remove_cmd = @base_cmd.define_command do
|
3
|
+
name 'remove'
|
4
|
+
usage _('remove [subcommand] [options]')
|
5
|
+
summary _('Remove or delete information about the PDK or current project.')
|
6
|
+
default_subcommand 'help'
|
7
|
+
|
8
|
+
run do |_opts, args, _cmd|
|
9
|
+
if args == ['help']
|
10
|
+
PDK::CLI.run(%w[remove --help])
|
11
|
+
exit 0
|
12
|
+
end
|
13
|
+
|
14
|
+
PDK::CLI.run(%w[remove help]) if args.empty?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
@remove_cmd.add_command Cri::Command.new_basic_help
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'pdk/cli/remove/config'
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module PDK::CLI
|
2
|
+
module Remove
|
3
|
+
module Config
|
4
|
+
def self.run(opts, args)
|
5
|
+
item_name = (args.count > 0) ? args[0] : nil
|
6
|
+
item_value = (args.count > 1) ? args[1].strip : nil
|
7
|
+
item_value = nil if !item_value.nil? && item_value.empty?
|
8
|
+
|
9
|
+
force = opts[:force] || false
|
10
|
+
|
11
|
+
raise PDK::CLI::ExitWithError, _('Configuration name is required') if item_name.nil?
|
12
|
+
|
13
|
+
current_value = PDK.config.get(item_name)
|
14
|
+
raise PDK::CLI::ExitWithError, _("The configuration item '%{name}' can not be removed.") % { name: item_name } if current_value.is_a?(PDK::Config::Namespace)
|
15
|
+
if current_value.nil?
|
16
|
+
PDK.logger.info(_("Could not remove '%{name}' as it has not been set") % { name: item_name })
|
17
|
+
return 0
|
18
|
+
end
|
19
|
+
|
20
|
+
PDK.logger.info(_("Ignoring the item value '%{value}' as --force has been set") % { value: item_value }) if current_value.is_a?(Array) && !item_value.nil? && force
|
21
|
+
PDK.logger.info(_('Ignoring --force as the setting is not multi-valued')) if !current_value.is_a?(Array) && force
|
22
|
+
|
23
|
+
# FIXME: It'd be nice to shortcircuit deleting default values. This causes the configuration file
|
24
|
+
# to be saved, even though nothing actually changes
|
25
|
+
|
26
|
+
# For most value types, just changing the value to nil is enough, however Arrays are a special case.
|
27
|
+
# Unless they're forced, array removal with either remove a single entry (matched by .to_s) or clear the
|
28
|
+
# array. When forced, the array is completed removed just like a string or number.
|
29
|
+
if current_value.is_a?(Array) && !force
|
30
|
+
# If the user didn't set a value then set the array as empty, otherwise remove that one item
|
31
|
+
new_value = item_value.nil? ? [] : current_value.reject { |item| item.to_s == item_value }
|
32
|
+
if current_value.count == new_value.count
|
33
|
+
if item_value.nil?
|
34
|
+
PDK.logger.info(_("Could not remove '%{name}' as it is already empty") % { name: item_name })
|
35
|
+
else
|
36
|
+
PDK.logger.info(_("Could not remove '%{value}' from '%{name}' as it has not been set") % { value: item_value, name: item_name })
|
37
|
+
end
|
38
|
+
return 0
|
39
|
+
end
|
40
|
+
PDK.config.set(item_name, new_value, force: true)
|
41
|
+
else
|
42
|
+
# Set the value to nil for deleting.
|
43
|
+
PDK.config.set(item_name, nil, force: true)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Output the result to the user
|
47
|
+
new_value = PDK.config.get(item_name)
|
48
|
+
if current_value.is_a?(Array) && !force
|
49
|
+
# Arrays have a special output format. If item_value is nil then the user wanted to empty/clear
|
50
|
+
# the array otherwise they just wanted to remove a single entry.
|
51
|
+
if item_value.nil?
|
52
|
+
PDK.logger.info(_("Cleared '%{name}' which had a value of '%{from}'") % { name: item_name, from: current_value })
|
53
|
+
else
|
54
|
+
PDK.logger.info(_("Removed '%{value}' from '%{name}'") % { value: item_value, name: item_name })
|
55
|
+
end
|
56
|
+
elsif !new_value.nil?
|
57
|
+
PDK.logger.info(_("Could not remove '%{name}' as it using a default value of '%{to}'") % { name: item_name, to: new_value })
|
58
|
+
else
|
59
|
+
PDK.logger.info(_("Removed '%{name}' which had a value of '%{from}'") % { name: item_name, from: current_value })
|
60
|
+
end
|
61
|
+
|
62
|
+
# Same output as `get config`
|
63
|
+
$stdout.puts _('%{name}=%{value}') % { name: item_name, value: new_value }
|
64
|
+
0
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
@remove_config_cmd = @remove_cmd.define_command do
|
70
|
+
name 'config'
|
71
|
+
usage _('config [name] [value]')
|
72
|
+
summary _('Remove or delete the configuration for <name>')
|
73
|
+
|
74
|
+
option :f, :force, _('Force multi-value configuration settings to be removed instead of emptied.'), argument: :forbidden
|
75
|
+
|
76
|
+
run do |opts, args, _cmd|
|
77
|
+
exit PDK::CLI::Remove::Config.run(opts, args)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/lib/pdk/cli/set.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module PDK::CLI
|
2
|
+
@set_cmd = @base_cmd.define_command do
|
3
|
+
name 'set'
|
4
|
+
usage _('set [subcommand] [options]')
|
5
|
+
summary _('Set or update information about the PDK or current project.')
|
6
|
+
default_subcommand 'help'
|
7
|
+
|
8
|
+
run do |_opts, args, _cmd|
|
9
|
+
if args == ['help']
|
10
|
+
PDK::CLI.run(%w[set --help])
|
11
|
+
exit 0
|
12
|
+
end
|
13
|
+
|
14
|
+
PDK::CLI.run(%w[set help]) if args.empty?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
@set_cmd.add_command Cri::Command.new_basic_help
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'pdk/cli/set/config'
|
@@ -0,0 +1,119 @@
|
|
1
|
+
module PDK::CLI
|
2
|
+
module Set
|
3
|
+
module Config
|
4
|
+
ALLOWED_TYPE_NAMES = %w[array boolean number string].freeze
|
5
|
+
|
6
|
+
# :nocov:
|
7
|
+
def self.pretty_allowed_names
|
8
|
+
ALLOWED_TYPE_NAMES.map { |name| "'#{name}'" }.join(', ')
|
9
|
+
end
|
10
|
+
# :nocov:
|
11
|
+
|
12
|
+
def self.transform_value(type_name, value)
|
13
|
+
normalized_name = type_name.downcase.strip
|
14
|
+
unless ALLOWED_TYPE_NAMES.include?(normalized_name)
|
15
|
+
raise PDK::CLI::ExitWithError, _('Unknown type %{type_name}. Expected one of %{allowed}') % { type_name: type_name, allowed: pretty_allowed_names }
|
16
|
+
end
|
17
|
+
|
18
|
+
# Short circuit string conversions as it's trivial
|
19
|
+
if normalized_name == 'string'
|
20
|
+
raise PDK::CLI::ExitWithError, _('An error occured converting \'%{value}\' into a %{type_name}') % { value: value.nil? ? 'nil' : value, type_name: type_name } unless value.is_a?(String)
|
21
|
+
return value
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
case normalized_name
|
26
|
+
when 'array'
|
27
|
+
convert_to_array(value)
|
28
|
+
when 'boolean'
|
29
|
+
convert_to_boolean(value)
|
30
|
+
when 'number'
|
31
|
+
convert_to_number(value)
|
32
|
+
else
|
33
|
+
value
|
34
|
+
end
|
35
|
+
rescue ArgumentError, TypeError
|
36
|
+
raise PDK::CLI::ExitWithError, _('An error occured converting \'%{value}\' into a %{type_name}') % { value: value.nil? ? 'nil' : value, type_name: type_name }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.convert_to_array(value)
|
41
|
+
return [] if value.nil?
|
42
|
+
value.is_a?(Array) ? value : [value]
|
43
|
+
end
|
44
|
+
private_class_method :convert_to_array
|
45
|
+
|
46
|
+
def self.convert_to_boolean(value)
|
47
|
+
string_val = value.to_s.strip.downcase
|
48
|
+
|
49
|
+
return true if %w[yes true -1 1].include?(string_val)
|
50
|
+
return false if %w[no false 0].include?(string_val)
|
51
|
+
|
52
|
+
raise ArgumentError
|
53
|
+
end
|
54
|
+
private_class_method :convert_to_boolean
|
55
|
+
|
56
|
+
def self.convert_to_number(value)
|
57
|
+
float_val = Float(value)
|
58
|
+
# Return an Integer if this is actually and Integer, otherwise return the float
|
59
|
+
(float_val.truncate == float_val) ? float_val.truncate : float_val
|
60
|
+
end
|
61
|
+
private_class_method :convert_to_number
|
62
|
+
|
63
|
+
def self.run(opts, args)
|
64
|
+
item_name = (args.count > 0) ? args[0] : nil
|
65
|
+
item_value = (args.count > 1) ? args[1] : nil
|
66
|
+
|
67
|
+
opts[:type] = opts[:as] if opts[:type].nil? && !opts[:as].nil?
|
68
|
+
force = opts[:force] || false
|
69
|
+
|
70
|
+
# Transform the value if we need to
|
71
|
+
item_value = PDK::CLI::Set::Config.transform_value(opts[:type], item_value) unless opts[:type].nil?
|
72
|
+
|
73
|
+
raise PDK::CLI::ExitWithError, _('Configuration name is required') if item_name.nil?
|
74
|
+
raise PDK::CLI::ExitWithError, _('Configuration value is required. If you wish to remove a value use \'pdk remove config\'') if item_value.nil?
|
75
|
+
|
76
|
+
current_value = PDK.config.get(item_name)
|
77
|
+
raise PDK::CLI::ExitWithError, _("The configuration item '%{name}' can not have a value set.") % { name: item_name } if current_value.is_a?(PDK::Config::Namespace)
|
78
|
+
|
79
|
+
# If we're forcing the value, don't do any munging
|
80
|
+
unless force
|
81
|
+
# Check if the setting already exists
|
82
|
+
if current_value.is_a?(Array) && current_value.include?(item_value)
|
83
|
+
PDK.logger.info(_("No changes made to '%{name}' as it already contains value '%{to}'") % { name: item_name, to: item_value })
|
84
|
+
return 0
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
new_value = PDK.config.set(item_name, item_value, force: opts[:force])
|
89
|
+
if current_value.nil? || force
|
90
|
+
PDK.logger.info(_("Set initial value of '%{name}' to '%{to}'") % { name: item_name, to: new_value })
|
91
|
+
elsif current_value.is_a?(Array)
|
92
|
+
# Arrays have a special output format
|
93
|
+
PDK.logger.info(_("Added new value '%{to}' to '%{name}'") % { name: item_name, to: item_value })
|
94
|
+
else
|
95
|
+
PDK.logger.info(_("Changed existing value of '%{name}' from '%{from}' to '%{to}'") % { name: item_name, from: current_value, to: new_value })
|
96
|
+
end
|
97
|
+
|
98
|
+
# Same output as `get config`
|
99
|
+
$stdout.puts _('%{name}=%{value}') % { name: item_name, value: PDK.config.get(item_name) }
|
100
|
+
0
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
@set_config_cmd = @set_cmd.define_command do
|
106
|
+
name 'config'
|
107
|
+
usage _('config [name] [value]')
|
108
|
+
summary _('Set or update the configuration for <name>')
|
109
|
+
|
110
|
+
option :f, :force, _('Force the configuration setting to be overwitten.'), argument: :forbidden
|
111
|
+
|
112
|
+
option :t, :type, _('The type of value to set. Acceptable values: %{values}') % { values: PDK::CLI::Set::Config.pretty_allowed_names }, argument: :required
|
113
|
+
option nil, :as, _('Alias of --type'), argument: :required
|
114
|
+
|
115
|
+
run do |opts, args, _cmd|
|
116
|
+
exit PDK::CLI::Set::Config.run(opts, args)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/lib/pdk/cli/update.rb
CHANGED
@@ -10,14 +10,12 @@ module PDK::CLI
|
|
10
10
|
PDK::CLI.template_ref_option(self)
|
11
11
|
|
12
12
|
run do |opts, _args, _cmd|
|
13
|
-
|
14
|
-
|
15
|
-
require 'pdk/module/update'
|
13
|
+
# Write the context information to the debug log
|
14
|
+
PDK.context.to_debug_log
|
16
15
|
|
17
|
-
PDK::
|
18
|
-
|
19
|
-
|
20
|
-
)
|
16
|
+
unless PDK.context.is_a?(PDK::Context::Module)
|
17
|
+
raise PDK::CLI::ExitWithError, _('`pdk update` can only be run from inside a valid module directory.')
|
18
|
+
end
|
21
19
|
|
22
20
|
raise PDK::CLI::ExitWithError, _('This module does not appear to be PDK compatible. To make the module compatible with PDK, run `pdk convert`.') unless PDK::Util.module_pdk_compatible?
|
23
21
|
|
@@ -46,7 +44,19 @@ module PDK::CLI
|
|
46
44
|
|
47
45
|
PDK::CLI::Util.analytics_screen_view('update', opts)
|
48
46
|
|
49
|
-
updater = PDK::Module::Update.new(opts)
|
47
|
+
updater = PDK::Module::Update.new(PDK.context.root_path, opts)
|
48
|
+
|
49
|
+
if updater.pinned_to_puppetlabs_template_tag?
|
50
|
+
PDK.logger.info _(
|
51
|
+
'This module is currently pinned to version %{current_version} ' \
|
52
|
+
'of the default template. If you would like to update your ' \
|
53
|
+
'module to the latest version of the template, please run `pdk ' \
|
54
|
+
'update --template-ref %{new_version}`.',
|
55
|
+
) % {
|
56
|
+
current_version: updater.template_uri.uri_fragment,
|
57
|
+
new_version: PDK::TEMPLATE_REF,
|
58
|
+
}
|
59
|
+
end
|
50
60
|
|
51
61
|
updater.run
|
52
62
|
end
|
data/lib/pdk/cli/util.rb
CHANGED
@@ -8,6 +8,7 @@ module PDK
|
|
8
8
|
autoload :OptionValidator, 'pdk/cli/util/option_validator'
|
9
9
|
autoload :Interview, 'pdk/cli/util/interview'
|
10
10
|
autoload :Spinner, 'pdk/cli/util/spinner'
|
11
|
+
autoload :UpdateManagerPrinter, 'pdk/cli/util/update_manager_printer'
|
11
12
|
|
12
13
|
# Ensures the calling code is being run from inside a module directory.
|
13
14
|
#
|
@@ -129,13 +130,14 @@ module PDK
|
|
129
130
|
end
|
130
131
|
module_function :check_for_deprecated_puppet
|
131
132
|
|
132
|
-
# @param opts [Hash] - the pdk options
|
133
|
+
# @param opts [Hash] - the pdk options to use, defaults to empty hash
|
133
134
|
# @option opts [String] :'puppet-dev' Use the puppet development version, default to PDK_PUPPET_DEV env
|
134
135
|
# @option opts [String] :'puppet-version' Puppet version to use, default to PDK_PUPPET_VERSION env
|
135
136
|
# @option opts [String] :'pe-version' PE Puppet version to use, default to PDK_PE_VERSION env
|
136
137
|
# @param logging_disabled [Boolean] - disable logging of PDK info
|
138
|
+
# @param context [PDK::Context::AbstractContext] - The context the PDK is running in
|
137
139
|
# @return [Hash] - return hash of { gemset: <>, ruby_version: 2.x.x }
|
138
|
-
def puppet_from_opts_or_env(opts, logging_disabled = false)
|
140
|
+
def puppet_from_opts_or_env(opts, logging_disabled = false, context = PDK.context)
|
139
141
|
opts ||= {}
|
140
142
|
use_puppet_dev = opts.fetch(:'puppet-dev', PDK::Util::Env['PDK_PUPPET_DEV'])
|
141
143
|
desired_puppet_version = opts.fetch(:'puppet-version', PDK::Util::Env['PDK_PUPPET_VERSION'])
|
@@ -150,8 +152,10 @@ module PDK
|
|
150
152
|
PDK::Util::PuppetVersion.find_gem_for(desired_puppet_version)
|
151
153
|
elsif desired_pe_version
|
152
154
|
PDK::Util::PuppetVersion.from_pe_version(desired_pe_version)
|
153
|
-
|
155
|
+
elsif context.is_a?(PDK::Context::Module)
|
154
156
|
PDK::Util::PuppetVersion.from_module_metadata || PDK::Util::PuppetVersion.latest_available
|
157
|
+
else
|
158
|
+
PDK::Util::PuppetVersion.latest_available
|
155
159
|
end
|
156
160
|
rescue ArgumentError => e
|
157
161
|
raise PDK::CLI::ExitWithError, e.message
|
@@ -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/validate.rb
CHANGED
@@ -19,6 +19,9 @@ module PDK::CLI
|
|
19
19
|
flag nil, :parallel, _('Run validations in parallel.')
|
20
20
|
|
21
21
|
run do |opts, args, _cmd|
|
22
|
+
# Write the context information to the debug log
|
23
|
+
PDK.context.to_debug_log
|
24
|
+
|
22
25
|
if args == ['help']
|
23
26
|
PDK::CLI.run(['validate', '--help'])
|
24
27
|
exit 0
|
@@ -26,42 +29,42 @@ module PDK::CLI
|
|
26
29
|
|
27
30
|
require 'pdk/validate'
|
28
31
|
|
29
|
-
validator_names = PDK::Validate.validators.map { |v| v.name }
|
30
|
-
validators = PDK::Validate.validators
|
31
|
-
targets = []
|
32
|
-
|
33
32
|
if opts[:list]
|
34
33
|
PDK::CLI::Util.analytics_screen_view('validate', opts)
|
35
|
-
PDK.logger.info(_('Available validators: %{validator_names}') % { validator_names: validator_names.join(', ') })
|
34
|
+
PDK.logger.info(_('Available validators: %{validator_names}') % { validator_names: PDK::Validate.validator_names.join(', ') })
|
36
35
|
exit 0
|
37
36
|
end
|
38
37
|
|
39
38
|
PDK::CLI::Util.validate_puppet_version_opts(opts)
|
39
|
+
unless PDK.feature_flag?('controlrepo') || PDK.context.is_a?(PDK::Context::Module)
|
40
|
+
raise PDK::CLI::ExitWithError.new(_('Code validation can only be run from inside a valid module directory'), log_level: :error)
|
41
|
+
end
|
40
42
|
|
41
|
-
PDK::CLI::Util.
|
42
|
-
message: _('Code validation can only be run from inside a valid module directory'),
|
43
|
-
log_level: :info,
|
44
|
-
)
|
43
|
+
PDK::CLI::Util.module_version_check if PDK.context.is_a?(PDK::Context::Module)
|
45
44
|
|
46
|
-
|
45
|
+
# Set the ruby version we're going to use early. Must be set before the validators are created.
|
46
|
+
# Note that this is a bit of code-smell and should be fixed
|
47
|
+
puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts)
|
48
|
+
PDK::Util::RubyVersion.use(puppet_env[:ruby_version])
|
47
49
|
|
50
|
+
targets = []
|
51
|
+
validators_to_run = nil
|
48
52
|
if args[0]
|
49
53
|
# This may be a single validator, a list of validators, or a target.
|
50
54
|
if Util::OptionValidator.comma_separated_list?(args[0])
|
51
55
|
# This is a comma separated list. Treat each item as a validator.
|
52
|
-
|
53
56
|
vals = Util::OptionNormalizer.comma_separated_list_to_array(args[0])
|
54
|
-
|
57
|
+
validators_to_run = PDK::Validate.validator_names.select { |name| vals.include?(name) }
|
55
58
|
|
56
|
-
|
57
|
-
|
58
|
-
PDK.logger.warn(_("Unknown validator '%{v}'. Available validators: %{validators}.") % { v: v, validators: validator_names.join(', ') })
|
59
|
+
vals.reject { |v| PDK::Validate.validator_names.include?(v) }
|
60
|
+
.each do |v|
|
61
|
+
PDK.logger.warn(_("Unknown validator '%{v}'. Available validators: %{validators}.") % { v: v, validators: PDK::Validate.validator_names.join(', ') })
|
59
62
|
end
|
60
63
|
else
|
61
64
|
# This is a single item. Check if it's a known validator, or otherwise treat it as a target.
|
62
|
-
val = PDK::Validate.
|
65
|
+
val = PDK::Validate.validator_names.find { |name| args[0] == name }
|
63
66
|
if val
|
64
|
-
|
67
|
+
validators_to_run = [val]
|
65
68
|
else
|
66
69
|
targets = [args[0]]
|
67
70
|
# We now know that no validators were passed, so let the user know we're using all of them by default.
|
@@ -71,11 +74,12 @@ module PDK::CLI
|
|
71
74
|
else
|
72
75
|
PDK.logger.info(_('Running all available validators...'))
|
73
76
|
end
|
77
|
+
validators_to_run = PDK::Validate.validator_names if validators_to_run.nil?
|
74
78
|
|
75
|
-
if
|
79
|
+
if validators_to_run.sort == PDK::Validate.validator_names.sort
|
76
80
|
PDK::CLI::Util.analytics_screen_view('validate', opts)
|
77
81
|
else
|
78
|
-
PDK::CLI::Util.analytics_screen_view(['validate',
|
82
|
+
PDK::CLI::Util.analytics_screen_view(['validate', validators_to_run.sort].flatten.join('_'), opts)
|
79
83
|
end
|
80
84
|
|
81
85
|
# Subsequent arguments are targets.
|
@@ -93,36 +97,14 @@ module PDK::CLI
|
|
93
97
|
|
94
98
|
options = targets.empty? ? {} : { targets: targets }
|
95
99
|
options[:auto_correct] = true if opts[:'auto-correct']
|
96
|
-
|
97
|
-
# Ensure that the bundled gems are up to date and correct Ruby is activated before running any validations.
|
98
|
-
puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts)
|
99
|
-
PDK::Util::RubyVersion.use(puppet_env[:ruby_version])
|
100
|
-
|
101
100
|
options.merge!(puppet_env[:gemset])
|
102
101
|
|
102
|
+
# Ensure that the bundled gems are up to date and correct Ruby is activated before running any validations.
|
103
|
+
# Note that if no Gemfile exists, then ensure_bundle! will log a debug message and exit gracefully
|
103
104
|
require 'pdk/util/bundler'
|
104
|
-
|
105
105
|
PDK::Util::Bundler.ensure_bundle!(puppet_env[:gemset])
|
106
106
|
|
107
|
-
exit_code =
|
108
|
-
if opts[:parallel]
|
109
|
-
require 'pdk/cli/exec_group'
|
110
|
-
|
111
|
-
exec_group = PDK::CLI::ExecGroup.new(_('Validating module using %{num_of_threads} threads' % { num_of_threads: validators.count }), opts)
|
112
|
-
|
113
|
-
validators.each do |validator|
|
114
|
-
exec_group.register do
|
115
|
-
validator.invoke(report, options.merge(exec_group: exec_group))
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
exit_code = exec_group.exit_code
|
120
|
-
else
|
121
|
-
validators.each do |validator|
|
122
|
-
validator_exit_code = validator.invoke(report, options.dup)
|
123
|
-
exit_code = validator_exit_code if validator_exit_code != 0
|
124
|
-
end
|
125
|
-
end
|
107
|
+
exit_code, report = PDK::Validate.invoke_validators_by_name(PDK.context, validators_to_run, opts.fetch(:parallel, false), options)
|
126
108
|
|
127
109
|
report_formats.each do |format|
|
128
110
|
report.send(format[:method], format[:target])
|