Class: Bosh::Cli::Command::Base

Inherits:
Object
  • Object
show all
Extended by:
Bosh::Cli::CommandDiscovery
Defined in:
lib/cli/base_command.rb

Constant Summary collapse

DEFAULT_DIRECTOR_PORT =
25555

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bosh::Cli::CommandDiscovery

desc, method_added, option, register_command, usage

Constructor Details

#initialize(runner = nil) ⇒ Base

Returns a new instance of Base.

Parameters:



20
21
22
23
24
25
26
27
# File 'lib/cli/base_command.rb', line 20

def initialize(runner = nil)
  @runner = runner
  @options = {}
  @work_dir = Dir.pwd
  @exit_code = 0
  @out = nil
  @args = []
end

Instance Attribute Details

#argsArray

Returns Arguments passed to command handler.

Returns:

  • (Array)

    Arguments passed to command handler



15
16
17
# File 'lib/cli/base_command.rb', line 15

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/cli/base_command.rb', line 8

def options
  @options
end

#outObject

Returns the value of attribute out.



12
13
14
# File 'lib/cli/base_command.rb', line 12

def out
  @out
end

#runnerObject (readonly)

Returns the value of attribute runner.



10
11
12
# File 'lib/cli/base_command.rb', line 10

def runner
  @runner
end

#work_dirObject (readonly)

Returns the value of attribute work_dir.



9
10
11
# File 'lib/cli/base_command.rb', line 9

def work_dir
  @work_dir
end

Instance Method Details

#add_option(name, value) ⇒ Object



42
43
44
# File 'lib/cli/base_command.rb', line 42

def add_option(name, value)
  @options[name] = value
end

#blob_managerObject



61
62
63
# File 'lib/cli/base_command.rb', line 61

def blob_manager
  @blob_manager ||= Bosh::Cli::BlobManager.new(release)
end

#blobstoreObject



65
66
67
# File 'lib/cli/base_command.rb', line 65

def blobstore
  release.blobstore
end

#cacheBosh::Cli::Cache

Returns Current CLI cache.

Returns:



30
31
32
# File 'lib/cli/base_command.rb', line 30

def cache
  Config.cache
end

#configBosh::Cli::Config

Returns Current configuration.

Returns:



35
36
37
38
39
40
# File 'lib/cli/base_command.rb', line 35

def config
  @config ||= begin
    config_file = options[:config] || Bosh::Cli::DEFAULT_CONFIG_PATH
    Bosh::Cli::Config.new(config_file)
  end
end

#confirmed?(question = "Are you sure?") ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/cli/base_command.rb', line 89

def confirmed?(question = "Are you sure?")
  return true if non_interactive?
  ask("#{question} (type 'yes' to continue): ") == "yes"
end

#deploymentString

Returns Deployment manifest path.

Returns:

  • (String)

    Deployment manifest path



103
104
105
# File 'lib/cli/base_command.rb', line 103

def deployment
  options[:deployment] || config.deployment
end

#directorObject



50
51
52
53
# File 'lib/cli/base_command.rb', line 50

def director
  @director ||= Bosh::Cli::Director.new(
    target, username, password, @options.select {|k,v| k == :no_track})
end

#exit_code(code = nil) ⇒ Object

Sets or returns command exit code

Parameters:

  • code (optional, Integer) (defaults to: nil)

    If param is given, sets exit code. If it’s nil, returns previously set exit_code



124
125
126
127
128
129
130
# File 'lib/cli/base_command.rb', line 124

def exit_code(code = nil)
  if code
    @exit_code = code
  else
    @exit_code
  end
end

#interactive?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/cli/base_command.rb', line 77

def interactive?
  !non_interactive?
end

#logged_in?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/cli/base_command.rb', line 69

def logged_in?
  username && password
end

#non_interactive?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/cli/base_command.rb', line 73

def non_interactive?
  options[:non_interactive]
end

#passwordString

Returns Director password.

Returns:

  • (String)

    Director password



113
114
115
# File 'lib/cli/base_command.rb', line 113

def password
  options[:password] || ENV["BOSH_PASSWORD"] || config.password(target)
end

#redirect(*args) ⇒ Object



85
86
87
# File 'lib/cli/base_command.rb', line 85

def redirect(*args)
  Bosh::Cli::Runner.new(args, @options).run
end

#releaseObject



55
56
57
58
59
# File 'lib/cli/base_command.rb', line 55

def release
  return @release if @release
  check_if_release_dir
  @release = Bosh::Cli::Release.new(@work_dir)
end

#remove_option(name) ⇒ Object



46
47
48
# File 'lib/cli/base_command.rb', line 46

def remove_option(name)
  @options.delete(name)
end

#targetString Also known as: target_url

Returns Target director URL.

Returns:

  • (String)

    Target director URL



95
96
97
98
99
# File 'lib/cli/base_command.rb', line 95

def target
  raw_url = options[:target] || config.target
  url = config.resolve_alias(:target, raw_url) || raw_url
  url ? normalize_url(url) : nil
end

#target_nameObject



117
118
119
# File 'lib/cli/base_command.rb', line 117

def target_name
  config.target_name || target_url
end

#task_report(status, task_id, success_msg = nil, error_msg = nil) ⇒ Object

Prints director task completion report. Note that event log usually contains pretty detailed error report and other UI niceties, so most of the time this could just do nothing

Parameters:

  • status (Symbol)

    Task status

  • task_id (#to_s)

    Task ID



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cli/base_command.rb', line 137

def task_report(status, task_id, success_msg = nil, error_msg = nil)
  case status
    when :non_trackable
      report = "Can't track director task".red
    when :track_timeout
      report = "Task tracking timeout".red
    when :running
      report = "Director task #{task_id.yellow} is running"
    when :error
      report = error_msg
    when :done
      report = success_msg
    else
      report = nil
  end

  if status != :done
    exit_code(1)
  end

  say("\n#{report}") if report
end

#usernameString

Returns Director username.

Returns:

  • (String)

    Director username



108
109
110
# File 'lib/cli/base_command.rb', line 108

def username
  options[:username] || ENV["BOSH_USER"] || config.username(target)
end

#verbose?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/cli/base_command.rb', line 81

def verbose?
  @options[:verbose]
end