Class: Scl::Control::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/scl/control/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Controller

Returns a new instance of Controller.



5
6
7
8
9
# File 'lib/scl/control/controller.rb', line 5

def initialize(args)
  @args = args
  puts "Using output format #{output_encoder.name}" if verbose?
  puts "Using input format #{input_decoder.name}" if verbose?
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



4
5
6
# File 'lib/scl/control/controller.rb', line 4

def args
  @args
end

Instance Method Details

#coder_for(format) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/scl/control/controller.rb', line 45

def coder_for(format)
  case "#{format}".strip
  when "base64"               then Format::BASE64
  when "qrcode"               then Format::QRCODE
  when "words"                then Format::WORDS
  when "hex"                  then Format::HEX
  when "binary","text","none" then Format::BINARY
  when "", "auto"             then Format::AUTO
  when "stdout"               then Format::STDOUT
  else
    puts "Unexpected format \"#{format}\""
    exit(1)
  end
end

#confirm_overwrite?(filename) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/scl/control/controller.rb', line 80

def confirm_overwrite?(filename)
  if File.exists?(filename)
    puts "File #{filename} already exists. Confirm overwrite? [Yn]"
    if gets.strip.downcase == 'y'
      puts "Confirmed overwrite for #{filename}" if verbose?
      return true
    else
      puts "Skipping save for #{filename}" if verbose?
      return false
    end
  end
  return true
end

#input_decoderObject



15
16
17
# File 'lib/scl/control/controller.rb', line 15

def input_decoder
  coder_for(args.input_format)
end

#key_coderObject



19
20
21
# File 'lib/scl/control/controller.rb', line 19

def key_coder
  coder_for(args.key_format)
end

#module(module_name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/scl/control/controller.rb', line 31

def module(module_name)
  case module_name
  when "aes" then Control::AES.new(self)
  when "rsa" then Control::RSA.new(self)
  when "dh"  then Control::DH.new(self)
  when "sss"  then Control::SSS.new(self)
  when "digest"  then Control::Digest.new(self)
  else
    puts "No scl module found \"#{module_name}\""
    puts args.opts
    exit(1)
  end
end

#output(*results) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/scl/control/controller.rb', line 60

def output(*results)
  case output_file
  when '' then
    puts "\n\n"
    puts results.compact.map{|r| output_encoder.encode(r.content) }.join("\n\n")
  else
    results.compact.each do |result|
      puts "Writing #{result.file(output_file)}" if verbose?
      if args.output_format == 'stdout'
        output_encoder.encode(result.content)
      else
        IO.write(
          result.file(output_file),
          output_encoder.encode(result.content)
        ) if confirm_overwrite?(result.file(output_file))
      end
    end
  end
end

#output_encoderObject



11
12
13
# File 'lib/scl/control/controller.rb', line 11

def output_encoder
  coder_for(args.output_format)
end

#output_fileObject



23
24
25
# File 'lib/scl/control/controller.rb', line 23

def output_file
  "#{@args.output_file}".strip
end

#verbose?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/scl/control/controller.rb', line 27

def verbose?
  @args.verbose
end