Class: Ghost::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/ghost/cli.rb,
lib/ghost/cli/task.rb

Defined Under Namespace

Classes: Task

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out = STDOUT) ⇒ Cli

Returns a new instance of Cli.



11
12
13
14
15
16
# File 'lib/ghost/cli.rb', line 11

def initialize(out = STDOUT)
  self.store = Ghost::Store::HostsFileStore.new(section_name: 'ghost')
  self.out   = out

  setup_parser
end

Instance Attribute Details

#outObject

Returns the value of attribute out.



9
10
11
# File 'lib/ghost/cli.rb', line 9

def out
  @out
end

#parserObject

Returns the value of attribute parser.



9
10
11
# File 'lib/ghost/cli.rb', line 9

def parser
  @parser
end

#storeObject

Returns the value of attribute store.



9
10
11
# File 'lib/ghost/cli.rb', line 9

def store
  @store
end

Class Method Details

.task(*names, &block) ⇒ Object



59
60
61
62
63
64
# File 'lib/ghost/cli/task.rb', line 59

def task(*names, &block)
  task = Class.new(Task, &block)
  names.map!(&:to_s)
  task.name = names.first
  names.each { |name| tasks[name] = task }
end

.tasksObject



66
67
68
# File 'lib/ghost/cli/task.rb', line 66

def tasks
  @tasks ||= {}
end

Instance Method Details

#parse(args = ARGV) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ghost/cli.rb', line 18

def parse(args = ARGV)
  parser.parse! args

  arg = args.shift.to_s

  if (task = tasks[arg])
    task.perform(*args)
  else
    abort "No such task"
  end
rescue Errno::EACCES
  abort "Insufficient privileges. Try using `sudo` or running as root."
end

#tasksObject



54
55
56
# File 'lib/ghost/cli/task.rb', line 54

def tasks
  @tasks ||= Hash[self.class.tasks.map { |name, task| [name, task.new(store, out)] }]
end