Class: Kekkan::CLI::Application

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/kekkan/cli/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



30
31
32
33
34
35
# File 'lib/kekkan/cli/application.rb', line 30

def initialize
  @options = {}
  @database = {}

  @options[:debug] = false
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



27
28
29
# File 'lib/kekkan/cli/application.rb', line 27

def database
  @database
end

Instance Method Details

#consolize(&block) ⇒ Object

Starts a console and executes anything in a block sent to it

Parameters:

  • block

    Code block to transfer control



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/kekkan/cli/application.rb', line 166

def consolize &block

  yield

  IRB.setup(nil)
  IRB.conf[:USE_READLINE] = true
  IRB.conf[:PROMPT_MODE] = :SIMPLE

  irb = IRB::Irb.new
  IRB.conf[:MAIN_CONTEXT] = irb.context

  irb.context.evaluate("require 'irb/completion'", 0)

  trap("SIGINT") do
    irb.signal_handle
  end
  catch(:IRB_EXIT) do
    irb.eval_input
  end
end

#create_config(file = CONFIG_FILE) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kekkan/cli/application.rb', line 38

def create_config file=CONFIG_FILE
  File.open(file, 'w+') do |f|
    f.write("database:\n")
    f.write("  adapter: \n")
    f.write("  host: \n")
    f.write("  port: \n")
    f.write("  database: \n")
    f.write("  username: \n")
    f.write("  password: \n")
    f.write("  timeout: \n\n")
  end
end

#db_connectObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/kekkan/cli/application.rb', line 116

def db_connect
  begin
    if @database["adapter"] == nil
      puts "[!] #{@database['adapter']}" if @options[:debug]

      return false, "[!] Invalid database adapter, please check your configuration file"
    end

    ActiveRecord::Base.establish_connection(@database)
    connection = ActiveRecord::Base.connection

    if @database["adapter"] =~ /sqlite/
      connection.execute("PRAGMA default_synchronous=OFF;")
      connection.execute("PRAGMA synchronous=OFF;")
      connection.execute("PRAGMA journal_mode=OFF;")
    end

    connection
  rescue ActiveRecord::AdapterNotSpecified => ans
    puts "[!] Database adapter not found, please check your configuration file"
    puts "#{ans.message}\n #{ans.backtrace}" if @options[:debug]
    exit
  rescue ActiveRecord::AdapterNotFound => anf
    puts "[!] Database adapter not found, please check your configuration file"
    puts "#{anf.message}\n #{anf.backtrace}" if @options[:debug]
    exit
  rescue => e
    puts "[!] Exception! #{e.message}\n #{e.backtrace}"
  end
end

#load_config(file = CONFIG_FILE, memory_config = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kekkan/cli/application.rb', line 52

def load_config file=CONFIG_FILE, memory_config=false
  if File.exists?(file) == true or memory_config == true
    begin
      if memory_config
        yaml = YAML::load(file)
      else
        yaml = YAML::load(File.open(file))
      end

      @database = yaml["database"]

      puts @database.inspect if @options[:debug]

    rescue => e
      puts "[!] Error loading configuration! - #{e.message}"
      exit
    end
  else
    puts "[!] Configuration file does not exist!"
    exit
  end
end

#migrate(direction) ⇒ Object

Initiator for [ActiveRecord] migrations.

Parameters:

  • direction (Symbol)

    :up or :down



78
79
80
81
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
111
112
113
# File 'lib/kekkan/cli/application.rb', line 78

def migrate direction
  begin
    if @database["adapter"] == nil
      return false, "[!] Invalid database adapter, please check your configuration file"
    end

    ActiveRecord::Base.establish_connection(@database)
    require 'kekkan/base/schema'
    Schema.migrate(direction)

    if direction == :up
      puts "[*] Creating tables"
      ver = Version.create
      ver.version = Kekkan::VERSION
      ver.save
    end

    puts "[*] Dropping tables" if direction == :down

  #@todo temp hack, fix this by checking the schema on :up or :down for exiting data
  rescue SQLite3::SQLException => sqlitex
    puts "#{sqlitex.message}\n #{sqlitex.backtrace}" if @options[:debug]
    continue
  rescue ActiveRecord::AdapterNotSpecified => ans
    puts "[!] Database adapter not found, please check your configuration file"
    puts "#{ans.message}\n #{ans.backtrace}" if @options[:debug]
    exit
  rescue ActiveRecord::AdapterNotFound => anf
    puts "[!] Database adapter not found, please check your configuration file"
    puts "#{ans.message}\n #{ans.backtrace}" if @options[:debug]
    exit
  rescue => e
    puts "[!] Exception! #{e.message}\n#{e.backtrace}"
    exit
  end
