class Producer::Core::CLI

Constants

ARGUMENTS_SEPARATOR
ArgumentError
ENV_DEBUG_KEY
ENV_DRYRUN_KEY
ENV_VERBOSE_KEY
EX_SOFTWARE
EX_USAGE
USAGE

Attributes

arguments[R]
env[R]
stderr[R]
stdin[R]
stdout[R]

Public Class Methods

new(args, environment, stdin: $stdin, stdout: $stdout, stderr: $stderr) click to toggle source
# File lib/producer/core/cli.rb, line 35
def initialize args, environment, stdin: $stdin, stdout: $stdout,
    stderr: $stderr
  @arguments  = args
  @stdin      = stdin
  @stdout     = stdout
  @stderr     = stderr
  @env        = build_env

  configure_environment! environment
end
run!(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr) click to toggle source
# File lib/producer/core/cli.rb, line 20
def run! arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr
  cli = new arguments, ENV, stdin: stdin, stdout: stdout, stderr: stderr
  cli.parse_arguments!
  cli.run
rescue ArgumentError => e
  stderr.puts e.message
  exit EX_USAGE
rescue StandardError => e
  stderr.puts ErrorFormatter.new(debug: cli.env.debug?).format e
  exit EX_SOFTWARE
end

Public Instance Methods

evaluate_recipes() click to toggle source
# File lib/producer/core/cli.rb, line 62
def evaluate_recipes
  @arguments.map { |e| Recipe::FileEvaluator.evaluate(e, @env) }
end
parse_arguments!() click to toggle source
# File lib/producer/core/cli.rb, line 46
def parse_arguments!
  if @arguments.include? ARGUMENTS_SEPARATOR
    @arguments, @env.recipe_argv = split_arguments_lists @arguments
  end
  option_parser.parse! @arguments
  fail ArgumentError, option_parser if @arguments.empty?
rescue OptionParser::InvalidOption
  raise ArgumentError, option_parser
end
run(worker: Worker.new(@env)) click to toggle source
# File lib/producer/core/cli.rb, line 56
def run worker: Worker.new(@env)
  evaluate_recipes.each { |recipe| worker.process recipe.tasks }
ensure
  @env.cleanup
end

Private Instance Methods

build_env() click to toggle source
# File lib/producer/core/cli.rb, line 68
def build_env
  Env.new(input: @stdin, output: @stdout, error_output: @stderr)
end
configure_environment!(environment) click to toggle source
# File lib/producer/core/cli.rb, line 72
def configure_environment! environment
  @env.verbose  = true if environment.key? ENV_VERBOSE_KEY
  @env.debug    = true if environment.key? ENV_DEBUG_KEY
  @env.dry_run  = true if environment.key? ENV_DRYRUN_KEY
end
option_parser() click to toggle source
# File lib/producer/core/cli.rb, line 85
def option_parser
  OptionParser.new do |opts|
    opts.banner = USAGE
    opts.separator ''
    opts.separator 'options:'

    option_parser_add_boolean_options opts
    opts.on '-t', '--target HOST', 'target host' do |e|
      env.target = e
    end
  end
end
option_parser_add_boolean_options(opts) click to toggle source
# File lib/producer/core/cli.rb, line 98
def option_parser_add_boolean_options opts
  { v: 'verbose', d: 'debug', n: 'dry run' }.each do |k, v|
    opts.on "-#{k}", "--#{v.tr ' ', '-'}", "enable #{v} mode" do
      env.send "#{v.tr ' ', '_'}=", true
    end
  end
end
split_arguments_lists(arguments) click to toggle source
# File lib/producer/core/cli.rb, line 78
def split_arguments_lists arguments
  arguments
    .chunk  { |e| e == ARGUMENTS_SEPARATOR }
    .reject { |a, _| a }
    .map    &:last
end