class Dandelion::CLI

Public Class Methods

new(args) click to toggle source
# File lib/dandelion/cli.rb, line 5
def initialize(args)
  @args = args

  @options = {}
  @options[:help] = true if @args.length == 0

  @parser = Command::Base.parser(@options)
end

Public Instance Methods

adapter() click to toggle source
# File lib/dandelion/cli.rb, line 20
def adapter
  @adapter ||= Adapter::Base.create_adapter(config[:adapter], config)
rescue Adapter::InvalidAdapterError => e
  log.fatal("Unsupported adapter: #{config[:adapter]}")
  exit 1
rescue Adapter::MissingDependencyError => e
  log.fatal("The #{config[:adapter]} adapter requires additional gems:")
  log.fatal(e.gems.map { |name| "    #{name}"}.join("\n"))
  log.fatal("Please install the gems first: gem install #{e.gems.join(' ')}")
  exit 1
end
command_class() click to toggle source
# File lib/dandelion/cli.rb, line 40
def command_class
  @command_class ||= Command::Base.lookup(@args.shift.to_sym)
rescue Command::InvalidCommandError => e
  log.fatal("Invalid command: #{e}")
  display_help
  exit 1
end
config() click to toggle source
# File lib/dandelion/cli.rb, line 14
def config
  @config ||= Config.new(path: config_path).tap do |config|
    config[:adapter] ||= config[:scheme] # backward compat
  end
end
execute!() click to toggle source
# File lib/dandelion/cli.rb, line 48
def execute!
  if @args.length == 0
    @options[:help] = true
  end

  parse!(@parser)

  if @options[:version]
    log.info("Dandelion #{Dandelion::VERSION}")
    exit
  end

  if @options[:help]
    display_help
    exit
  end

  parse!(command_class.parser(@options))

  validate!

  command = command_class.new(workspace, config, @options)
  command.setup(@args)

  begin
    command.execute!
  rescue RevisionError => e
    log.fatal("Invalid revision: #{e}")
    exit 1
  end
end
repo() click to toggle source
# File lib/dandelion/cli.rb, line 32
def repo
  @repo ||= Rugged::Repository.new(repo_path)
end
workspace() click to toggle source
# File lib/dandelion/cli.rb, line 36
def workspace
  @workspace ||= Workspace.new(repo, adapter, config)
end

Private Instance Methods

config_path() click to toggle source
# File lib/dandelion/cli.rb, line 82
def config_path
  if @options[:config]
    @options[:config]
  else
    paths = [
      File.join(repo_path, 'dandelion.yml'),
      File.join(repo_path, 'dandelion.yaml')
    ]

    paths.drop_while { |path| !path || !File.exists?(path) }.first || paths.first
  end
end
display_help() click to toggle source
# File lib/dandelion/cli.rb, line 140
def display_help
  log.info(@parser.help)
  log.info("Available commands:")
  log.info(Command::Base.commands.map { |c| "    #{c}"}.join("\n"))
end
log() click to toggle source
# File lib/dandelion/cli.rb, line 146
def log
  Dandelion.logger
end
parse!(parser) click to toggle source
# File lib/dandelion/cli.rb, line 130
def parse!(parser)
  begin
    parser.order!(@args)
  rescue OptionParser::InvalidOption => e
    log.fatal(e.to_s.capitalize)
    display_help
    exit 1
  end
end
repo_exists?() click to toggle source
# File lib/dandelion/cli.rb, line 103
def repo_exists?
  return !!(repo)
rescue ::IOError, ::Rugged::OSError, ::Rugged::RepositoryError
  # squash exceptions for instantiating Rugged repo
  return false
end
repo_path() click to toggle source
# File lib/dandelion/cli.rb, line 95
def repo_path
  if @options[:repo]
    File.expand_path(@options[:repo])
  else
    File.expand_path('.')
  end
end
validate!() click to toggle source
# File lib/dandelion/cli.rb, line 110
def validate!
  unless repo_exists?
    log.fatal("Not a git repository: #{repo_path}")
    exit 1
  end

  unless File.exists?(config_path)
    log.fatal("Missing config file: #{config_path}")
    exit 1
  end

  begin
    config
  rescue Psych::SyntaxError => e
    log.fatal("There's a syntax error in your config file: #{config_path}")
    log.fatal(e)
    exit 1
  end
end