Module: Befog::CLI

Extended by:
Befog::Commands::Mixins::Safely
Defined in:
lib/befog/cli.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

COMMANDS =
{
  "add" => Befog::Commands::Add,
  "remove" => Befog::Commands::Remove,
  "rm" => Befog::Commands::Remove,
  "start" => Befog::Commands::Start,
  "stop" => Befog::Commands::Stop,
  "run" => Befog::Commands::Run,
  "list" => Befog::Commands::List,
  "ls" => Befog::Commands::List,
  "configure" => Befog::Commands::Configure,
  "config" => Befog::Commands::Configure
}

Class Method Summary collapse

Methods included from Befog::Commands::Mixins::Safely

safely

Class Method Details

.parse(arguments) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/befog/cli.rb', line 31

def self.parse(arguments)
  options = {}
  key = :bank
  while not arguments.empty?
    argument = arguments.shift
    flag, short, long = /^(?:\-(\w)|\-\-(\w+))$/.match(argument).to_a
    if flag
      key = (short or long).to_sym
      options[key] = true
    else
      case options[key]
      when Array then options[key] << argument
      when String then options[key] = [ options[key], argument ]
      when true, nil then options[key] = argument
      end
    end
  end
  return options
end

.run(arguments) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/befog/cli.rb', line 51

def self.run(arguments)
  subcommand = arguments.shift
  # TODO: Rejigger this so we can have top-level options
  # instead of special-casing --version
  if subcommand == "--version" or subcommand == "-v"
    Befog.show_version
    exit if arguments.empty?
    subcommand = arguments.shift
  end
  if subcommand && (command = COMMANDS[subcommand])
    command.run(CLI.parse(arguments))
  elsif subcommand
    usage "'#{subcommand}' is not a supported command"
  else
    usage
  end
end

.usage(message = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/befog/cli.rb', line 69

def self.usage(message=nil)
  $stderr.puts "befog: #{message}" if message
  $stderr.puts <<-eos
    
Usage: befog <subcommand> [<bank>] [<options>]

The befog command allows you to manage your cloud servers from the command line.
You reference these sets of servers as banks.  A bank can have one or many servers.

Example: befog add web-servers --count 3

Adds 3 servers to the bank "web-servers"

Valid commands:

configure, config   Configure a bank of servers
add                 Provision new servers for a bank of servers
remove, rm          De-provision servers
start               Start servers
stop                Stop (suspend) servers
run                 Run a command on each of a bank of servers
list, ls            List servers

You can get more options for any command with --help or -h.
eos
  exit(-1)
  
end