Sha256: 524b7304b19adff52c2b162d1163a98e772f5aa2673830706f6fc40981c9b74c

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module GitWit
  module Commands
    module Util
      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

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

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

      def app_user
        File.stat(rails_root).uid
      end

      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

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

      def booted?
        defined?(Rails)
      end
    end  
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
git_wit-0.0.6 lib/git_wit/commands/util.rb
git_wit-0.0.6.pre lib/git_wit/commands/util.rb