Module: BoshExtensions
- Included in:
- Object
- Defined in:
- lib/cli/core_ext.rb
Overview
Copyright © 2009-2012 VMware, Inc.
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #err(message) ⇒ Object
- #format_time(time) ⇒ Object
- #header(message, filler = '-') ⇒ Object
- #load_yaml_file(path, expected_type = Hash) ⇒ Object
- #nl(count = 1) ⇒ Object
- #pluralize(number, singular, plural = nil) ⇒ Object
- #pretty_size(what, prec = 1) ⇒ Object
- #quit(message = nil) ⇒ Object
- #say(message, sep = "\n") ⇒ Object
- #terminal_width ⇒ Fixnum
- #warning(message) ⇒ Object
- #with_indent(indent) ⇒ Object
- #write_yaml(manifest, path) ⇒ Object
Instance Method Details
#blank? ⇒ Boolean
38 39 40 |
# File 'lib/cli/core_ext.rb', line 38 def blank? self.to_s.blank? end |
#err(message) ⇒ Object
29 30 31 |
# File 'lib/cli/core_ext.rb', line 29 def err() raise Bosh::Cli::CliError, end |
#format_time(time) ⇒ Object
63 64 65 66 |
# File 'lib/cli/core_ext.rb', line 63 def format_time(time) ts = time.to_i sprintf("%02d:%02d:%02d", ts / 3600, (ts / 60) % 60, ts % 60); end |
#header(message, filler = '-') ⇒ Object
19 20 21 22 23 |
# File 'lib/cli/core_ext.rb', line 19 def header(, filler = '-') say("\n") say() say(filler.to_s * .size) end |
#load_yaml_file(path, expected_type = Hash) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cli/core_ext.rb', line 68 def load_yaml_file(path, expected_type = Hash) 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 => e err("Incorrect YAML structure in `#{path}': #{e}".make_red) end 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) ⇒ Object
25 26 27 |
# File 'lib/cli/core_ext.rb', line 25 def nl(count = 1) say("\n" * count) end |
#pluralize(number, singular, plural = nil) ⇒ Object
58 59 60 61 |
# File 'lib/cli/core_ext.rb', line 58 def pluralize(number, singular, plural = nil) plural = plural || "#{singular}s" number == 1 ? "1 #{singular}" : "#{number} #{plural}" end |
#pretty_size(what, prec = 1) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cli/core_ext.rb', line 42 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) ⇒ Object
33 34 35 36 |
# File 'lib/cli/core_ext.rb', line 33 def quit( = nil) say() raise Bosh::Cli::GracefulExit, end |
#say(message, sep = "\n") ⇒ Object
5 6 7 8 9 10 |
# File 'lib/cli/core_ext.rb', line 5 def say(, sep = "\n") return unless Bosh::Cli::Config.output && = .dup.to_s sep = "" if [-1..-1] == sep Bosh::Cli::Config.output.print("#{$indent}#{}#{sep}") end |
#terminal_width ⇒ Fixnum
98 99 100 |
# File 'lib/cli/core_ext.rb', line 98 def terminal_width STDIN.tty? ? [HighLine::SystemExtensions.terminal_size[0], 120].min : 80 end |
#warning(message) ⇒ Object
102 103 104 |
# File 'lib/cli/core_ext.rb', line 102 def warning() warn("[WARNING] #{}".make_yellow) end |
#with_indent(indent) ⇒ Object
12 13 14 15 16 17 |
# 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) ⇒ Object
91 92 93 94 95 |
# File 'lib/cli/core_ext.rb', line 91 def write_yaml(manifest, path) File.open(path, "w+") do |f| f.write(manifest.to_yaml) end end |