Module: Heroku::Helpers

Included in:
Auth, Client, Client, Client::Rendezvous, Command, Command::Base, Plugin, Plugin, HerokuPostgresql::Client, PGBackups::Client
Defined in:
lib/heroku/helpers.rb

Constant Summary

@@kb =
1024
@@mb =
1024 * @@kb
@@gb =
1024 * @@mb

Instance Method Summary (collapse)

Instance Method Details

- (Object) app_urls(name)



163
164
165
# File 'lib/heroku/helpers.rb', line 163

def app_urls(name)
  "http://#{name}.heroku.com/ | git@heroku.com:#{name}.git"
end

- (Object) ask



86
87
88
# File 'lib/heroku/helpers.rb', line 86

def ask
  gets.strip
end

- (Object) confirm(message = "Are you sure you wish to continue? (y/n)?")



51
52
53
54
# File 'lib/heroku/helpers.rb', line 51

def confirm(message="Are you sure you wish to continue? (y/n)?")
  display("#{message} ", false)
  ask.downcase == 'y'
end

- (Object) confirm_billing



40
41
42
43
44
45
46
47
48
49
# File 'lib/heroku/helpers.rb', line 40

def confirm_billing
  display
  display "This action will cause your account to be billed at the end of the month"
  display "For more information, see http://devcenter.heroku.com/articles/billing"
  display "Are you sure you want to do this? (y/n) ", false
  if ask.downcase == 'y'
    heroku.confirm_billing
    return true
  end
end

- (Object) confirm_command(app = app)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/heroku/helpers.rb', line 56

def confirm_command(app = app)
  raise(Heroku::Command::CommandFailed, "No app specified.\nRun this command from app folder or set it adding --app <app name>") unless app

  confirmed_app = extract_option('--confirm', false)
  if confirmed_app
    unless confirmed_app == app
      raise(Heroku::Command::CommandFailed, "Confirmed app #{confirmed_app} did not match the selected app #{app}.")
    end
    return true
  else
    display
    display " !    WARNING: Potentially Destructive Action"
    display " !    This command will affect the app: #{app}"
    display " !    To proceed, type \"#{app}\" or re-run this command with --confirm #{app}"
    display
    display "> ", false
    if ask.downcase != app
      display " !    Input did not match #{app}. Aborted."
      false
    else
      true
    end
  end
end

- (Object) create_git_remote(app, remote)



155
156
157
158
159
160
161
# File 'lib/heroku/helpers.rb', line 155

def create_git_remote(app, remote)
  return unless has_git?
  return if git('remote').split("\n").include?(remote)
  return unless File.exists?(".git")
  git "remote add #{remote} git@#{heroku.host}:#{app}.git"
  display "Git remote #{remote} added"
end

- (Object) deprecate(version)



30
31
32
33
# File 'lib/heroku/helpers.rb', line 30

def deprecate(version)
  display "!!! DEPRECATION WARNING: This command will be removed in version #{version}"
  display
end

- (Object) display(msg = "", newline = true)



17
18
19
20
21
22
23
24
# File 'lib/heroku/helpers.rb', line 17

def display(msg="", newline=true)
  if newline
    puts(msg)
  else
    print(msg)
    STDOUT.flush
  end
end

- (Object) display_row(row, lengths)



184
185
186
187
188
189
190
# File 'lib/heroku/helpers.rb', line 184

def display_row(row, lengths)
  row.zip(lengths).each do |column, length|
    format = column.is_a?(Fixnum) ? "%#{length}s  " : "%-#{length}s  "
    display format % column, false
  end
  display
end

- (Object) display_table(objects, columns, headers)



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/heroku/helpers.rb', line 171

def display_table(objects, columns, headers)
  lengths = []
  columns.each_with_index do |column, index|
    header = headers[index]
    lengths << longest([header].concat(objects.map { |o| o[column].to_s }))
  end
  display_row headers, lengths
  display_row headers.map { |header| "-" * header.length }, lengths
  objects.each do |row|
    display_row columns.map { |column| row[column] }, lengths
  end
end

- (Object) error(msg)



35
36
37
38
# File 'lib/heroku/helpers.rb', line 35

def error(msg)
  STDERR.puts(msg)
  exit 1
end

- (Object) fail(message)



216
217
218
# File 'lib/heroku/helpers.rb', line 216

def fail(message)
  raise Heroku::Command::CommandFailed, message
end

- (Object) format_bytes(amount)



142
143
144
145
146
147
148
149
# File 'lib/heroku/helpers.rb', line 142

