Top Level Namespace

Defined Under Namespace

Modules: Snapback

Instance Method Summary collapse

Instance Method Details

#ask_confirm(question) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/snapback.rb', line 116

def ask_confirm question
  while true
    print "#{question} [Y/N]: "

    confirmed = $stdin.gets.chomp.upcase

    if confirmed.eql? 'Y' then
      return true
    elsif confirmed.eql? 'N' then
      return false
    else
      puts "Please enter either Y or N."
    end
  end
end

#ask_int(question, max) ⇒ Object

Ask



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/snapback.rb', line 83

def ask_int question, max
  number = nil

  while true
    print "#{question}: "

    number = $stdin.gets.chomp

    if !Integer(number) then
      puts "The value you entered is not a number."
      puts ""
      next
    end

    number = number.to_i

    if number < 1 || number > max then
      puts "Please enter a number between 1 and #{max}."
      puts ""
      next
    end

    break
  end

  number
end

#ask_string(question) ⇒ Object



111
112
113
114
# File 'lib/snapback.rb', line 111

def ask_string question
  print "#{question}: "
  $stdin.gets.chomp
end

#return_failedObject



76
77
78
79
# File 'lib/snapback.rb', line 76

def return_failed
  return nil if Snapback.quiet?
  puts "[#{"FAILED".colorize(:red)}]"
end

#return_noObject



71
72
73
74
# File 'lib/snapback.rb', line 71

def return_no
  return nil if Snapback.quiet?
  puts "[#{"  NO  ".colorize(:red)}]"
end

#return_okObject

Output status



66
67
68
69
# File 'lib/snapback.rb', line 66

def return_ok
  return nil if Snapback.quiet?
  puts "[#{"  OK  ".colorize(:green)}]"
end

#run_command(description, command = "", &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/snapback.rb', line 29

def run_command description, command = "", &block
  if Snapback.verbose?
    print "#{description}".to_s.ljust(72)
    STDOUT.flush
  end

  begin
    if block_given? then
      result = block.call

      if result then
        return_ok
      else
        return_no
      end
    else
      err = ""
      status = Open4::popen4(command) do |pid, stdin, stdout, stderr|
        err = stderr.read
      end

      if status != 0 then
        raise err
      end

      return_ok
    end
  rescue Exception => e 
    return_failed
    raise $! # rethrow
  end

  return result
end