module BoshExtensions
Copyright © 2009-2012 VMware, Inc.
Public Instance Methods
blank?()
click to toggle source
# File lib/cli/core_ext.rb, line 42 def blank? self.to_s.blank? end
err(message)
click to toggle source
# File lib/cli/core_ext.rb, line 33 def err(message) raise Bosh::Cli::CliError, message end
err_nl()
click to toggle source
# File lib/cli/core_ext.rb, line 29 def err_nl warn('') end
format_time(time)
click to toggle source
# File lib/cli/core_ext.rb, line 67 def format_time(time) ts = time.to_i sprintf("%02d:%02d:%02d", ts / 3600, (ts / 60) % 60, ts % 60); end
header(message, filler = '-')
click to toggle source
# File lib/cli/core_ext.rb, line 19 def header(message, filler = '-') say("\n") say(message) say(filler.to_s * message.size) end
load_yaml_file(path, expected_type = Hash)
click to toggle source
# File lib/cli/core_ext.rb, line 72 def load_yaml_file(path, expected_type = Hash) yaml_str = read_yaml_file(path) yaml = Psych::load(yaml_str) if expected_type && !yaml.is_a?(expected_type) err("Incorrect YAML structure in `#{path}': expected #{expected_type} at the root".make_red) end yaml end
nl(count = 1)
click to toggle source
# File lib/cli/core_ext.rb, line 25 def nl(count = 1) say("\n" * count) end
pluralize(number, singular, plural = nil)
click to toggle source
# File lib/cli/core_ext.rb, line 62 def pluralize(number, singular, plural = nil) plural = plural || "#{singular}s" number == 1 ? "1 #{singular}" : "#{number} #{plural}" end
pretty_size(what, prec=1)
click to toggle source
# File lib/cli/core_ext.rb, line 46 def pretty_size(what, prec=1) if what.is_a?(String) && File.exists?(what) size = File.size(what) else size = what.to_i end return "NA" unless size return "#{size}B" if size < 1024 return sprintf("%.#{prec}fK", size/1024.0) if size < (1024*1024) if size < (1024*1024*1024) return sprintf("%.#{prec}fM", size/(1024.0*1024.0)) end sprintf("%.#{prec}fG", size/(1024.0*1024.0*1024.0)) end
quit(message = nil)
click to toggle source
# File lib/cli/core_ext.rb, line 37 def quit(message = nil) say(message) raise Bosh::Cli::GracefulExit, message end
read_yaml_file(path)
click to toggle source
# File lib/cli/core_ext.rb, line 83 def read_yaml_file(path) err("Cannot find file `#{path}'".make_red) unless File.exist?(path) begin yaml_str = ERB.new(File.read(path)).result rescue SystemCallError => e err("Cannot load YAML file at `#{path}': #{e}".make_red) end begin Bosh::Cli::YamlHelper.check_duplicate_keys(yaml_str) rescue Exception => e # on ruby 1.9.3 Psych::SyntaxError isn't a StandardError err("Incorrect YAML structure in `#{path}': #{e}".make_red) end yaml_str end
say(message, sep = "\n")
click to toggle source
# File lib/cli/core_ext.rb, line 5 def say(message, sep = "\n") return unless Bosh::Cli::Config.output && message message = message.dup.to_s sep = "" if message[-1] == sep Bosh::Cli::Config.output.print("#{$indent}#{message}#{sep}") end
terminal_width()
click to toggle source
@return [Fixnum]
# File lib/cli/core_ext.rb, line 107 def terminal_width STDIN.tty? ? [HighLine::SystemExtensions.terminal_size[0], 120].min : 80 end
warning(message)
click to toggle source
# File lib/cli/core_ext.rb, line 111 def warning(message) warn("[WARNING] #{message}".make_yellow) end
with_indent(indent) { || ... }
click to toggle source
# File lib/cli/core_ext.rb, line 12 def with_indent(indent) old_indent, $indent = $indent, old_indent.to_s + indent.to_s yield ensure $indent = old_indent end
write_yaml(manifest, path)
click to toggle source
# File lib/cli/core_ext.rb, line 100 def write_yaml(manifest, path) File.open(path, "w+") do |f| f.write(manifest.to_yaml) end end