Class: Dandelion::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/dandelion/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



5
6
7
8
9
10
11
12
# 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

Instance Method Details

#adapterObject



20
21
22
23
24
25
26
27
28
29
30
# 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_classObject



40
41
42
43
44
45
46
# 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

#configObject



14
15
16
17
18
# 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!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dandelion/cli.rb', line 48

def execute!
  parse!(@parser)

  if @options[:help]
    display_help
    exit
  end

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

  validate!

  parse!(command_class.parser(@options))

  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

#repoObject



32
33
34
# File 'lib/dandelion/cli.rb', line 32

def repo
  @repo ||= Rugged::Repository.new(repo_path)
end

#workspaceObject



36
37
38
# File 'lib/dandelion/cli.rb', line 36

def workspace
  @workspace ||= Workspace.new(repo, adapter, config)
end