Class: GitAuth::Client

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/gitauth/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_name, command) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
33
# File 'lib/gitauth/client.rb', line 28

def initialize(user_name, command)
  logger.debug "Initializing client with command: #{command.inspect} and user name #{user_name.inspect}"
  @callbacks = Hash.new { |h,k| h[k] = [] }
  @user      = GitAuth::User.get(user_name.to_s.strip)
  @command   = command
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



26
27
28
# File 'lib/gitauth/client.rb', line 26

def command
  @command
end

#userObject

Returns the value of attribute user.



26
27
28
# File 'lib/gitauth/client.rb', line 26

def user
  @user
end

Class Method Details

.start!(user, command) {|client| ... } ⇒ Object

Yields:

  • (client)


77
78
79
80
81
82
83
84
85
86
# File 'lib/gitauth/client.rb', line 77

def self.start!(user, command)
  # Gitorious does it so I should too!
  File.umask(0022)
  # Setup models etc
  GitAuth.prepare
  # Finally, create and initialize
  client = self.new(user, command)
  yield client if block_given?
  client.run!
end

Instance Method Details

#exit_with_error(error) ⇒ Object



35
36
37
38
39
# File 'lib/gitauth/client.rb', line 35

def exit_with_error(error)
  logger.warn "Exiting with error: #{error}"
  $stderr.puts error
  exit! 1
end

#run!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gitauth/client.rb', line 41

def run!
  if @user.nil?
    exit_with_error "An invalid user / key was used. Please ensure it is setup with GitAuth"
  elsif @command.to_s.strip.empty?
    if user.shell_accessible?
      exec(ENV["SHELL"])
    else
      exit_with_error "SSH_ORIGINAL_COMMAND is needed, mmmkay?"
    end
  else
    command   = Command.parse(@command)
    repo      = command.bad? ? nil : Repo.get(extract_repo_name(command))
    if command.bad?
      if user.shell_accessible?
        exec(@command)
      else
        exit_with_error "A Bad Command Has Failed Ye, Thou Shalt Not Continue."
      end
    elsif repo.nil?
      exit_with_error "Ze repository you specified does not exist."
    elsif user.can_execute?(command, repo)
      git_shell_argument = "#{command.verb} '#{repo.real_path}'"
      logger.info "Running command: #{git_shell_argument} for user: #{@user.name}"
      exec("git-shell", "-c", git_shell_argument)
    else
      exit_with_error "These are not the droids you are looking for"
    end
  end
rescue Exception => e
  logger.fatal "Exception: #{e.class.name}: #{e.message}"
  e.backtrace.each do |l|
    logger.fatal "  => #{l}"
  end
  exit_with_error "Holy crap, we've imploded cap'n!"
end