Module: Dawn::CLI::Helpers

Includes:
OutputFormatter
Included in:
App, Auth, Domain, Drain, Env, Key, Local, Release
Defined in:
lib/dawn/cli/helpers.rb

Instance Method Summary collapse

Methods included from OutputFormatter

#format_apps, #format_domains, #format_drains, #format_gears, #format_keys, #table_style

Instance Method Details

#current_app(options = {}) ⇒ Object



94
95
96
97
# File 'lib/dawn/cli/helpers.rb', line 94

def current_app(options={})
  name = current_app_name(options)
  Dawn::App.find(name: name)
end

#current_app_name(options = {}) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/dawn/cli/helpers.rb', line 86

def current_app_name(options={})
  @current_app ||= begin
    options[:app] || ENV["DAWN_APP"] ||
    Dawn::CLI.selected_app || extract_app_in_dir(Dir.pwd, options) ||
    abort("App could not be located!")
  end
end

#extract_app_in_dir(dir, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dawn/cli/helpers.rb', line 70

def extract_app_in_dir(dir, options={})
  return unless remotes = git_remotes(dir)
  if remote = options[:remote]
    remotes[remote]
  elsif remote = extract_app_remote_from_git_config
    remotes[remote]
  else
    apps = remotes.values.uniq
    if apps.size == 1
      apps.first
    else
      abort "Multiple apps in folder and no app specified.\nSpecify app with --app APP."
    end
  end
end

#extract_app_remote_from_git_configObject



65
66
67
68
# File 'lib/dawn/cli/helpers.rb', line 65

def extract_app_remote_from_git_config
  remote = git "config dawn.remote"
  remote.empty? ? nil : remote
end

#git(args) ⇒ Object



18
19
20
21
22
# File 'lib/dawn/cli/helpers.rb', line 18

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

#git_add_dawn_remote(app) ⇒ Object



60
61
62
63
# File 'lib/dawn/cli/helpers.rb', line 60

def git_add_dawn_remote(app)
  abort "dawn remote already exists, please `dawn app:delete` first" if git_dawn_remote?
  git "remote add dawn git@#{Dawn.git_host}:#{app.git}"
end

#git_dawn_remote?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dawn/cli/helpers.rb', line 51

def git_dawn_remote?
  !!(git_remotes && git_remotes["dawn"])
end

#git_remotes(base_dir = Dir.pwd) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dawn/cli/helpers.rb', line 36

def git_remotes(base_dir=Dir.pwd)
  remotes = {}
  original_dir = Dir.pwd
  Dir.chdir(base_dir) do
    return unless File.exists?(".git")
    git("remote -v").split("\n").each do |remote|
      name, url, method = remote.split(/\s+/)
      if url =~ /^git@#{Dawn.git_host}:([\w\d-]+)\.git$/
        remotes[name] = $1
      end
    end
  end
  remotes.empty? ? nil : remotes
end

#git_remove_dawn_remote(app) ⇒ Object



55
56
57
58
# File 'lib/dawn/cli/helpers.rb', line 55

def git_remove_dawn_remote(app)
  # remove old dawn remote
  git "remote remove dawn"
end

#has_git?Boolean

swiped from Heroku

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/dawn/cli/helpers.rb', line 13

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

#try_create_app(appname = nil) ⇒ Object

CLI App resolver



25
26
27
28
29
30
31
32
33
34
# File 'lib/dawn/cli/helpers.rb', line 25

def try_create_app(appname=nil)
  abort "dawn remote already exists, please remove it (dawn app:delete)" if git_dawn_remote?
  begin
    app = Dawn::App.create(app: { name: appname })
  rescue Excon::Errors::Conflict
    app = Dawn::App.find(name: appname)
    puts " warning ! App (#{app.name}) already exists"
  end
  return app
end