Module: BoshExtensions

Included in:
Object
Defined in:
lib/cli/core_ext.rb

Overview

Copyright © 2009-2012 VMware, Inc.

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (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(message)
  raise Bosh::Cli::CliError, message
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(message, filler = '-')
  say("\n")
  say(message)
  say(filler.to_s * message.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(message = nil)
  say(message)
  raise Bosh::Cli::GracefulExit, message
end

#say(message, sep = "\n") ⇒ Object



5
6
7
8
9
10
# 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..-1] == sep
  Bosh::Cli::Config.output.print("#{$indent}#{message}#{sep}")
end

#terminal_widthFixnum

Returns:

  • (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(message)
  warn("[WARNING] #{message}".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