Class: Kekkan::CLI::Application
- Inherits:
-
Object
- Object
- Kekkan::CLI::Application
- Includes:
- Base
- Defined in:
- lib/kekkan/cli/application.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
Instance Method Summary collapse
-
#consolize(&block) ⇒ Object
Starts a console and executes anything in a block sent to it.
- #create_config(file = CONFIG_FILE) ⇒ Object
- #db_connect ⇒ Object
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #load_config(file = CONFIG_FILE, memory_config = false) ⇒ Object
-
#migrate(direction) ⇒ Object
Initiator for [ActiveRecord] migrations.
- #parse_file(file) ⇒ Object
- #parse_options ⇒ Object
- #run ⇒ Object
- #test_connection? ⇒ Boolean
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
36 37 38 39 40 41 |
# File 'lib/kekkan/cli/application.rb', line 36 def initialize @options = {} @database = {} @options[:debug] = false end |
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
33 34 35 |
# File 'lib/kekkan/cli/application.rb', line 33 def database @database end |
Instance Method Details
#consolize(&block) ⇒ Object
Starts a console and executes anything in a block sent to it
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/kekkan/cli/application.rb', line 172 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
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/kekkan/cli/application.rb', line 44 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_connect ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/kekkan/cli/application.rb', line 122 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.}\n #{ans.backtrace}" if @options[:debug] exit rescue ActiveRecord::AdapterNotFound => anf puts "[!] Database adapter not found, please check your configuration file" puts "#{anf.}\n #{anf.backtrace}" if @options[:debug] exit rescue => e puts "[!] Exception! #{e.}\n #{e.backtrace}" end end |
#load_config(file = CONFIG_FILE, memory_config = false) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/kekkan/cli/application.rb', line 58 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.}" exit end else puts "[!] Configuration file does not exist!" exit end end |
#migrate(direction) ⇒ Object
Initiator for [ActiveRecord] migrations.
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 114 115 116 117 118 119 |
# File 'lib/kekkan/cli/application.rb', line 84 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.}\n #{sqlitex.backtrace}" if @options[:debug] continue rescue ActiveRecord::AdapterNotSpecified => ans puts "[!] Database adapter not found, please check your configuration file" puts "#{ans.}\n #{ans.backtrace}" if @options[:debug] exit rescue ActiveRecord::AdapterNotFound => anf puts "[!] Database adapter not found, please check your configuration file" puts "#{ans.}\n #{ans.backtrace}" if @options[:debug] exit rescue => e puts "[!] Exception! #{e.}\n#{e.backtrace}" exit end end |
#parse_file(file) ⇒ Object
283 284 285 286 287 288 289 290 291 292 |
# File 'lib/kekkan/cli/application.rb', line 283 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_options ⇒ Object
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 275 276 277 278 279 280 |
# File 'lib/kekkan/cli/application.rb', line 194 def begin opts = OptionParser.new do |opt| opt. = "#{APP_NAME} v#{VERSION}\nJacob Hammack\n#{SITE}\n\n" opt. << "Usage: #{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 |
#run ⇒ Object
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 341 342 343 344 345 346 |
# File 'lib/kekkan/cli/application.rb', line 295 def run 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.}\n #{e.backtrace.join("\n")}\n" puts "[!] Error: #{file}" next end end end |
#test_connection? ⇒ Boolean
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/kekkan/cli/application.rb', line 154 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.}\n #{e.backtrace}" end end |