class Grantinee::CLI

Attributes

dsl[RW]
engine[RW]
options[RW]

Public Class Methods

new(args = ARGV, logger = ::Logger.new($stderr)) click to toggle source
# File lib/grantinee/cli.rb, line 9
def initialize(args = ARGV, logger = ::Logger.new($stderr))
  @args    = args
  @logger  = logger

  @options = {}
end

Public Instance Methods

run!() click to toggle source
# File lib/grantinee/cli.rb, line 16
def run!
  parse_command_line_parameters
  process_command_line_parameters

  @dsl      = build_dsl
  @engine   = build_engine
  @executor = build_executor
  @executor.run!

  [@dsl, @engine, @executor]
end

Private Instance Methods

build_dsl() click to toggle source
# File lib/grantinee/cli.rb, line 87
def build_dsl
  Grantinee.configuration.logger = @logger
  Grantinee::Dsl.eval(File.read(@options[:file]))
end
build_engine() click to toggle source
# File lib/grantinee/cli.rb, line 92
def build_engine
  Grantinee::Engine.for Grantinee.configuration.engine
end
build_executor() click to toggle source
# File lib/grantinee/cli.rb, line 96
def build_executor
  Grantinee::Executor.new(@dsl, @engine)
end
parse_command_line_parameters() click to toggle source
# File lib/grantinee/cli.rb, line 30
def parse_command_line_parameters # rubocop:disable Metrics/MethodLength
  parser = OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength
    opts.banner = "Usage: grantinee [options]"

    # Help
    opts.on(
      '-h', '--help',
      "Displays help"
    ) do
      puts opts
      exit
    end

    # Verbose mode
    opts.on(
      '-vLEVEL', '--verbosity=LEVEL',
      "Set verbosity level to debug, info, warn, error, fatal, or unknown (default: warning)"
    ) do |level|
      @options[:verbose] = level || 'warning'
    end

    # App boot file
    opts.on(
      '-rFILE', '--require=FILE',
      "Application boot file path (default: ./config/environment.rb)"
    ) do |file_path|
      @options[:require] = file_path
    end

    # Grantinee file
    opts.on(
      '-fFILE', '--file=FILE',
      "Permission definitions file path (default: ./Grantinee)"
    ) do |file_path|
      @options[:file] = file_path
    end

    # Database configuration file
    opts.on(
      '-cFILE', '--config=FILE',
      "Database configuration file path"
    ) do |file_path|
      @options[:config] = file_path
    end
  end

  parser.parse! @args
end
process_command_line_parameters() click to toggle source

Process parsed parameters

# File lib/grantinee/cli.rb, line 80
def process_command_line_parameters
  process_require_param
  process_database_param
  process_grantinee_param
  process_verbosity_param
end
process_database_param() click to toggle source

Database configuration file

# File lib/grantinee/cli.rb, line 110
def process_database_param
  unless @options[:config] || Grantinee.configuration.configured?
    Grantinee::Engine.detect_active_record_connection!

    unless Grantinee.configuration.configured?
      raise "No configuration file found. Please use the -c option"\
            " to pass a configuration file."
    end
  end

  require options[:config]
rescue StandardError, LoadError => error
  puts error
  exit
end
process_grantinee_param() click to toggle source

Grantinee file

# File lib/grantinee/cli.rb, line 127
def process_grantinee_param
  @options[:file] ||= "Grantinee"
end
process_require_param() click to toggle source

Application boot file

# File lib/grantinee/cli.rb, line 101
def process_require_param
  if @options[:require]
    require @options[:require]
  elsif defined?(Rails)
    require './config/environment'
  end
end
process_verbosity_param() click to toggle source

Explicit verbose mode, overrides configuration value

# File lib/grantinee/cli.rb, line 132
def process_verbosity_param
  return unless @options[:verbose]
  log_levels = %w[debug info warn error fatal unknown]
  @logger.level = log_levels.index(@options[:verbose])
end