本文介绍了批量验证 yaml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要验证一大堆 YAML 文件.
I need to validate a whole bunch of YAML files.
我尝试了 yaml 在线解析器(http://yaml-online-parser.appspot.com/) 工作完美,但是将每个 YAML 文件内容复制到框中并解析它们需要太多的手动工作.
I tried the yaml online parser (http://yaml-online-parser.appspot.com/) which works perfect, but it's too much manual work to copy each YAML file content into the box and parse them.
有没有办法批量解析/验证 YAML 文件?
Is there a way to parse/validate YAML files in bulk?
推荐答案
这在任何具有 YAML 库的脚本语言中都相当简单.例如,您可以在 Ruby 中这样做:
This is fairly straightforward in any scripting language that has a YAML library. For example, here's how you might do it in Ruby:
#!/usr/bin/env ruby
require "yaml"
def check_file(filename)
YAML.parse_file(filename)
puts "OK"
0
rescue Psych::SyntaxError => ex
puts "Error#{ex.message[/: .+/]}"
1
end
exit_code = 0
max_filename_length = ARGV.max_by(&:size).size
ARGV.each do |filename|
printf "%-*s ", max_filename_length, filename
exit_code |= check_file(filename)
end
exit exit_code
用法:
$ ruby check_yaml.rb *.yml
config-1.yml OK
config-2.yml OK
invalid.yml Error: did not find expected key while parsing a block mapping at line 2 column 3
xyzzy.yml OK
$ echo $EXIT_CODE
1
这篇关于批量验证 yaml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!