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
# 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

  run_command
end

Instance Method Details

#decryptObject



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

def decrypt
  CICI::Decrypt.new(@ui, @config).start(@options.set)
end

#encryptObject



84
85
86
# File 'lib/cici/cli.rb', line 84

def encrypt
  CICI::Encrypt.new(@ui, @config).start
end

#parse_optionsObject



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

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


42
43
44
45
# File 'lib/cici/cli.rb', line 42

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

#run_commandObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cici/cli.rb', line 30

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