bin/dropcaster in dropcaster-0.0.8 vs bin/dropcaster in dropcaster-1.0.0

- old
+ new

@@ -1,129 +1,131 @@ #!/usr/bin/env ruby +# frozen_string_literal: true -$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) +$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'yaml' +require 'English' -help = <<HELP -Dropcaster is a podcast feed generator for the command line. +help = <<~HELP + Dropcaster is a podcast feed generator for the command line. -Author: Nicolas E. Rabenau nerab@gmx.at -Homepage: http://nerab.github.io/dropcaster/ + Author: Nicolas E. Rabenau nerab@gmx.at + Homepage: http://nerab.github.io/dropcaster/ -Basic Usage: + Basic Usage: - dropcaster Prints a podcast feed document for the mp3 files in the current directory. - dropcaster [FILE]... Prints a podcast feed document for FILES - dropcaster [DIR]... Prints a podcast feed document for the mp3 files in DIR + dropcaster Prints a podcast feed document for the mp3 files in the current directory. + dropcaster [FILE]... Prints a podcast feed document for FILES + dropcaster [DIR]... Prints a podcast feed document for the mp3 files in DIR -Options: + Options: HELP def usage "Run '#{File.basename(__FILE__)} --help' for further help." end require 'optparse' require 'dropcaster' +require 'dropcaster/log_formatter' -options = Hash.new +options = {} options[:auto_detect_channel_file] = true -opts = OptionParser.new do |opts| +logger = Logger.new(STDERR).tap do |l| + l.level = Logger::WARN + l.formatter = Dropcaster::LogFormatter.new +end + +# rubocop:disable Metrics/BlockLength +OptionParser.new do |opts| opts.banner = help - opts.on("--verbose", "Verbose mode - displays additional diagnostic information") do |file| - Dropcaster.logger = Logger.new(STDERR) - Dropcaster.logger.formatter = Dropcaster::LogFormatter.new - Dropcaster.logger.level = Logger::INFO + opts.on('--verbose', 'Verbose mode - displays additional diagnostic information') do |file| + logger.level = Logger::INFO end - opts.on("--trace", "Verbose mode - displays additional diagnostic information") do |file| - Dropcaster.logger = Logger.new(STDERR) - Dropcaster.logger.formatter = Dropcaster::LogFormatter.new - Dropcaster.logger.level = Logger::DEBUG + opts.on('--trace', 'Verbose mode - displays additional diagnostic information') do |file| + logger.level = Logger::DEBUG end - opts.on("--channel FILE", "Read the channel definition from FILE instead of channel.yml in the current directory.") do |file| - begin - Dropcaster.logger.info "Reading channel definition from #{file}" - options = YAML.load_file(file).merge(options) - options[:auto_detect_channel_file] = false - rescue - Dropcaster.logger.error "Could not load channel definition. #{$!.message}" - Dropcaster.logger.info $!.backtrace - exit(1) - end + opts.on('--channel FILE', 'Read the channel definition from FILE instead of channel.yml in the current directory.') do |file| + logger.info "Reading channel definition from #{file}" + options = YAML.load_file(file).merge(options) + options[:auto_detect_channel_file] = false + rescue StandardError + logger.error "Could not load channel definition. #{$ERROR_INFO.message}" + logger.info $ERROR_INFO.backtrace + exit(1) end - opts.on("--title STRING", "Use STRING as the channel's title. Overrides settings read from channel definition file.") do |title| - Dropcaster.logger.info "Setting channel title to '#{title}' via command line" + opts.on('--title STRING', "Use STRING as the channel's title. Overrides settings read from channel definition file.") do |title| + logger.info "Setting channel title to '#{title}' via command line" options[:title] = title end - opts.on("--subtitle STRING", "Use STRING as the channel's subtitle. Overrides settings read from channel definition file.") do |subtitle| - Dropcaster.logger.info "Setting channel subtitle to '#{subtitle}' via command line" + opts.on('--subtitle STRING', "Use STRING as the channel's subtitle. Overrides settings read from channel definition file.") do |subtitle| + logger.info "Setting channel subtitle to '#{subtitle}' via command line" options[:subtitle] = subtitle end - opts.on("--url URL", "Use URL as the channel's url. Overrides settings read from channel definition file.") do |url| - Dropcaster.logger.info "Setting channel URL to '#{url}' via command line" + opts.on('--url URL', "Use URL as the channel's url. Overrides settings read from channel definition file.") do |url| + logger.info "Setting channel URL to '#{url}' via command line" options[:url] = url end - opts.on("--description STRING", "Use STRING as the channel's description. Overrides settings read from channel definition file.") do |description| - Dropcaster.logger.info "Setting channel description to '#{description}' via command line" + opts.on('--description STRING', "Use STRING as the channel's description. Overrides settings read from channel definition file.") do |description| + logger.info "Setting channel description to '#{description}' via command line" options[:description] = description end - opts.on("--enclosures URL", "Use URL as the base URL for the channel's enclosures. Overrides settings read from channel definition file.") do |enclosures_url| - Dropcaster.logger.info "Setting enclosures base URL to '#{enclosures_url}' via command line" + opts.on('--enclosures URL', "Use URL as the base URL for the channel's enclosures. Overrides settings read from channel definition file.") do |enclosures_url| + logger.info "Setting enclosures base URL to '#{enclosures_url}' via command line" options[:enclosures_url] = enclosures_url end - opts.on("--image URL", "Use URL as the channel's image URL. Overrides settings read from channel definition file.") do |image_url| - Dropcaster.logger.info "Setting image URL to '#{image_url}' via command line" + opts.on('--image URL', "Use URL as the channel's image URL. Overrides settings read from channel definition file.") do |image_url| + logger.info "Setting image URL to '#{image_url}' via command line" options[:image_url] = image_url end - opts.on("--channel-template FILE", "Use FILE as template for generating the channel feed. Overrides the default that comes with Dropcaster.") do |file| - Dropcaster.logger.info "Using'#{file}' as channel template file" + opts.on('--channel-template FILE', 'Use FILE as template for generating the channel feed. Overrides the default that comes with Dropcaster.') do |file| + logger.info "Using'#{file}' as channel template file" options[:channel_template] = file end - opts.on("--version", "Display current version") do + opts.on('--version', 'Display current version') do puts "#{File.basename(__FILE__)} " + Dropcaster::VERSION exit 0 end -end +end.parse! +# rubocop:enable Metrics/BlockLength -opts.parse! sources = ARGV.blank? ? '.' : ARGV if options[:auto_detect_channel_file] # There was no channel file specified, so we try to load channel.yml from sources dir channel_file = Dropcaster::ChannelFileLocator.locate(sources) - if File.exists?(channel_file) - Dropcaster.logger.info "Auto-detected channel file at #{channel_file}" + if File.exist?(channel_file) + logger.info "Auto-detected channel file at #{channel_file}" options_from_yaml = YAML.load_file(channel_file) options = options_from_yaml.merge(options) else - Dropcaster.logger.error "No channel file found at #{channel_file})" - Dropcaster.logger.info usage + logger.error "No channel file found at #{channel_file})" + logger.info usage exit(1) # No way to continue without a channel definition end end -Dropcaster.logger.info "Generating the channel with these options: #{options.inspect}" - +logger.info "Generating the channel with these options: #{options.inspect}" begin puts Dropcaster::Channel.new(sources, options).to_rss -rescue - Dropcaster.logger.error $!.message - $!.backtrace.each do |line| - Dropcaster.logger.debug(line) +rescue StandardError + logger.error $ERROR_INFO.message + $ERROR_INFO.backtrace.each do |line| + logger.debug(line) end - Dropcaster.logger.info usage + logger.info usage exit(1) end