end

#parse_file(file) ⇒ Object



277
278
279
280
281
282
283
284
285
286
# File 'lib/kekkan/cli/application.rb', line 277

def parse_file file
  begin
    parser = Nokogiri::XML::SAX::Parser.new(Kekkan::Parsers::Cve2Document.new)

    parser.parse(File.open(file))

  rescue => e
    raise e
  end
end

#parse_optionsObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/kekkan/cli/application.rb', line 188

def parse_options
  begin
    opts = OptionParser.new do |opt|
      opt.banner = "#{Kekkan::APP_NAME} v#{Kekkan::VERSION}\nJacob Hammack\n#{Kekkan::SITE}\n\n"
      opt.banner << "Usage: #{Kekkan::APP_NAME} [options] [files_to_parse]"

      opt.separator('')
      opt.separator('Configuration Options')

      opt.on('--config-file FILE', "Loads configuration settings for the specified file. By default #{APP_NAME} loads #{CONFIG_FILE}") do |option|
        if File.exists?(option) == true
          @options[:config_file] = option
        else
          puts "[!] Specified config file does not exist. Please specify a file that exists."
          exit
        end
      end

      opt.on('--create-config-file [FILE]',"Creates a configuration file in the current directory with the specified name, Default is #{CONFIG_FILE}") do |option|
        if option == nil
          option = CONFIG_FILE
        end

        if File.exists?(option) == true
          puts "[!] Configuration file already exists; If you wish to over-write this file please delete it."
        else
          if option == nil
            create_config
          else
            create_config option
          end

          exit
        end
      end

    opt.separator('')
      opt.separator('Database Options')

      opt.on('--test-connection','Tests the database connection settings') do |option|
        @options[:test_connection] = option
      end

      opt.on('--create-tables',"Creates the tables required for #{APP_NAME}") do |option|
        @options[:create_tables] = option
      end

      opt.on('--drop-tables', "Deletes the tables and data from #{APP_NAME}") do |option|
        @options[:drop_tables] = option
      end

    opt.separator ''
      opt.separator 'Other Options'

      opt.on_tail('-v', '--version', "Shows application version information") do
        puts "#{APP_NAME}: #{VERSION}\nRuby Version: #{RUBY_VERSION}\nRubygems Version: #{Gem::VERSION}"
        exit
      end

      opt.on('-d','--debug','Enable Debug Mode (More verbose output)') do |option|
        @options[:debug] = true
      end

      opt.on('--console', 'Starts an ActiveRecord console into the configured database') do |option|
        @options[:console] = option
      end

      opt.on_tail("-?", "--help", "Show this message") do
        puts opt.to_s + "\n"
        exit
      end
    end

    if ARGV.length != 0
      opts.parse!
    else
      puts opts.to_s + "\n"
      exit
    end
  rescue OptionParser::MissingArgument => m
    puts opts.to_s + "\n"
    exit
  rescue OptionParser::InvalidOption => i
    puts opts.to_s + "\n"
    exit
  end
end

#runObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/kekkan/cli/application.rb', line 289

def run
  parse_options

  if @options[:debug]
    puts "[*] Enabling Debug Mode"
  end

  if @options[:config_file] != nil
    load_config @options[:config_file]
  else
    load_config
  end

  db_connect

  if @options[:console] != nil
    consolize do
      puts Kekkan::CLI::Banner
      puts "#{APP_NAME} Console v#{VERSION}"
    end
    exit
  end

  if @options[:test_connection] != nil
    result = test_connection?

    puts "#{result[1]}"
    exit
  end

  if @options[:create_tables] != nil
    migrate(:up)
    exit
  end

  if @options[:drop_tables] != nil
    migrate(:down)
    exit
  end

  ARGV.each do |file|
    begin
      parse_file file

    rescue => e
      puts e.inspect
      puts "[!] #{e.message}\n #{e.backtrace.join("\n")}\n"
      puts "[!] Error: #{file}"
      next
    end
  end
end

#test_connection?Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/kekkan/cli/application.rb', line 148

def test_connection?
  begin

    db_connect

    if ActiveRecord::Base.connected? == true
      return true, "[*] Connection Test Successful"
    else
      return false, "[!] Connection Test Failed"
    end
  rescue => e
    puts "[!] Exception! #{e.message}\n #{e.backtrace}"
  end
end