Class: GridCLI::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/gridcli/runner.rb

Class Method Summary collapse

Class Method Details

.command(key) ⇒ Object



27
28
29
# File 'lib/gridcli/runner.rb', line 27

def self.command(key)
  @@cmds[key]
end

.commandsObject



31
32
33
# File 'lib/gridcli/runner.rb', line 31

def self.commands
  @@cmds.values
end

.register(name, klass) ⇒ Object



22
23
24
25
# File 'lib/gridcli/runner.rb', line 22

def self.register(name, klass)
  @@cmds ||= {}
  @@cmds[name] = klass
end

.run(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gridcli/runner.rb', line 5

def self.run(args)
  cmd = args.shift

  aliases = GridCLI.config['alias']
  if aliases.has_key?(cmd)
    args = self.shellsplit(aliases[cmd]) + args
    cmd = args.shift
  end

  cmd = "help" if not @@cmds.has_key? cmd
  begin
    @@cmds[cmd].new.run(args)
  rescue ActiveResource::UnauthorizedAccess
    puts "Sorry gridder, your username or auth token is invalid."
  end
end

.shellsplit(line) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gridcli/runner.rb', line 36

def self.shellsplit(line)
  words = []
  field = ''
  line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
    |word, sq, dq, esc, garbage, sep|
    raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage
    field << (word || sq || (dq || esc).gsub(/\\(.)/, '\\1'))
    if sep
      words << field
      field = ''
    end
  end
  words
end