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)


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

def blank?
  self.to_s.blank?
end

#err(message) ⇒ Object



33
34
35
# File 'lib/cli/core_ext.rb', line 33

def err(message)
  raise Bosh::Cli::CliError, message
end

#err_nlObject



29
30
31
# File 'lib/cli/core_ext.rb', line 29

def err_nl
  warn('')
end

#format_time(time) ⇒ Object



67
68
69
70
# 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 = '-') ⇒ 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



72
73
74
75
76
77
78
79
80
81
# 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) ⇒ 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



62
63
64
65
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



37
38
39
40
# File 'lib/cli/core_ext.rb', line 37

def quit(message = nil)
  say(message)
  raise Bosh::Cli::GracefulExit, message
end

#read_yaml_file(path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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") ⇒ 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] == sep
  Bosh::Cli::Config.output.print("#{$indent}#{message}#{sep}")
end

#terminal_widthFixnum

Returns:

  • (Fixnum)


107
108
109
# File 'lib/cli/core_ext.rb', line 107

def terminal_width
  STDIN.tty? ? [HighLine::SystemExtensions.terminal_size[0], 120].min : 80
end

#warning(message) ⇒ Object



111
112
113
# File 'lib/cli/core_ext.rb', line 111

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



100
101
102
103
104
# 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