# -*- encoding : utf-8 -*- # Created by Thomas Boerger on 2012-03-16. # Copyright (c) 2012. All rights reserved. require 'optparse' require 'progressbar' module Archiver class Cli attr_accessor :stdout attr_accessor :stderr attr_accessor :config attr_accessor :parser def initialize(argv, stdout, stderr) @stdout = stdout @stderr = stderr @config = { :verbose => false } @parser = OptionParser.new do |opt| opt.banner = "Usage: archiver [options] " opt.on("-V", "--verbose", "Verbose output format") do @config[:verbose] = true end opt.on("-h", "--help", "Show this help message") do puts @parser exit 0 end opt.on("-v", "--version", "Show current version") do puts "archiver v#{Version::STRING}" exit 0 end end unless argv.size == 2 puts @parser exit 1 end @config[:fromdir] = argv.shift @config[:intodir] = argv.shift @parser.parse! checks process end def checks unless File.directory? config[:fromdir] error "Fromdir does not exist" end unless File.directory? config[:intodir] error "Intodir does not exist" end config[:fromdir_real] = File.realpath config[:fromdir] config[:intodir_real] = File.realpath config[:intodir] end def process unless config[:verbose] progress = ProgressBar.new('Archiver', records.count) end records.each do |record| next unless record.process? record.move config[:intodir_real] if config[:verbose] puts "#{record.path} -> #{record.filename}" else progress.inc end end if config[:verbose] puts "Processed #{records.count} records!" else progress.finish end rescue SystemExit, Interrupt progress.finish if progress exit 1 end def records @records ||= begin rows = [] Dir.glob(File.join(config[:fromdir_real], '**', '*')).each do |row| next if File.directory? row rows << Record.new(row) end rows end end def error(message) puts "Error: #{message}" puts @parser exit 1 end end end