Module: Boxcutter

Defined in:
lib/bluebox-boxcutter.rb,
lib/bluebox-boxcutter/ui.rb,
lib/bluebox-boxcutter/git.rb,
lib/bluebox-boxcutter/kvm.rb,
lib/bluebox-boxcutter/razor.rb,
lib/bluebox-boxcutter/machine.rb,
lib/bluebox-boxcutter/version.rb,
lib/bluebox-boxcutter/password.rb

Defined Under Namespace

Modules: Git, KVM, Password, Razor Classes: Boxpanel, Machine

Constant Summary collapse

VERSION =
'0.0.14'
@@colors =
true
@@yes =
false
@@quiet =
false

Class Method Summary collapse

Class Method Details

.confirm(prompt) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/bluebox-boxcutter/ui.rb', line 31

def self.confirm(prompt)
  if @@yes
    yield
  else
    puts "#{prompt}  :  are you sure? [y/n]".yellow
    answer = STDIN.gets.chomp
    answer == 'y' ? yield : error!('aborting at user request')
  end
end

.error!(msg) ⇒ Object



10
11
12
13
# File 'lib/bluebox-boxcutter/ui.rb', line 10

def self.error!(msg)
  STDERR.puts msg.red
  exit 1
end

.flatten_hash(hash, parent_key = "", new_hash = { }) ⇒ Object

flatten any child hashes into top level hash e.g. {“x” => { “a” => “b” } will become {“x_a” => “b” }



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bluebox-boxcutter.rb', line 41

def self.flatten_hash(hash, parent_key = "", new_hash = { })
  hash.each do |key,value|
    key_string = parent_key.empty? ? key : "#{parent_key}_#{key}"
    if value.class == Hash
      flatten_hash(value, key_string, new_hash)
    else
      new_hash.merge!({ key_string => value })
    end
  end
  new_hash
end

.install(git_repo) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/bluebox-boxcutter.rb', line 19

def self.install(git_repo)

  # if we're passed a file path
  if git_repo.match /^(\/.*)+\/?/
    Dir.chdir git_repo
    `rake install`
  end
end

.msg(s) ⇒ Object



23
24
25
# File 'lib/bluebox-boxcutter/ui.rb', line 23

def self.msg(s)
  puts (@@colors ? s.green : s) unless @@quiet
end

.no_colors!Object



15
16
17
# File 'lib/bluebox-boxcutter/ui.rb', line 15

def self.no_colors!
  @@colors = false
end

.quiet!Object



19
20
21
# File 'lib/bluebox-boxcutter/ui.rb', line 19

def self.quiet!
  @@quiet = true
end

.table(rows, headers_present = true) ⇒ Object



46
47
48
49
50
51
# File 'lib/bluebox-boxcutter/ui.rb', line 46

def self.table(rows, headers_present=true)
  headers = nil
  headers = rows.shift.map { |x| x.to_s } if headers_present
  headers.map! { |h| h.dup.cyan } if @@colors
  Terminal::Table.new(:headings => headers, :rows => rows).to_s
end

.table_of_hashes(rows) ⇒ Object



41
42
43
44
# File 'lib/bluebox-boxcutter/ui.rb', line 41

def self.table_of_hashes(rows)
  keys = rows.empty? ? [] : rows.first.keys.map { |k| k.dup }
  table([keys] + rows.map { |r| r.values })
end

.updateObject



28
29
30
31
32
33
34
35
36
# File 'lib/bluebox-boxcutter.rb', line 28

def self.update
  Boxcutter::Git::clone
  upstream_version = Boxcutter::Git::upstream_version
  our_version = Boxcutter::VERSION
  if Gem::Version.new(upstream_version) > Gem::Version.new(our_version)
    Boxcutter::install(Boxcutter::Git::LOCAL_CLONE)
  end
  Boxcutter::Git::rm_clone
end

.versionObject



15
16
17
# File 'lib/bluebox-boxcutter.rb', line 15

def self.version
  eval(File.read("#{File.dirname __FILE__}/../bluebox-boxcutter.gemspec")).version.to_s
end

.yes!Object



27
28
29
# File 'lib/bluebox-boxcutter/ui.rb', line 27

def self.yes!
  @@yes = true
end