Class: CICI::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cici/cli.rb', line 18

def initialize
  @options = parse_options

  @ui = CICI::UI.new(@options.verbose, @options.debug)
  @ui.debug("Options: #{@options}")

  @config = CICI::Config.new(@ui)
  @config.load

  @decrypter = CICI::Decrypt.new(@ui, @config)
  @encrypter = CICI::Encrypt.new(@ui, @decrypter, @config)

  run_command
end

Instance Method Details

#decryptObject



91
92
93
# File 'lib/cici/cli.rb', line 91

def decrypt
  @decrypter.start(@options.set)
end

#encryptObject



87
88
89
# File 'lib/cici/cli.rb', line 87

def encrypt
  @encrypter.start
end

#parse_optionsObject



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
# File 'lib/cici/cli.rb', line 50

def parse_options
  options = Options.new
  options.verbose = false
  options.debug = false

  opt_parser = OptionParser.new do |opts|
    opts.banner = 'Usage: cici encrypt|decrypt [options]'

    opts.on('-v', '--version', 'Print version') do
      puts CICI::Version.get
      exit
    end
    opts.on('--verbose', 'Verbose output') do
      options.verbose = true
    end
    opts.on('--debug', 'Debug output (also turns on verbose)') do
      options.verbose = true
      options.debug = true
    end
    opts.on('--set SET_NAME', 'Set to decrypt (Note: option ignored for encrypt command)') do |set_name|
      options.set = set_name
    end
    opts.on('-h', '--help', 'Prints this help') do
      puts opts
      exit
    end
  end

  help = opt_parser.help
  options.help = help
  abort(help) if ARGV.empty?

  opt_parser.parse!(ARGV)

  options
end


45
46
47
48
# File 'lib/cici/cli.rb', line 45

def print_help(exit_code)
  puts @options.help
  exit exit_code
end

#run_commandObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cici/cli.rb', line 33

def run_command
  case ARGV[0]
  when 'encrypt'
    encrypt
  when 'decrypt'
    decrypt
  else
    @ui.fail('Command invalid.')
    print_help(1)
  end
end