def format_bytes(amount)
  amount = amount.to_i
  return '(empty)' if amount == 0
  return amount if amount < @@kb
  return "#{(amount / @@kb).round}k" if amount < @@mb
  return "#{(amount / @@mb).round}M" if amount < @@gb
  return "#{(amount / @@gb).round}G"
end

- (Object) format_date(date)



81
82
83
84
# File 'lib/heroku/helpers.rb', line 81

def format_date(date)
  date = Time.parse(date) if date.is_a?(String)
  date.strftime("%Y-%m-%d %H:%M %Z")
end

- (Object) get_terminal_environment



212
213
214
# File 'lib/heroku/helpers.rb', line 212

def get_terminal_environment
  { "TERM" => ENV["TERM"], "COLUMNS" => `tput cols`, "LINES" => `tput lines` }
end

- (Object) git(args)



115
116
117
118
119
# File 'lib/heroku/helpers.rb', line 115

def git(args)
  return "" unless has_git?
  flattened_args = [args].flatten.compact.join(" ")
  %x{ git #{flattened_args} 2>&1 }.strip
end

- (Boolean) has_git?

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/heroku/helpers.rb', line 110

def has_git?
  %x{ git --version }
  $?.success?
end

- (Object) home_directory



5
6
7
# File 'lib/heroku/helpers.rb', line 5

def home_directory
  running_on_windows? ? ENV['USERPROFILE'] : ENV['HOME']
end

- (Object) json_decode(json)



198
199
200
201
202
# File 'lib/heroku/helpers.rb', line 198

def json_decode(json)
  OkJson.decode(json)
rescue OkJson::ParserError
  nil
end

- (Object) json_encode(object)



192
193
194
195
196
# File 'lib/heroku/helpers.rb', line 192

def json_encode(object)
  OkJson.encode(object)
rescue OkJson::ParserError
  nil
end

- (Object) longest(items)



167
168
169
# File 'lib/heroku/helpers.rb', line 167

def longest(items)
  items.map(&:to_s).map(&:length).sort.last
end

- (Object) quantify(string, num)



151
152
153
# File 'lib/heroku/helpers.rb', line 151

def quantify(string, num)
  "%d %s" % [ num, num.to_i == 1 ? string : "#{string}s" ]
end

- (Object) redisplay(line, line_break = false)



26
27
28
# File 'lib/heroku/helpers.rb', line 26

def redisplay(line, line_break = false)
  display("\r\e[0K#{line}", line_break)
end

- (Object) retry_on_exception(*exceptions)



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/heroku/helpers.rb', line 98

def retry_on_exception(*exceptions)
  retry_count = 0
  begin
    yield
  rescue *exceptions => ex
    raise ex if retry_count >= 3
    sleep 3
    retry_count += 1
    retry
  end
end

- (Object) run_command(command, args = [])



94
95
96
# File 'lib/heroku/helpers.rb', line 94

def run_command(command, args=[])
  Heroku::Command.run(command, args)
end

- (Boolean) running_on_a_mac?

Returns:

  • (Boolean)


13
14
15
# File 'lib/heroku/helpers.rb', line 13

def running_on_a_mac?
  RUBY_PLATFORM =~ /-darwin\d/
end

- (Boolean) running_on_windows?

Returns:

  • (Boolean)


9
10
11
# File 'lib/heroku/helpers.rb', line 9

def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end

- (Object) set_buffer(enable)



204
205
206
207
208
209
210
# File 'lib/heroku/helpers.rb', line 204

def set_buffer(enable)
  if enable
    `stty icanon echo`
  else
    `stty -icanon -echo`
  end
end

- (Object) shell(cmd)



90
91
92
# File 'lib/heroku/helpers.rb', line 90

def shell(cmd)
  FileUtils.cd(Dir.pwd) {|d| return `#{cmd}`}
end

- (Object) time_ago(elapsed)



121
122
123
124
125
126
127
128
129
# File 'lib/heroku/helpers.rb', line 121

def time_ago(elapsed)
  if elapsed < 60
    "#{elapsed.floor}s ago"
  elsif elapsed < (60 * 60)
    "#{(elapsed / 60).floor}m ago"
  else
    "#{(elapsed / 60 / 60).floor}h ago"
  end
end

- (Object) truncate(text, length)



131
132
133
134
135
136
137
# File 'lib/heroku/helpers.rb', line 131

def truncate(text, length)
  if text.size > length
    text[0, length - 2] + '..'
  else
    text
  end
end