Class: Digestif::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/digestif/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



22
23
24
25
# File 'lib/digestif/cli.rb', line 22

def initialize(args)
  self.args = args
  self.options = parse_options
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



20
21
22
# File 'lib/digestif/cli.rb', line 20

def args
  @args
end

#optionsObject

Returns the value of attribute options.



20
21
22
# File 'lib/digestif/cli.rb', line 20

def options
  @options
end

Class Method Details

.default_optionsObject



12
13
14
15
16
17
18
# File 'lib/digestif/cli.rb', line 12

def self.default_options
  options = OpenStruct.new
  options.digest = :sha1
  options.seek_size = 1024
  options.read_size = 512
  options
end

.run(args) ⇒ Object



8
9
10
# File 'lib/digestif/cli.rb', line 8

def self.run(args)
  new(args).run
end

Instance Method Details

#error(error_obj_or_str, code = 1) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/digestif/cli.rb', line 97

def error(error_obj_or_str, code = 1)
  if error_obj_or_str.respond_to?('to_s')
    error_str = error_obj_or_str.to_s
  else
    error_str = error_obj_or_str.inspect
  end

  $stderr.puts "digestif: #{error_str}"
  exit code
end

#parse_optionsObject



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/digestif/cli.rb', line 41

def parse_options
  options = CLI.default_options

  parser = OptionParser.new do |p|
    p.banner = "Usage: digestif [options] filename"

    p.separator ""
    p.separator "Options:"
    p.separator ""

    p.on("-d", "--digest DIGEST", [:md5, :sha1],
         "Digest algorithm to use.  Currently supported:",
         "  md5", "  sha1",
         "(default #{options.digest.to_s})", ' ') do |digest|
      options.digest = digest
         end

    p.on("--print-sample-count",
          "outputs the number of samples used to create the hash") do
      options.print_sample_count = true
          end


    p.on("-r", "--read-size SIZE", Integer,
         "Size of chunk to read, in bytes " +
         "(#{options.read_size})") do |size|
      options.read_size = size
         end

    p.on("-s", "--seek-size SIZE", Integer,
         "Size of chunk to skip after each read, in bytes " + 
         "(#{options.seek_size})") do |size|
      options.seek_size = size
         end

    p.on_tail("-v", "--version", "Show version") do
      puts Digestif.version_string
      exit 0
    end

    p.on_tail("-h", "--help", "This is it") do
      puts p
      exit 0
    end

  end

  begin
    parser.parse!(args)
  rescue OptionParser::ParseError => e
    error e
  end

  options
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/digestif/cli.rb', line 27

def run
  # validate files first - fail fast
  args.each do |file|
    unless File.exists?(file)
      error "file not found: #{file}"
    end
  end

  # engage hasher
  args.each do |file|
    puts Hasher.new(file, options).digest
  end
end