Class: CfScript::Scope::Script

Inherits:
Target show all
Includes:
UI
Defined in:
lib/cf_script/scope/script.rb

Constant Summary collapse

VALID_OPTIONS =
[:api, :org, :username, :password, :space]

Constants included from UI

UI::COLORS, UI::EMOJI, UI::TAGS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UI

alert, call_type, debug, detail, emoji, emoji_for, error, info, print_err, print_out, progress, puts_err, puts_out, step, success, tag_char, tag_close, tag_color, tag_format, tag_open, tag_style, title, trace, ui_format, with_color_of

Methods inherited from Target

#app, #apps, #current_org, #current_space, #space, #target

Methods inherited from Base

#finalize, #method_missing, #spec_for

Methods included from Execution

exec_in

Constructor Details

#initialize(options = {}) ⇒ Script

Returns a new instance of Script.



9
10
11
12
13
# File 'lib/cf_script/scope/script.rb', line 9

def initialize(options = {})
  super()

  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CfScript::Scope::Base

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/cf_script/scope/script.rb', line 3

def options
  @options
end

Instance Method Details

#apply_optionsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cf_script/scope/script.rb', line 30

def apply_options
  check_options

  # If api is given and there are no username or password, set the api
  if options[:api] and (options[:username].nil? or options[:password].nil?)
    unless CfScript::Command.api(options[:api])
      error :cf, "Could not set the API endpoint: #{options[:api]}"
      raise "Could not set the API endpoint: #{options[:api]}"
    end
  end

  # If username and password are given, call login. This also sets the api
  # and the space if they are given.
  if options[:username] and options[:password]
    unless CfScript::Command.(options[:username], options[:password], options)
      error :cf, 'Could not login with given credentials'
      raise 'Could not login with given credentials'
    end
  end

  # If a space is given, don't set it if there is a username and password,
  # login would have already set it.
  if options[:space] and (options[:username].nil? or options[:password].nil?)
    # Use target, not CfScript::Command.target, so we can keep track of
    # the previous and current spaces and reset them on scope exit.
    target options[:space]
  end
end

#check_optionsObject



21
22
23
24
25
26
27
28
# File 'lib/cf_script/scope/script.rb', line 21

def check_options
  options.keys.each do |key|
    unless VALID_OPTIONS.include?(key)
      alert :options, "#{key} is not a valid option for the " +
                      'cf method. It will be ignored.'
    end
  end
end

#run(&block) ⇒ Object



15
16
17
18
19
# File 'lib/cf_script/scope/script.rb', line 15

def run(&block)
  apply_options unless options.empty?

  exec_in(self, nil, &block)
end