class Ghost::Cli

Attributes

out[RW]
parser[RW]
store[RW]

Public Class Methods

new(out = STDOUT) click to toggle source
# 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
task(*names, &block) click to toggle source
# 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
tasks() click to toggle source
# File lib/ghost/cli/task.rb, line 66
def tasks
  @tasks ||= {}
end

Public Instance Methods

parse(args = ARGV) click to toggle source
# 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
tasks() click to toggle source
# File lib/ghost/cli/task.rb, line 54
def tasks
  @tasks ||= Hash[self.class.tasks.map { |name, task| [name, task.new(store, out)] }]
end

Private Instance Methods

abort(*args) click to toggle source

TODO: should output to STDERR

# File lib/ghost/cli.rb, line 55
def abort(*args)
  puts *args
  exit 1
end
print(*args) click to toggle source
puts(*args) click to toggle source
# File lib/ghost/cli.rb, line 50
def puts(*args)
  out.puts(*args)
end
setup_parser() click to toggle source
# File lib/ghost/cli.rb, line 34
def setup_parser
  self.parser = OptionParser.new do |o|
    o.on_tail '-v', '--version' do
      puts parser.ver
      exit
    end
  end

  parser.program_name = "ghost"
  parser.version = Ghost::VERSION
end