Class: Archiver::Cli
- Inherits:
-
Object
- Object
- Archiver::Cli
- Defined in:
- lib/archiver/cli.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#stderr ⇒ Object
Returns the value of attribute stderr.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
Instance Method Summary collapse
- #checks ⇒ Object
- #error(message) ⇒ Object
-
#initialize(argv, stdout, stderr) ⇒ Cli
constructor
A new instance of Cli.
- #process ⇒ Object
- #records ⇒ Object
Constructor Details
#initialize(argv, stdout, stderr) ⇒ Cli
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/archiver/cli.rb', line 16 def initialize(argv, stdout, stderr) @stdout = stdout @stderr = stderr @config = { :verbose => false, :from => false, :into => false } @parser = OptionParser.new do |opt| opt. = "Usage: archiver [options]" opt.separator "" opt.separator "Mandatory options:" opt.on("-f", "--from DIR", "From directory name") do |dir| @config[:from] = dir end opt.on("-i", "--into DIR", "Into directory name") do |dir| @config[:into] = dir end opt.separator "" opt.separator "Optional options:" opt.on("-V", "--verbose", "Verbose output format") do @config[:verbose] = true end opt.on("-h", "--help", "Show this help message") do stdout.puts @parser exit 0 end opt.on("-v", "--version", "Show current version") do stdout.puts "archiver v#{Version::STRING}" exit 0 end end @parser.parse! unless @config[:from] or @config[:into] puts @parser exit 1 end checks process end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
13 14 15 |
# File 'lib/archiver/cli.rb', line 13 def config @config end |
#parser ⇒ Object
Returns the value of attribute parser.
14 15 16 |
# File 'lib/archiver/cli.rb', line 14 def parser @parser end |
#stderr ⇒ Object
Returns the value of attribute stderr.
12 13 14 |
# File 'lib/archiver/cli.rb', line 12 def stderr @stderr end |
#stdout ⇒ Object
Returns the value of attribute stdout.
11 12 13 |
# File 'lib/archiver/cli.rb', line 11 def stdout @stdout end |
Instance Method Details
#checks ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/archiver/cli.rb', line 69 def checks unless File.directory? config[:from] error "Fromdir #{config[:from]} does not exist" end unless File.directory? config[:into] error "Intodir #{config[:into]} does not exist" end config[:fromdir] = File.realpath config[:from] config[:intodir] = File.realpath config[:into] end |
#error(message) ⇒ Object
124 125 126 127 128 |
# File 'lib/archiver/cli.rb', line 124 def error() stderr.puts "Error: #{message}" stderr.puts @parser exit 1 end |
#process ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/archiver/cli.rb', line 82 def process unless config[:verbose] progress = ProgressBar.new("Archiver", records.count, stdout) end records.each do |record| next unless record.process? if config[:verbose] from = record.path.gsub(/^#{config[:fromdir]}\//, "") into = File.join(record.segment, record.filename) stdout.puts "#{from} -> #{into}" else progress.inc end record.move config[:intodir] end if config[:verbose] stdout.puts "Processed #{records.count} records!" else progress.finish end rescue SystemExit, Interrupt progress.finish if progress exit 1 end |
#records ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/archiver/cli.rb', line 112 def records @records ||= begin rows = [] Dir.glob(File.join(config[:fromdir], "**", "*")).each do |row| next if File.directory? row rows << Record.new(row) end rows end end |