Class: Keigan::Cli::Application
- Inherits:
-
Object
- Object
- Keigan::Cli::Application
- Defined in:
- lib/keigan/cli/application.rb
Overview
Application class for Keigan
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
Instance Method Summary collapse
-
#db_connect ⇒ Object
Establishes an [ActiveRecord::Base] database connection.
-
#initialize ⇒ Application
constructor
Initializes a CLI Application.
-
#load_config(file = CONFIG_FILE, in_memory_config = false) ⇒ Object
Loads the configuration file.
-
#parse_options ⇒ Object
Parses all the command line.
-
#run ⇒ Object
Main Application loop, handles all of the command line arguments and parsing of files on the command line.
-
#test_connection? ⇒ Boolean
Tests the database connection.
Constructor Details
#initialize ⇒ Application
Initializes a CLI Application
37 38 39 40 41 42 |
# File 'lib/keigan/cli/application.rb', line 37 def initialize @options = {} @database = {} @options[:debug] = false end |
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
33 34 35 |
# File 'lib/keigan/cli/application.rb', line 33 def database @database end |
Instance Method Details
#db_connect ⇒ Object
Establishes an [ActiveRecord::Base] database connection
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 |
# File 'lib/keigan/cli/application.rb', line 81 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) ActiveRecord::Base.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, in_memory_config = false) ⇒ Object
Loads the configuration file
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 |
# File 'lib/keigan/cli/application.rb', line 49 def load_config(file=CONFIG_FILE, in_memory_config=false) if File.exists?(file) == true or in_memory_config == true begin if in_memory_config yaml = YAML::load(file) else yaml = YAML::load(File.open(file)) end @database = yaml["database"] @report = yaml["report"] puts @database.inspect if @options[:debug] #If no values were entered put a default value in @report.each do |k, v| if v == nil @report[k] = "No #{k}" end end rescue => e puts "[!] Error loading configuration! - #{e.}" exit end else puts "[!] Configuration file does not exist!" exit end end |
#parse_options ⇒ Object
Parses all the command line
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/keigan/cli/application.rb', line 127 def begin opts = OptionParser.new do |opt| opt. = "#{APP_NAME} v#{VERSION}\n#{AUTHOR}\n#{SITE}\n\n" opt. << "Usage: #{APP_NAME} [options]" 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 configuration file does not exist. Please specify a file that exists." 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.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_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
Main Application loop, handles all of the command line arguments and parsing of files on the command line
187 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 |
# File 'lib/keigan/cli/application.rb', line 187 def run if @options[:debug] == true puts "[*] Enabling Debug Mode" end if @options[:config_file] != nil load_config @options[:config_file] else load_config end if @options[:test_connection] != nil result = test_connection? puts "#{result[1]}" exit end db_connect puts "Keigan Web Interface at http://localhost:8969/" Keigan::Web::Application.run! end |
#test_connection? ⇒ Boolean
Tests the database connection
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/keigan/cli/application.rb', line 110 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 |