Class: Heroku::Command::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/heroku/commands/base.rb

Direct Known Subclasses

Account, App, Auth, BaseWithApp, Help, Keys, Plugins, Ps, Service, Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#home_directory, #running_on_a_mac?, #running_on_windows?

Constructor Details

#initialize(args, heroku = nil) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
# File 'lib/heroku/commands/base.rb', line 9

def initialize(args, heroku=nil)
	@args = args
	@heroku = heroku
	@autodetected_app = false
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



7
8
9
# File 'lib/heroku/commands/base.rb', line 7

def args
  @args
end

#autodetected_appObject (readonly)

Returns the value of attribute autodetected_app.



8
9
10
# File 'lib/heroku/commands/base.rb', line 8

def autodetected_app
  @autodetected_app
end

Instance Method Details

#app_urls(name) ⇒ Object



116
117
118
# File 'lib/heroku/commands/base.rb', line 116

def app_urls(name)
	"#{web_url(name)} | #{git_url(name)}"
end

#askObject



33
34
35
# File 'lib/heroku/commands/base.rb', line 33

def ask
	gets.strip
end

#display(msg, newline = true) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/heroku/commands/base.rb', line 15

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

#error(msg) ⇒ Object



29
30
31
# File 'lib/heroku/commands/base.rb', line 29

def error(msg)
	Heroku::Command.error(msg)
end

#escape(value) ⇒ Object



120
121
122
# File 'lib/heroku/commands/base.rb', line 120

def escape(value)
	heroku.escape(value)
end

#extract_app(force = true) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/heroku/commands/base.rb', line 45

def extract_app(force=true)
	app = extract_option('--app')
	unless app
		app = extract_app_in_dir(Dir.pwd) ||
		raise(CommandFailed, "No app specified.\nRun this command from app folder or set it adding --app <app name>") if force
		@autodetected_app = true
	end
	app
end

#extract_app_in_dir(dir) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/heroku/commands/base.rb', line 55

def extract_app_in_dir(dir)
	return unless remotes = git_remotes(dir)

	if remote = extract_option('--remote')
		remotes[remote]
	else
		apps = remotes.values.uniq
		case apps.size
			when 0; return nil
			when 1; return apps.first
			else
				current_dir_name = dir.split('/').last.downcase
				apps.select { |a| a.downcase == current_dir_name }.first
		end
	end
end

#extract_option(options, default = true) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/heroku/commands/base.rb', line 92

def extract_option(options, default=true)
	values = options.is_a?(Array) ? options : [options]
	return unless opt_index = args.select { |a| values.include? a }.first
	opt_position = args.index(opt_index) + 1
	if args.size > opt_position && opt_value = args[opt_position]
		if opt_value.include?('--')
			opt_value = nil
		else
			args.delete_at(opt_position)
		end
	end
	opt_value ||= default
	args.delete(opt_index)
	block_given? ? yield(opt_value) : opt_value
end

#format_date(date) ⇒ Object



24
25
26
27
# File 'lib/heroku/commands/base.rb', line 24

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

#git_remotes(base_dir) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/heroku/commands/base.rb', line 72

def git_remotes(base_dir)
	git_config = "#{base_dir}/.git/config"
	unless File.exists?(git_config)
		parent = base_dir.split('/')[0..-2].join('/')
		return git_remotes(parent) unless parent.empty?
	else
		remotes = {}
		current_remote = nil
		File.read(git_config).split(/\n/).each do |l|
			current_remote = $1 if l.match(/\[remote \"([\w\d-]+)\"\]/)
			app = (l.match(/url = git@#{heroku.host}:([\w\d-]+)\.git/) || [])[1]
			if current_remote && app
				remotes[current_remote.downcase] = app
				current_remote = nil
			end
		end
		return remotes
	end
end

#git_url(name) ⇒ Object



112
113
114
# File 'lib/heroku/commands/base.rb', line 112

def git_url(name)
	"git@#{heroku.host}:#{name}.git"
end

#herokuObject



41
42
43
# File 'lib/heroku/commands/base.rb', line 41

def heroku
	@heroku ||= Heroku::Command.run_internal('auth:client', args)
end

#shell(cmd) ⇒ Object



37
38
39
# File 'lib/heroku/commands/base.rb', line 37

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

#web_url(name) ⇒ Object



108
109
110
# File 'lib/heroku/commands/base.rb', line 108

def web_url(name)
	"http://#{name}.#{heroku.host}/"
end