From 1545145147b7b414a48662ccac73abd99666f3d0 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Mon, 26 Feb 2018 10:48:30 +0900 Subject: [PATCH] check yaml existence and parse error. Closes #958 --- bin/review-compile | 21 ++++++++++++++++++--- bin/review-epubmaker | 7 ++++++- lib/review/epubmaker.rb | 11 +++++++++-- lib/review/pdfmaker.rb | 16 +++++++++++++--- lib/review/textmaker.rb | 14 ++++++++++++-- lib/review/webmaker.rb | 14 ++++++++++++-- 6 files changed, 70 insertions(+), 13 deletions(-) diff --git a/bin/review-compile b/bin/review-compile index 06f5b9eb3..2891b30ca 100755 --- a/bin/review-compile +++ b/bin/review-compile @@ -99,11 +99,26 @@ def _main end begin - loader = ReVIEW::YAMLLoader.new if config['yaml'] - config.deep_merge!(loader.load_file(config['yaml'])) + unless File.exist?(config['yaml']) + @logger.error "#{config['yaml']} not found." + exit 1 + end + begin + config.deep_merge!(YAML.load_file(config['yaml'])) + rescue => e + @logger.error 'yaml error' + @logger.error e.message + exit 1 + end elsif File.exist?(DEFAULT_CONFIG_FILENAME) - config.deep_merge!(loader.load_file(DEFAULT_CONFIG_FILENAME)) + begin + config.deep_merge!(YAML.load_file(DEFAULT_CONFIG_FILENAME)) + rescue => e + @logger.error 'yaml error' + @logger.error e.message + exit 1 + end end config['builder'] = target diff --git a/bin/review-epubmaker b/bin/review-epubmaker index afb030cd1..2d9b0a0db 100755 --- a/bin/review-epubmaker +++ b/bin/review-epubmaker @@ -33,11 +33,16 @@ rescue OptionParser::ParseError => err exit 1 end -if ARGV.size < 1 || !File.exist?(ARGV[0]) +if ARGV.size < 1 puts opts.help exit 1 end +unless File.exist?(ARGV[0]) + @logger.error "#{ARGV[0]} not found." + exit 1 +end + yaml_file = ARGV[0] bookname = ARGV[1] rv.produce(yaml_file, bookname) diff --git a/lib/review/epubmaker.rb b/lib/review/epubmaker.rb index 941c4b3c8..0205a1eb6 100644 --- a/lib/review/epubmaker.rb +++ b/lib/review/epubmaker.rb @@ -49,8 +49,15 @@ def log(s) end def load_yaml(yamlfile) - loader = ReVIEW::YAMLLoader.new - @config = ReVIEW::Configure.values.deep_merge(loader.load_file(yamlfile)) + @config = ReVIEW::Configure.values + begin + @config.deep_merge!(YAML.load_file(yamlfile)) + rescue => e + @logger.error 'yaml error' + @logger.error e.message + exit 1 + end + @producer = Producer.new(@config) @producer.load(yamlfile) @config = @producer.config diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index f26c4dfbb..f10a59aab 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -107,10 +107,20 @@ def execute(*args) @config = ReVIEW::Configure.values @config.maker = 'pdfmaker' cmd_config, yamlfile = parse_opts(args) - loader = ReVIEW::YAMLLoader.new - @config.deep_merge!(loader.load_file(yamlfile)) + unless File.exist?(yamlfile) + @logger.error "#{yamlfile} not found." + exit 1 + end + + begin + @config.deep_merge!(YAML.load_file(yamlfile)) + rescue => e + @logger.error 'yaml error' + @logger.error e.message + exit 1 + end # YAML configs will be overridden by command line options. - @config.merge!(cmd_config) + @config.deep_merge!(cmd_config) I18n.setup(@config['language']) @basedir = File.dirname(yamlfile) @basehookdir = File.absolute_path(File.dirname(yamlfile)) diff --git a/lib/review/textmaker.rb b/lib/review/textmaker.rb index f7c49bcd9..d669d08aa 100644 --- a/lib/review/textmaker.rb +++ b/lib/review/textmaker.rb @@ -62,10 +62,20 @@ def execute(*args) @config = ReVIEW::Configure.values @config.maker = 'textmaker' cmd_config, yamlfile = parse_opts(args) + unless File.exist?(yamlfile) + @logger.error "#{yamlfile} not found." + exit 1 + end - @config.merge!(YAML.load_file(yamlfile)) + begin + @config.deep_merge!(YAML.load_file(yamlfile)) + rescue => e + @logger.error 'yaml error' + @logger.error e.message + exit 1 + end # YAML configs will be overridden by command line options. - @config.merge!(cmd_config) + @config.deep_merge!(cmd_config) I18n.setup(@config['language']) generate_text_files(yamlfile) end diff --git a/lib/review/webmaker.rb b/lib/review/webmaker.rb index aa8292c06..cb325b314 100644 --- a/lib/review/webmaker.rb +++ b/lib/review/webmaker.rb @@ -68,10 +68,20 @@ def execute(*args) @config = ReVIEW::Configure.values @config.maker = 'webmaker' cmd_config, yamlfile = parse_opts(args) + unless File.exist?(yamlfile) + @logger.error "#{yamlfile} not found." + exit 1 + end - @config.merge!(YAML.load_file(yamlfile)) + begin + @config.deep_merge!(YAML.load_file(yamlfile)) + rescue => e + @logger.error 'yaml error' + @logger.error e.message + exit 1 + end # YAML configs will be overridden by command line options. - @config.merge!(cmd_config) + @config.deep_merge!(cmd_config) @config['htmlext'] = 'html' I18n.setup(@config['language']) generate_html_files(yamlfile)