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
@@ -1,86 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/cli/exec'
3
- require 'pdk/validate/base_validator'
4
- require 'pdk/util/bundler'
5
- require 'pathname'
6
-
7
- module PDK
8
- module Validate
9
- class MetadataJSONLint < BaseValidator
10
- # Validate each metadata file separately, as metadata-json-lint does not
11
- # support multiple targets.
12
- INVOKE_STYLE = :per_target
13
-
14
- def self.name
15
- 'metadata-json-lint'
16
- end
17
-
18
- def self.cmd
19
- 'metadata-json-lint'
20
- end
21
-
22
- def self.spinner_text(targets = [])
23
- _('Checking module metadata style (%{targets}).') % {
24
- targets: PDK::Util.targets_relative_to_pwd(targets).join(' '),
25
- }
26
- end
27
-
28
- def self.pattern
29
- 'metadata.json'
30
- end
31
-
32
- def self.parse_options(_options, targets)
33
- cmd_options = ['--format', 'json']
34
- cmd_options << '--strict-dependencies'
35
-
36
- cmd_options.concat(targets)
37
- end
38
-
39
- def self.parse_output(report, result, targets)
40
- raise ArgumentError, _('More than 1 target provided to PDK::Validate::MetadataJSONLint.') if targets.count > 1
41
-
42
- if result[:stdout].strip.empty?
43
- # metadata-json-lint will print nothing if there are no problems with
44
- # the file being linted. This should be handled separately to
45
- # metadata-json-lint generating output that can not be parsed as JSON
46
- # (unhandled exception in metadata-json-lint).
47
- json_data = {}
48
- else
49
- begin
50
- json_data = JSON.parse(result[:stdout])
51
- rescue JSON::ParserError
52
- raise PDK::Validate::ParseOutputError, result[:stdout]
53
- end
54
- end
55
-
56
- if json_data.empty?
57
- report.add_event(
58
- file: targets.first,
59
- source: name,
60
- state: :passed,
61
- severity: :ok,
62
- )
63
- else
64
- json_data.delete('result')
65
- json_data.keys.each do |type|
66
- json_data[type].each do |offense|
67
- # metadata-json-lint groups the offenses by type, so the type ends
68
- # up being `warnings` or `errors`. We want to convert that to the
69
- # singular noun for the event.
70
- event_type = type[%r{\A(.+?)s?\Z}, 1]
71
-
72
- report.add_event(
73
- file: targets.first,
74
- source: name,
75
- message: offense['msg'],
76
- test: offense['check'],
77
- severity: event_type,
78
- state: :failure,
79
- )
80
- end
81
- end
82
- end
83
- end
84
- end
85
- end
86
- end
@@ -1,109 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/cli/exec'
3
- require 'pdk/validate/base_validator'
4
- require 'pathname'
5
-
6
- module PDK
7
- module Validate
8
- class MetadataSyntax < BaseValidator
9
- def self.name
10
- 'metadata-syntax'
11
- end
12
-
13
- def self.pattern
14
- ['metadata.json', 'tasks/*.json']
15
- end
16
-
17
- def self.spinner_text(_targets = [])
18
- _('Checking metadata syntax (%{targets}).') % {
19
- targets: pattern.join(' '),
20
- }
21
- end
22
-
23
- def self.create_spinner(targets = [], options = {})
24
- return unless PDK::CLI::Util.interactive?
25
- options = options.merge(PDK::CLI::Util.spinner_opts_for_platform)
26
-
27
- exec_group = options[:exec_group]
28
- @spinner = if exec_group
29
- exec_group.add_spinner(spinner_text(targets), options)
30
- else
31
- TTY::Spinner.new("[:spinner] #{spinner_text(targets)}", options)
32
- end
33
- @spinner.auto_spin
34
- end
35
-
36
- def self.stop_spinner(exit_code)
37
- if exit_code.zero? && @spinner
38
- @spinner.success
39
- elsif @spinner
40
- @spinner.error
41
- end
42
- end
43
-
44
- def self.invoke(report, options = {})
45
- targets, skipped, invalid = parse_targets(options)
46
-
47
- process_skipped(report, skipped)
48
- process_invalid(report, invalid)
49
-
50
- return 0 if targets.empty?
51
-
52
- return_val = 0
53
- create_spinner(targets, options)
54
-
55
- # The pure ruby JSON parser gives much nicer parse error messages than
56
- # the C extension at the cost of slightly slower parsing. We require it
57
- # here and restore the C extension at the end of the method (if it was
58
- # being used).
59
- require 'json/pure'
60
- JSON.parser = JSON::Pure::Parser
61
-
62
- targets.each do |target|
63
- unless File.readable?(target)
64
- report.add_event(
65
- file: target,
66
- source: name,
67
- state: :failure,
68
- severity: 'error',
69
- message: _('Could not be read.'),
70
- )
71
- return_val = 1
72
- next
73
- end
74
-
75
- begin
76
- JSON.parse(File.read(target))
77
-
78
- report.add_event(
79
- file: target,
80
- source: name,
81
- state: :passed,
82
- severity: 'ok',
83
- )
84
- rescue JSON::ParserError => e
85
- # Because the message contains a raw segment of the file, we use
86
- # String#dump here to unescape any escape characters like newlines.
87
- # We then strip out the surrounding quotes and the exclaimation
88
- # point that json_pure likes to put in exception messages.
89
- sane_message = e.message.dump[%r{\A"(.+?)!?"\Z}, 1]
90
-
91
- report.add_event(
92
- file: target,
93
- source: name,
94
- state: :failure,
95
- severity: 'error',
96
- message: sane_message,
97
- )
98
- return_val = 1
99
- end
100
- end
101
-
102
- stop_spinner(return_val)
103
- return_val
104
- ensure
105
- JSON.parser = JSON::Ext::Parser if defined?(JSON::Ext::Parser)
106
- end
107
- end
108
- end
109
- end
@@ -1,30 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/cli/exec'
3
- require 'pdk/validate/base_validator'
4
- require 'pdk/validate/metadata/metadata_json_lint'
5
- require 'pdk/validate/metadata/metadata_syntax'
6
-
7
- module PDK
8
- module Validate
9
- class MetadataValidator < BaseValidator
10
- def self.name
11
- 'metadata'
12
- end
13
-
14
- def self.metadata_validators
15
- [MetadataSyntax, MetadataJSONLint]
16
- end
17
-
18
- def self.invoke(report, options = {})
19
- exit_code = 0
20
-
21
- metadata_validators.each do |validator|
22
- exit_code = validator.invoke(report, options)
23
- break if exit_code != 0
24
- end
25
-
26
- exit_code
27
- end
28
- end
29
- end
30
- end
@@ -1,67 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/util'
3
- require 'pdk/cli/exec'
4
- require 'pdk/validate/base_validator'
5
-
6
- module PDK
7
- module Validate
8
- class PuppetLint < BaseValidator
9
- def self.name
10
- 'puppet-lint'
11
- end
12
-
13
- def self.cmd
14
- 'puppet-lint'
15
- end
16
-
17
- def self.pattern
18
- '**/*.pp'
19
- end
20
-
21
- def self.spinner_text(_targets = nil)
22
- _('Checking Puppet manifest style (%{pattern}).') % { pattern: pattern }
23
- end
24
-
25
- def self.parse_options(options, targets)
26
- cmd_options = ['--json', '--relative']
27
-
28
- cmd_options << '--fix' if options[:auto_correct]
29
-
30
- cmd_options.concat(targets)
31
- end
32
-
33
- def self.parse_output(report, result, targets)
34
- begin
35
- json_data = JSON.parse(result[:stdout]).flatten
36
- rescue JSON::ParserError
37
- raise PDK::Validate::ParseOutputError, result[:stdout]
38
- end
39
-
40
- # puppet-lint does not include files without problems in its JSON
41
- # output, so we need to go through the list of targets and add passing
42
- # events to the report for any target not listed in the JSON output.
43
- targets.reject { |target| json_data.any? { |j| j['path'] == target } }.each do |target|
44
- report.add_event(
45
- file: target,
46
- source: name,
47
- severity: 'ok',
48
- state: :passed,
49
- )
50
- end
51
-
52
- json_data.each do |offense|
53
- report.add_event(
54
- file: offense['path'],
55
- source: name,
56
- line: offense['line'],
57
- column: offense['column'],
58
- message: offense['message'],
59
- test: offense['check'],
60
- severity: (offense['kind'] == 'fixed') ? 'corrected' : offense['kind'],
61
- state: :failure,
62
- )
63
- end
64
- end
65
- end
66
- end
67
- end
@@ -1,112 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/cli/exec'
3
- require 'pdk/validate/base_validator'
4
-
5
- module PDK
6
- module Validate
7
- class PuppetSyntax < BaseValidator
8
- # In Puppet >= 5.3.4, the error context formatting was changed to facilitate localization
9
- ERROR_CONTEXT = %r{(?:file:\s(?<file>.+?)|line:\s(?<line>.+?)|column:\s(?<column>.+?))}
10
- # In Puppet < 5.3.3, the error context was formatted in these variations:
11
- # - "at file_path:line_num:col_num"
12
- # - "at file_path:line_num"
13
- # - "at line line_num"
14
- # - "in file_path"
15
- ERROR_CONTEXT_LEGACY = %r{(?:at\sline\s(?<line>\d+)|at\s(?<file>.+?):(?<line>\d+):(?<column>\d+)|at\s(?<file>.+?):(?<line>\d+)|in\s(?<file>.+?))}
16
-
17
- PUPPET_LOGGER_PREFIX = %r{^(debug|info|notice|warning|error|alert|critical):\s.+?$}i
18
- PUPPET_SYNTAX_PATTERN = %r{^
19
- (?<severity>.+?):\s
20
- (?<message>.+?)
21
- (?:
22
- \s\(#{ERROR_CONTEXT}(,\s#{ERROR_CONTEXT})*\)| # attempt to match the new localisation friendly location
23
- \s#{ERROR_CONTEXT_LEGACY}| # attempt to match the old " at file:line:column" location
24
- $ # handle cases where the output has no location
25
- )
26
- $}x
27
-
28
- def self.name
29
- 'puppet-syntax'
30
- end
31
-
32
- def self.cmd
33
- 'puppet'
34
- end
35
-
36
- def self.pattern
37
- '**/**.pp'
38
- end
39
-
40
- def self.pattern_ignore
41
- '/plans/**/*.pp'
42
- end
43
-
44
- def self.spinner_text(_targets = nil)
45
- _('Checking Puppet manifest syntax (%{pattern}).') % { pattern: pattern }
46
- end
47
-
48
- def self.parse_options(_options, targets)
49
- ['parser', 'validate', '--config', null_file].concat(targets)
50
- end
51
-
52
- def self.null_file
53
- Gem.win_platform? ? 'NUL' : '/dev/null'
54
- end
55
-
56
- def self.parse_output(report, result, targets)
57
- # Due to PUP-7504, we will have to programmatically construct the json
58
- # object from the text output for now.
59
- output = result[:stderr].split("\n").reject { |entry| entry.empty? }
60
-
61
- results_data = []
62
- output.each do |offense|
63
- offense_data = parse_offense(offense)
64
- results_data << offense_data
65
- end
66
-
67
- # puppet parser validate does not include files without problems in its
68
- # output, so we need to go through the list of targets and add passing
69
- # events to the report for any target not listed in the output.
70
- targets.reject { |target| results_data.any? { |j| j[:file] =~ %r{#{target}} } }.each do |target|
71
- report.add_event(
72
- file: target,
73
- source: name,
74
- severity: :ok,
75
- state: :passed,
76
- )
77
- end
78
-
79
- results_data.each do |offense|
80
- report.add_event(offense)
81
- end
82
- end
83
-
84
- def self.parse_offense(offense)
85
- sanitize_console_output(offense)
86
-
87
- offense_data = {
88
- source: name,
89
- state: :failure,
90
- }
91
-
92
- if offense.match(PUPPET_LOGGER_PREFIX)
93
- attributes = offense.match(PUPPET_SYNTAX_PATTERN)
94
-
95
- unless attributes.nil?
96
- attributes.names.each do |name|
97
- offense_data[name.to_sym] = attributes[name] unless attributes[name].nil?
98
- end
99
- end
100
- else
101
- offense_data[:message] = offense
102
- end
103
-
104
- offense_data
105
- end
106
-
107
- def self.sanitize_console_output(line)
108
- line.gsub!(%r{\e\[([;\d]+)?m}, '')
109
- end
110
- end
111
- end
112
- end
@@ -1,30 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/cli/exec'
3
- require 'pdk/validate/base_validator'
4
- require 'pdk/validate/puppet/puppet_lint'
5
- require 'pdk/validate/puppet/puppet_syntax'
6
-
7
- module PDK
8
- module Validate
9
- class PuppetValidator < BaseValidator
10
- def self.name
11
- 'puppet'
12
- end
13
-
14
- def self.puppet_validators
15
- [PuppetSyntax, PuppetLint]
16
- end
17
-
18
- def self.invoke(report, options = {})
19
- exit_code = 0
20
-
21
- puppet_validators.each do |validator|
22
- exit_code = validator.invoke(report, options)
23
- break if exit_code != 0
24
- end
25
-
26
- exit_code
27
- end
28
- end
29
- end
30
- end
@@ -1,77 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/cli/exec'
3
- require 'pdk/util'
4
- require 'pdk/util/bundler'
5
- require 'pdk/validate/base_validator'
6
- require 'pdk/validate/ruby_validator'
7
-
8
- module PDK
9
- module Validate
10
- class Rubocop < BaseValidator
11
- ALLOW_EMPTY_TARGETS = true
12
-
13
- def self.name
14
- 'rubocop'
15
- end
16
-
17
- def self.cmd
18
- 'rubocop'
19
- end
20
-
21
- def self.pattern
22
- '**/**.rb'
23
- end
24
-
25
- def self.spinner_text(_targets = nil)
26
- _('Checking Ruby code style (%{pattern}).') % { pattern: pattern }
27
- end
28
-
29
- def self.parse_options(options, targets)
30
- cmd_options = ['--format', 'json']
31
-
32
- if options[:auto_correct]
33
- cmd_options << '--auto-correct'
34
- end
35
-
36
- cmd_options.concat(targets)
37
- end
38
-
39
- def self.parse_output(report, result, _targets)
40
- return if result[:stdout].empty?
41
-
42
- begin
43
- json_data = JSON.parse(result[:stdout])
44
- rescue JSON::ParserError
45
- raise PDK::Validate::ParseOutputError, result[:stdout]
46
- end
47
-
48
- return unless json_data.key?('files')
49
-
50
- json_data['files'].each do |file_info|
51
- next unless file_info.key?('offenses')
52
- result = {
53
- file: file_info['path'],
54
- source: 'rubocop',
55
- }
56
-
57
- if file_info['offenses'].empty?
58
- report.add_event(result.merge(state: :passed, severity: :ok))
59
- else
60
- file_info['offenses'].each do |offense|
61
- report.add_event(
62
- result.merge(
63
- line: offense['location']['line'],
64
- column: offense['location']['column'],
65
- message: offense['message'],
66
- severity: (offense['corrected']) ? 'corrected' : offense['severity'],
67
- test: offense['cop_name'],
68
- state: :failure,
69
- ),
70
- )
71
- end
72
- end
73
- end
74
- end
75
- end
76
- end
77
- end
@@ -1,29 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/cli/exec'
3
- require 'pdk/validate/base_validator'
4
- require 'pdk/validate/ruby/rubocop'
5
-
6
- module PDK
7
- module Validate
8
- class RubyValidator < BaseValidator
9
- def self.name
10
- 'ruby'
11
- end
12
-
13
- def self.ruby_validators
14
- [Rubocop]
15
- end
16
-
17
- def self.invoke(report, options = {})
18
- exit_code = 0
19
-
20
- ruby_validators.each do |validator|
21
- exit_code = validator.invoke(report, options)
22
- break if exit_code != 0
23
- end
24
-
25
- exit_code
26
- end
27
- end
28
- end
29
- end
@@ -1,126 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/cli/exec'
3
- require 'pdk/validate/base_validator'
4
- require 'pdk/util'
5
- require 'pathname'
6
- require 'json-schema'
7
-
8
- module PDK
9
- module Validate
10
- class Tasks
11
- class MetadataLint < BaseValidator
12
- FORGE_SCHEMA_URL = 'https://forgeapi.puppet.com/schemas/task.json'.freeze
13
-
14
- def self.name
15
- 'task-metadata-lint'
16
- end
17
-
18
- def self.pattern
19
- 'tasks/*.json'
20
- end
21
-
22
- def self.spinner_text(_targets = [])
23
- _('Checking task metadata style (%{targets}).') % {
24
- targets: pattern,
25
- }
26
- end
27
-
28
- def self.create_spinner(targets = [], options = {})
29
- return unless PDK::CLI::Util.interactive?
30
- options = options.merge(PDK::CLI::Util.spinner_opts_for_platform)
31
-
32
- exec_group = options[:exec_group]
33
- @spinner = if exec_group
34
- exec_group.add_spinner(spinner_text(targets), options)
35
- else
36
- TTY::Spinner.new("[:spinner] #{spinner_text(targets)}", options)
37
- end
38
- @spinner.auto_spin
39
- end
40
-
41
- def self.stop_spinner(exit_code)
42
- if exit_code.zero? && @spinner
43
- @spinner.success
44
- elsif @spinner
45
- @spinner.error
46
- end
47
- end
48
-
49
- def self.schema_file
50
- schema = PDK::Util::VendoredFile.new('task.json', FORGE_SCHEMA_URL).read
51
-
52
- JSON.parse(schema)
53
- rescue PDK::Util::VendoredFile::DownloadError => e
54
- raise PDK::CLI::FatalError, e.message
55
- rescue JSON::ParserError
56
- raise PDK::CLI::FatalError, _('Failed to parse Task Metadata Schema file.')
57
- end
58
-
59
- def self.invoke(report, options = {})
60
- targets, skipped, invalid = parse_targets(options)
61
-
62
- process_skipped(report, skipped)
63
- process_invalid(report, invalid)
64
-
65
- return 0 if targets.empty?
66
-
67
- return_val = 0
68
- create_spinner(targets, options)
69
-
70
- targets.each do |target|
71
- unless File.readable?(target)
72
- report.add_event(
73
- file: target,
74
- source: name,
75
- state: :failure,
76
- severity: 'error',
77
- message: _('Could not be read.'),
78
- )
79
- return_val = 1
80
- next
81
- end
82
-
83
- begin
84
- # Need to set the JSON Parser and State Generator to the Native one to be
85
- # compatible with the multi_json adapter.
86
- JSON.parser = JSON::Ext::Parser if defined?(JSON::Ext::Parser)
87
- JSON.generator = JSON::Ext::Generator
88
-
89
- begin
90
- errors = JSON::Validator.fully_validate(schema_file, File.read(target)) || []
91
- rescue JSON::Schema::SchemaError => e
92
- raise PDK::CLI::FatalError, _('Unable to validate Task Metadata. %{error}.') % { error: e.message }
93
- end
94
-
95
- if errors.empty?
96
- report.add_event(
97
- file: target,
98
- source: name,
99
- state: :passed,
100
- severity: 'ok',
101
- )
102
- else
103
- errors.each do |error|
104
- # strip off the trailing parts that aren't relevant
105
- error = error.split('in schema').first if error.include? 'in schema'
106
-
107
- report.add_event(
108
- file: target,
109
- source: name,
110
- state: :failure,
111
- severity: 'error',
112
- message: error,
113
- )
114
- end
115
- return_val = 1
116
- end
117
- end
118
- end
119
-
120
- stop_spinner(return_val)
121
- return_val
122
- end
123
- end
124
- end
125
- end
126
- end