class Megam::Birr
Main entry for the command line, after validating the class, run the install_file
Attributes
Public Class Methods
I don’t think we need this method. Will remove it later. We need to just call text.msg
# File lib/megam/birr.rb 31 def self.msg(msg="") 32 text.msg(msg) 33 end
Create a new instance of the current class configured for the given arguments and options
# File lib/megam/birr.rb 37 def initialize() 38 end
text is used to print stuff in the terminal (message, log, info, warn etc.)
# File lib/megam/birr.rb 25 def self.text 26 @text ||= Megam::Text.new(STDOUT, STDERR, STDIN, {}) 27 end
Private Class Methods
# File lib/megam/birr.rb 172 def self.working_directory 173 ENV['PWD'] || Dir.pwd 174 end
Public Instance Methods
Catch-all method that does any massaging needed for various config components, such as expanding file paths and converting verbosity level into log level.
# File lib/megam/birr.rb 88 def apply_computed_config 89 90 case config[:verbosity] 91 when 0, nil 92 config[:log_level] = :error 93 when 1 94 config[:log_level] = :info 95 else 96 config[:log_level] = :debug 97 end 98 99 Mixlib::Log::Formatter.show_time = false 100 Megam::Log.init(config[:log_location] || STDOUT) 101 Megam::Log.level(config[:log_level] || :error) 102 103 config.each do |key, val| 104 Megam::Config[key] = val 105 end 106 107 end
configure meggy, to startwith locate the config file under .meggy/Birr.rb Once located, read the Birr.rb config file. parse them, and report any ruby syntax errors. if not merge then inside Meggy::Config object.
# File lib/megam/birr.rb 56 def configure_birr 57 # look for a .birr/birr.rb for configuration. Not used currently. 58 unless config[:install_file] 59 locate_install_file 60 end 61 62 # Now load the installation file provided as input {-i} parm. 63 if config[:install_file] 64 @text.info "Using #{config[:install_file]}" 65 apply_computed_config 66 read_config_file(config[:install_file]) 67 else 68 text.warn("No birr configuration file found") 69 end 70 71 end
ERROR: You have invalid ruby syntax in your config file /home/ram/.meggy/Birr.rb /home/ram/.meggy/Birr.rb:9: syntax error, unexpected ‘=’ Birr > = “admin”
^ # /home/ram/.meggy/Birr.rb 8: meggy_server_url 'http://localhost:6167' 9: Birr[:username] > = "admin" 10: Birr[:password] = "team4dog" Line 9 is marked in red, and the 3rd line where the error is show is highlighted in red.
This is in case of a ruby parse error.
# File lib/megam/birr.rb 144 def highlight_config_error(file, line) 145 config_file_lines = [] 146 147 # A file line is split into the line number (index) and the line content. 148 # The line number is converted to string (to_s), right justified 3 characters with a colon, and its trimmed (chomp) 149 150 IO.readlines(file).each_with_index {|l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}"} 151 # mark the appropriate line with a red color, if its just one line, then mark the zeroth line. 152 # if not get the range (deducting 2), and mark the second line. 153 if line == 1 154 lines = config_file_lines[0..3] 155 lines[0] = text.color(lines[0], :red) 156 else 157 lines = config_file_lines[Range.new(line - 2, line)] 158 lines[1] = text.color(lines[1], :red) 159 end 160 text.msg "" 161 # print the name of the file in white 162 text.msg text.color(" # #{file}", :white) 163 # print the rest of the line. 164 lines.each {|l| text.msg(l)} 165 text.msg "" 166 end
# File lib/megam/birr.rb 73 def locate_install_file 74 # Look for $HOME/.meggy/birr.rb, this aint' supported currently 75 # the idea is to allow configuration of the options used here. 76 if ENV['HOME'] 77 user_config_file = File.expand_path(File.join(ENV['HOME'], '.birr', 'birr.rb')) 78 end 79 80 if File.exist?(user_config_file) 81 config[:install_file] = user_config_file 82 end 83 end
load the content as provided in -i {installation_file} ruby errors get reported or else the file is executed.
# File lib/megam/birr.rb 111 def read_config_file(file) 112 self.instance_eval(IO.read(file), file, 1) 113 rescue SyntaxError => e 114 @text.error "You have invalid ruby syntax in your config file #{file}" 115 @text.info(text.color(e.message, :red)) 116 if file_line = e.message[/#{Regexp.escape(file)}:[\d]+/] 117 line = file_line[/:([\d]+)$/, 1].to_i 118 highlight_config_error(file, line) 119 end 120 exit 1 121 rescue Exception => e 122 @text.error "You have an error in your config file #{file}" 123 @text.info "#{e.class.name}: #{e.message}" 124 filtered_trace = e.backtrace.grep(/#{Regexp.escape(file)}/) 125 filtered_trace.each {|line| text.msg(" " + text.color(line, :red))} 126 if !filtered_trace.empty? 127 line_nr = filtered_trace.first[/#{Regexp.escape(file)}:([\d]+)/, 1] 128 highlight_config_error(file, line_nr.to_i) 129 end 130 131 exit 1 132 end
Run Birr
for the given args
(ARGV), adding options
to the list of CLI options that the subcommand knows how to handle.
Arguments¶ ↑
- args:
-
usually ARGV
- options:
-
A Mixlib::CLI option parser hash. These
options
are how
subcommands know about global Birr
CLI options
# File lib/megam/birr.rb 46 def run(args=[], config={}) 47 @config = config.dup 48 @text ||= Megam::Text.new(STDOUT, STDERR, STDIN, config) 49 # configure your Birr. 50 configure_birr 51 end