class Dk::CLI

Constants

DEFAULT_CONFIG_PATH
ShowTaskList

Attributes

clirb[R]

Public Class Methods

new(kernel = nil) click to toggle source
# File lib/dk/cli.rb, line 19
def initialize(kernel = nil)
  @kernel = kernel || Kernel

  load config_path
  Dk.init
  @config = Dk.config

  @clirb = CLIRB.new do
    option 'list-tasks', 'list all tasks available to run', {
      :abbrev => 'T'
    }
    option 'dry-run', 'run the tasks without executing any local/remote cmds'
    option 'tree',    'print out the tree of tasks/sub-tasks that would be run'
    option 'verbose', 'run tasks showing verbose (ie debug log level) details'
  end
end
run(args) click to toggle source
# File lib/dk/cli.rb, line 13
def self.run(args)
  self.new.run(*args)
end

Public Instance Methods

run(*args) click to toggle source
# File lib/dk/cli.rb, line 36
def run(*args)
  begin
    run!(*args)
  rescue ShowTaskList
    @kernel.puts task_list
  rescue CLIRB::HelpExit
    @kernel.puts help
  rescue CLIRB::VersionExit
    @kernel.puts Dk::VERSION
  rescue CLIRB::Error, Dk::Config::UnknownTaskError => exception
    @kernel.puts help
    @kernel.puts "\n\n#{exception.message}\n"
    @kernel.exit 1
  rescue Dk::NoticeError => exception
    @kernel.puts "\n\n#{exception.message}\n\n"
    @kernel.puts exception.backtrace.first
    @kernel.exit 1
  rescue StandardError => exception
    @kernel.puts "\n\n#{exception.class}: #{exception.message}"
    @kernel.puts exception.backtrace.join("\n")
    @kernel.exit 1
  end
  @kernel.exit 0
end

Private Instance Methods

config_path() click to toggle source
# File lib/dk/cli.rb, line 99
def config_path
  File.expand_path(ENV['DK_CONFIG'] || DEFAULT_CONFIG_PATH, ENV['PWD'])
end
get_runner(config, opts) click to toggle source
# File lib/dk/cli.rb, line 103
def get_runner(config, opts)
  return Dk::DryRunner.new(config) if opts['dry-run']

  if opts['tree']
    @kernel.puts "building task tree#{'s' if @clirb.args.size > 1}..."
    return Dk::TreeRunner.new(config, @kernel)
  end

  Dk::DkRunner.new(config)
end
help() click to toggle source
# File lib/dk/cli.rb, line 84
def help
  "Usage: dk [TASKS] [options]\n\n" \
  "Tasks:\n" \
  "#{task_list('    ')}\n\n" \
  "Options: #{@clirb}"
end
run!(*args) click to toggle source
# File lib/dk/cli.rb, line 63
def run!(*args)
  @clirb.parse!(args)
  raise ShowTaskList if @clirb.opts['list-tasks']

  @config.stdout_log_level('debug') if @clirb.opts['verbose']

  unknowns = @clirb.args.select{ |name| !@config.tasks.keys.include?(name) }
  if !unknowns.empty?
    raise Dk::Config::UnknownTaskError, unknowns.map{ |u| "`#{u}`"}.join(', ')
  end

  runner = get_runner(@config, @clirb.opts)
  runner.log_cli_run(args.join(' ')) do
    @clirb.args.each do |task_name|
      runner.log_cli_task_run(task_name) do
        runner.run(@config.task(task_name))
      end
    end
  end
end
task_list(prefix = '') click to toggle source
# File lib/dk/cli.rb, line 91
def task_list(prefix = '')
  max_name_width = @config.tasks.keys.map(&:size).max
  items = @config.tasks.map do |(name, task_class)|
    "#{prefix}#{name.ljust(max_name_width)} # #{task_class.description}"
  end
  items.sort.join("\n")
end