Module: CfCompletion

Defined in:
lib/cf_completion.rb,
lib/cf_completion/version.rb

Constant Summary collapse

CF_COMMAND_POSITION =
1
APP_COMMANDS =
%w{ app push p scale delete d rename start st stop sp restart rs
restage rg events files f logs env e set-env se unset-env stacks }
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.command_name(completion_word_position, params) ⇒ Object



19
20
21
22
# File 'lib/cf_completion.rb', line 19

def self.command_name(completion_word_position, params)
  return '' unless completion_word_position > CF_COMMAND_POSITION
  params[CF_COMMAND_POSITION]
end

.complete(completion_word, completion_word_position, params) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/cf_completion.rb', line 9

def self.complete(completion_word, completion_word_position, params)
  command_name = command_name(completion_word_position, params)

  if completion_word_position == CF_COMMAND_POSITION
    puts list_commands(completion_word)
  elsif APP_COMMANDS.include?(command_name) && completion_word_position == 2
    puts list_apps(completion_word)
  end
end

.list_apps(filter) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cf_completion.rb', line 35

def self.list_apps(filter)
  app_list = `cf apps`

  found_heading = false

  heading_search = ->(line) do
    if (!found_heading && !line[/^name/].nil?)
      found_heading = true
      false
    else
      found_heading
    end
  end

  app_list.
      split("\n").
      select(&heading_search).
      map { |line| line.split(" ")[0] }.
      select { |name| name[/^#{filter}/] }
end

.list_commands(filter) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/cf_completion.rb', line 24

def self.list_commands(filter)
  help_output = `cf help | sed -n '/^GETTING STARTED:/,/^ENVIRONMENT VARIABLES/p'`

  help_output.
      split("\n").
      map { |line| line.match(/^\s+([^, ]*)/) }.
      reject { |match| match.nil? }.
      map { |match| match[1] }.
      reject { |match| match[/^#{filter}/].nil? }
end