Module: GitWit::Commands::Util

Included in:
GitWit::Cli
Defined in:
lib/git_wit/commands/util.rb

Instance Method Summary collapse

Instance Method Details

#app_userObject



21
22
23
# File 'lib/git_wit/commands/util.rb', line 21

def app_user
  File.stat(rails_root).uid
end

#boot_appObject



32
33
34
35
# File 'lib/git_wit/commands/util.rb', line 32

def boot_app
  require File.expand_path File.join(rails_root, "config/environment") unless booted?
  require "git_wit"
end

#booted?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/git_wit/commands/util.rb', line 37

def booted?
  defined?(Rails)
end

#exec_with_sudo!(user = app_user) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/git_wit/commands/util.rb', line 4

def exec_with_sudo!(user = app_user)
  return if running_as?(user)
  Dir.chdir rails_root
  ENV["TERM"] = "dumb"
  load_gw_env
  cmd = ["sudo", "-u", "##{user}", $PROGRAM_NAME, *ARGV]
  exec *cmd
end

#load_gw_envObject



13
14
15
# File 'lib/git_wit/commands/util.rb', line 13

def load_gw_env
  ENV.keys.each { |k| ENV[k.gsub /^GW_/, ""] = ENV[k] if k.start_with? "GW_" }
end

#rails_rootObject



25
26
27
28
29
30
# File 'lib/git_wit/commands/util.rb', line 25

def rails_root
  return File.expand_path(ENV["RAILS_ROOT"]) if ENV["RAILS_ROOT"]
  return Dir.pwd if File.exist? File.join(Dir.pwd, "config/environment.rb")
  return File.expand_path("..", ENV["BUNDLE_GEMFILE"]) if ENV["BUNDLE_GEMFILE"]
  Dir.pwd
end

#running_as?(user) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/git_wit/commands/util.rb', line 17

def running_as?(user)
  Process.uid == user
end