-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(PDK-735) Implement a YAML validator
- Loading branch information
Showing
15 changed files
with
307 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
require 'pdk' | ||
require 'pdk/cli/exec' | ||
require 'pdk/validate/base_validator' | ||
require 'pdk/util' | ||
require 'pathname' | ||
|
||
module PDK | ||
module Validate | ||
class YAML | ||
class Syntax < BaseValidator | ||
IGNORE_DOTFILES = false | ||
|
||
def self.name | ||
'yaml-syntax' | ||
end | ||
|
||
def self.pattern | ||
[ | ||
'**/*.yaml', | ||
'*.yaml', | ||
'**/*.yml', | ||
'*.yml', | ||
] | ||
end | ||
|
||
def self.spinner_text(_targets = []) | ||
_('Checking YAML syntax (%{targets}).') % { | ||
targets: pattern, | ||
} | ||
end | ||
|
||
def self.create_spinner(targets = [], options = {}) | ||
return unless PDK::CLI::Util.interactive? | ||
options = options.merge(PDK::CLI::Util.spinner_opts_for_platform) | ||
|
||
exec_group = options[:exec_group] | ||
@spinner = if exec_group | ||
exec_group.add_spinner(spinner_text(targets), options) | ||
else | ||
TTY::Spinner.new("[:spinner] #{spinner_text(targets)}", options) | ||
end | ||
@spinner.auto_spin | ||
end | ||
|
||
def self.stop_spinner(exit_code) | ||
if exit_code.zero? && @spinner | ||
@spinner.success | ||
elsif @spinner | ||
@spinner.error | ||
end | ||
end | ||
|
||
def self.invoke(report, options = {}) | ||
targets, skipped, invalid = parse_targets(options) | ||
|
||
process_skipped(report, skipped) | ||
process_invalid(report, invalid) | ||
|
||
return 0 if targets.empty? | ||
|
||
return_val = 0 | ||
create_spinner(targets, options) | ||
|
||
targets.each do |target| | ||
unless File.readable?(target) | ||
report.add_event( | ||
file: target, | ||
source: name, | ||
state: :failure, | ||
severity: 'error', | ||
message: _('Could not be read.'), | ||
) | ||
return_val = 1 | ||
next | ||
end | ||
|
||
begin | ||
::YAML.safe_load(File.read(target), [], [], true) | ||
|
||
report.add_event( | ||
file: target, | ||
source: name, | ||
state: :passed, | ||
severity: 'ok', | ||
) | ||
rescue Psych::SyntaxError => e | ||
report.add_event( | ||
file: target, | ||
source: name, | ||
state: :failure, | ||
severity: 'error', | ||
line: e.line, | ||
column: e.column, | ||
message: _('%{problem} %{context}') % { | ||
problem: e.problem, | ||
context: e.context, | ||
}, | ||
) | ||
return_val = 1 | ||
end | ||
end | ||
|
||
stop_spinner(return_val) | ||
return_val | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'pdk' | ||
require 'pdk/cli/exec' | ||
require 'pdk/validate/base_validator' | ||
require 'pdk/validate/yaml/syntax' | ||
|
||
module PDK | ||
module Validate | ||
class YAMLValidator < BaseValidator | ||
def self.name | ||
'yaml' | ||
end | ||
|
||
def self.validators | ||
[ | ||
PDK::Validate::YAML::Syntax, | ||
] | ||
end | ||
|
||
def self.invoke(report, options = {}) | ||
exit_code = 0 | ||
|
||
validators.each do |validator| | ||
exit_code = validator.invoke(report, options) | ||
break if exit_code != 0 | ||
end | ||
|
||
exit_code | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.