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

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/commands/base.rb

Constant Summary collapse

BLOBS_DIR =
"blobs"
BLOBS_INDEX_FILE =
"blob_index.yml"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
18
19
# File 'lib/cli/commands/base.rb', line 12

def initialize(options = {})
  @options = options.dup
  @work_dir = Dir.pwd
  config_file = @options[:config] || Bosh::Cli::DEFAULT_CONFIG_PATH
  @config = Config.new(config_file)
  @cache = Config.cache
  @exit_code = 0
end

Class Attribute Details

.commandsObject (readonly)

Returns the value of attribute commands.



22
23
24
# File 'lib/cli/commands/base.rb', line 22

def commands
  @commands
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



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

def cache
  @cache
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#outObject

Returns the value of attribute out.



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

def out
  @out
end

#usageObject

Returns the value of attribute usage.



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

def usage
  @usage
end

#work_dirObject (readonly)

Returns the value of attribute work_dir.



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

def work_dir
  @work_dir
end

Class Method Details

.command(name, &block) ⇒ Object



24
25
26
27
# File 'lib/cli/commands/base.rb', line 24

def command(name, &block)
  @commands ||= {}
  @commands[name] = block
end

Instance Method Details

#blob_managerObject



40
41
42
# File 'lib/cli/commands/base.rb', line 40

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

#blobstoreObject



44
45
46
# File 'lib/cli/commands/base.rb', line 44

def blobstore
  release.blobstore
end

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

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/cli/commands/base.rb', line 82

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

#directorObject



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

def director
  @director ||= Bosh::Cli::Director.new(target, username, password)
end

#dry_run?Boolean

TODO: implement it

Returns:

  • (Boolean)


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

def dry_run?
  options[:dry_run]
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



113
114
115
116
117
118
119
# File 'lib/cli/commands/base.rb', line 113

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

#full_target_nameObject



103
104
105
106
107
108
# File 'lib/cli/commands/base.rb', line 103

def full_target_name
  # TODO refactor this method
  ret = (target_name.blank? || target_name == target_url ?
      target_name : "%s (%s)" % [target_name, target_url])
  ret + " %s" % target_version if ret
end

#interactive?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cli/commands/base.rb', line 56

def interactive?
  !options[:non_interactive]
end

#logged_in?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/cli/commands/base.rb', line 48

def logged_in?
  username && password
end

#non_interactive?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cli/commands/base.rb', line 52

def non_interactive?
  !interactive?
end

#redirect(*args) ⇒ Object



77
78
79
80
# File 'lib/cli/commands/base.rb', line 77

def redirect(*args)
  run(*args)
  raise Bosh::Cli::GracefulExit, "redirected to %s" % [args.join(" ")]
end

#releaseObject



34
35
36
37
38
# File 'lib/cli/commands/base.rb', line 34

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

#run(namespace, action, *args) ⇒ Object



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

def run(namespace, action, *args)
  eval(namespace.to_s.capitalize).new(options).send(action.to_sym, *args)
end

#show_usageObject



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

def show_usage
  say("Usage: #{@usage}") if @usage
end

#target_nameObject



95
96
97
# File 'lib/cli/commands/base.rb', line 95

def target_name
  config.target_name || target_url
end

#target_versionObject



99
100
101
# File 'lib/cli/commands/base.rb', line 99

def target_version
  config.target_version ? "Ver: " + config.target_version : ""
end

#task_report(status, 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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cli/commands/base.rb', line 125

def task_report(status, 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 :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

#verbose?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/cli/commands/base.rb', line 60

def verbose?
  options[:verbose]
end