80 lines
1.6 KiB
Ruby
80 lines
1.6 KiB
Ruby
require "colorize"
|
|
|
|
# @example
|
|
# BootFrameWork::Application.new(ENV).boot do
|
|
# harshly_need_env "VARIABLE_NAME1"
|
|
# kindly_ask_env "VARIABLE_NAME2" { do_something_if_failure() }
|
|
# harshly_do true
|
|
# kindly_do env["SOME_OTHER_VARIABLE"] { error "this should not happen :(((" }
|
|
# end.finish!
|
|
module BootFramework
|
|
class Application
|
|
attr_reader :env, :stop
|
|
|
|
def initialize(env)
|
|
@env = env
|
|
@stop = false
|
|
end
|
|
|
|
private def message(string)
|
|
$stderr.puts string
|
|
end
|
|
|
|
def error(string)
|
|
message "Error: #{string}".red
|
|
end
|
|
|
|
def warning(string)
|
|
message "Warning: #{string}".yellow
|
|
end
|
|
|
|
def boot(&block)
|
|
instance_eval(&block)
|
|
self
|
|
end
|
|
|
|
def finish!
|
|
if @stop
|
|
exit 1
|
|
end
|
|
self
|
|
end
|
|
|
|
def will_stop!
|
|
@stop = true
|
|
end
|
|
|
|
def harshly_need_env(env_variable, &block)
|
|
@env.fetch env_variable do
|
|
error "\"#{env_variable}\" is a required ENV variable. Provide it.".red
|
|
will_stop!
|
|
instance_eval(&block) if block_given?
|
|
end
|
|
end
|
|
|
|
def kindly_ask_env(env_variable, &block)
|
|
@env.fetch env_variable do
|
|
warning "\"#{env_variable}\" is a prefered ENV variable. Provide it if possible.".yellow
|
|
instance_eval(&block) if block_given?
|
|
end
|
|
end
|
|
|
|
def harshly_do(test, &block)
|
|
if test
|
|
instance_eval(&block) if block_given?
|
|
will_stop!
|
|
end
|
|
end
|
|
|
|
def kindly_do(test, &block)
|
|
if test
|
|
instance_eval(&block) if block_given?
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.boot_application(&block)
|
|
Application.new(ENV).boot(&block)
|
|
end
|
|
end
|