Class: Megam::App

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/megam/app.rb

Constant Summary collapse

NO_COMMAND_GIVEN =

include(a_module) is called inside a class, and adds module methods as instance methods. extend(a_module) adds all module methods to the instance it is called on.

"You need to pass a command with option (e.g., birr -i <install_path>)\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/megam/app.rb', line 74

def initialize
  super # The super calls the mixlib cli.

  ##Traps are being set for the following when an application starts.
  ##SIGHUP        1       Term    Hangup detected on controlling terminal
  ##                             or death of controlling process
  ##SIGINT        2       Term    Interrupt from keyboard
  ##SIGQUIT       3       Core    Quit from keyboard
  trap("TERM") do
    Megam::App.fatal!("SIGTERM received, stopping", 1)
  end

  trap("INT") do
    Megam::App.fatal!("SIGINT received, stopping", 2)
  end

  trap("QUIT") do
    Megam::Log.info("SIGQUIT received, call stack:\n  " + caller.join("\n  "))
  end

  @text ||= Megam::Text.new(STDOUT, STDERR, STDIN, {})

end

Instance Attribute Details

#name_argsObject

attr_accessors are setters/getters in ruby. The arguments are filtered and available for use to subclasses.



71
72
73
# File 'lib/megam/app.rb', line 71

def name_args
  @name_args
end

#textObject

Returns the value of attribute text.



73
74
75
# File 'lib/megam/app.rb', line 73

def text
  @text
end

Class Method Details

.debug_stacktrace(e) ⇒ Object

The exception in ruby carries the class, message and the trace. www.ruby-doc.org/core-2.0/Exception.html



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/megam/app.rb', line 164

def debug_stacktrace(e)
  message = "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
  megam_stacktrace_out = "Generated at #{Time.now.to_s}\n"
  megam_stacktrace_out += message

  #after the message is formulated in the variable megam_stack_trace_out, its
  #stored in a file named megam-stacktrace.out
  Megam::FileCache.store("megam-stacktrace.out", megam_stacktrace_out)

  ##The same error is logged in the log file saying, go look at megam-stacktrace.out for error.
  Megam::Log.fatal("Stacktrace dumped to #{Megam::FileCache.load("megam-stacktrace.out", false)}")
  Megam::Log.debug(message)
  true
end

.exit!(msg, err = -1)) ⇒ Object



185
186
187
188
# File 'lib/megam/app.rb', line 185

def exit!(msg, err = -1)
  Megam::Log.debug(msg)
  Process.exit err
end

.fatal!(msg, err = -1)) ⇒ Object

Log a fatal error message to both STDERR and the Logger, exit the application



180
181
182
183
# File 'lib/megam/app.rb', line 180

def fatal!(msg, err = -1)
  Megam::Log.fatal(msg)
  Process.exit err
end

Instance Method Details

#parse_options(args) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/megam/app.rb', line 109

def parse_options(args)
super
  rescue OptionParser::InvalidOption => e
puts "Error: " + e.to_s
puts self.opt_parser
exit(1)
end

#runObject

Run the “birr app”. Let it roam and stay by our side.[Go birr..Does it remind of the Hutch adv.]. The first thing run does is it parses the options. Once the first level of parsing is done, ie the help, no_command, sub_command entry is verified it proceeds to call Megam_Birr with the user entered options and arguments (ARGV)



102
103
104
105
106
107
# File 'lib/megam/app.rb', line 102

def run
  Mixlib::Log::Formatter.show_time = false
  validate_and_parse_options
  Megam::Birr.new.run(@named_args, config)
  exit 0
end