lib/puppet-herald.rb in puppet-herald-0.2.0 vs lib/puppet-herald.rb in puppet-herald-0.8.0
- old
+ new
@@ -3,85 +3,115 @@
rescue LoadError # rubocop:disable Lint/HandleExceptions
# Do nothing here
end
require 'puppet-herald/database'
+require 'logger'
# A module for Herald
module PuppetHerald
@root = File.dirname(File.dirname(File.realpath(__FILE__)))
@database = PuppetHerald::Database.new
+ @logger = Logger.new STDOUT
+ @errlogger = Logger.new STDERR
- # A database object
- # @return [PuppetHerald::Database] a database object
- def self.database # rubocop:disable Style/TrivialAccessors
- @database
- end
+ class << self
+ # Logger for CLI interface (error)
+ # @return [Logger] logger for CLI
+ attr_reader :errlogger
- # Calculates a replative directory inside the project
- #
- # @param dir [String] a sub directory
- # @return [String] a full path to replative dir
- def self.relative_dir(dir)
- File.realpath(File.join @root, dir)
- end
+ # Logger for CLI interface
+ # @return [Logger] logger for CLI
+ attr_reader :logger
- def self.environment=(environment)
- rackenv = :production
- envsymbol = environment.to_s.to_sym
- case envsymbol
- when :dev, :development
- rackenv = :development
- when :test, :ci
- rackenv = :test
- else
- rackenv = :production
- envsymbol = :production
+ # A database object
+ # @return [PuppetHerald::Database] a database object
+ def database # rubocop:disable Style/TrivialAccessors
+ @database
end
- ENV['PUPPET_HERALD_ENV'] = envsymbol.to_s
- ENV['RACK_ENV'] = rackenv.to_s
- end
- # Gets the environment set for Herald
- # @return [Symbol] an environment
- def self.environment
- env = :production
- env = ENV['PUPPET_HERALD_ENV'].to_sym unless ENV['PUPPET_HERALD_ENV'].nil?
- ENV['RACK_ENV'] = env.to_s
- env
- end
+ # Calculates a replative directory inside the project
+ #
+ # @param dir [String] a sub directory
+ # @return [String] a full path to replative dir
+ def relative_dir(dir)
+ File.realpath(File.join @root, dir)
+ end
- # Checks is running in DEVELOPMENT kind of environment (dev, ci, test)
- #
- # @return [Boolean] true if runs in development
- def self.in_dev?
- [:development, :dev, :test, :ci].include? environment
- end
+ def environment=(newenv)
+ envsymbol = newenv.to_s.to_sym
+ ENV['PUPPET_HERALD_ENV'] = envsymbol.to_s
+ rackenv
+ setup_logger
+ end
- # Checks is running in production environment
- #
- # @return [Boolean] true if runs in production
- def self.in_prod?
- !in_dev?
- end
+ # Setups logger's level
+ #
+ # @return [nil]
+ def setup_logger
+ logger.level = self.in_dev? ? Logger::DEBUG : Logger::INFO
+ errlogger.level = logger.level
+ nil
+ end
- # Reports a bug in desired format
- #
- # @param ex [Exception] an exception that was thrown
- # @return [Hash] a hash with info about bug to be displayed to user
- def self.bug(ex)
- file = Tempfile.new(['puppet-herald-bug', '.log'])
- filepath = file.path
- file.close
- file.unlink
- message = "v#{PuppetHerald::VERSION}-#{ex.class}: #{ex.message}"
- contents = message + "\n\n" + ex.backtrace.join("\n") + "\n"
- File.write(filepath, contents)
- bugo = {
- message: message,
- homepage: PuppetHerald::HOMEPAGE,
- bugfile: filepath,
- help: "Please report this bug to #{PuppetHerald::HOMEPAGE} by passing contents of bug file: #{filepath}"
- }
- bugo
+ # Gets the environment set for Herald
+ #
+ # @return [Symbol] an environment
+ def environment
+ ENV['PUPPET_HERALD_ENV'] = :production.to_s if ENV['PUPPET_HERALD_ENV'].nil?
+ ENV['PUPPET_HERALD_ENV'].to_sym
+ end
+
+ # Rack environment
+ #
+ # @return [Symbol] Rack environment
+ def rackenv
+ case environment
+ when :dev, :development
+ rackenv = :development
+ when :test, :ci
+ rackenv = :test
+ else
+ rackenv = :production
+ end
+ ENV['RACK_ENV'] = rackenv.to_s
+ rackenv
+ end
+
+ # Checks is running in DEVELOPMENT kind of environment (dev, ci, test)
+ #
+ # @return [Boolean] true if runs in development
+ def in_dev?
+ [:development, :dev, :test, :ci].include? environment
+ end
+
+ # Checks is running in production environment
+ #
+ # @return [Boolean] true if runs in production
+ def in_prod?
+ !in_dev?
+ end
+
+ # Reports a bug in desired format
+ #
+ # @param ex [Exception] an exception that was thrown
+ # @return [Hash] a hash with info about bug to be displayed to user
+ def bug(ex)
+ file = Tempfile.new(['puppet-herald-bug', '.log'])
+ filepath = file.path
+ file.close
+ file.unlink
+ message = "v#{PuppetHerald::VERSION}-#{ex.class}: #{ex.message}"
+ contents = message + "\n\n" + ex.backtrace.join("\n") + "\n"
+ File.write(filepath, contents)
+ bugo = {
+ message: message,
+ homepage: PuppetHerald::HOMEPAGE,
+ bugfile: filepath,
+ help: "Please report this bug to #{PuppetHerald::HOMEPAGE} by passing contents of bug file: #{filepath}"
+ }
+ bugo
+ end
end
+ PuppetHerald.rackenv
+ PuppetHerald.setup_logger
end