Class: GitSSHWrapper::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/git-ssh-wrapper/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



28
29
30
# File 'lib/git-ssh-wrapper/cli.rb', line 28

def initialize(*args)
  @key, *@command = *args
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



26
27
28
# File 'lib/git-ssh-wrapper/cli.rb', line 26

def command
  @command
end

#keyObject (readonly)

Returns the value of attribute key.



26
27
28
# File 'lib/git-ssh-wrapper/cli.rb', line 26

def key
  @key
end

Class Method Details

.git_ssh(args = ARGV) ⇒ Object

Inserts git into the command if it’s not there. The command must be a git command.

Examples of argv:

[key_path, fetch, origin]
[key_path, git, fetch, origin]


20
21
22
23
24
# File 'lib/git-ssh-wrapper/cli.rb', line 20

def self.git_ssh(args=ARGV)
  key, *command = *args
  command.unshift('git') unless command.first == 'git'
  new(key, *command).call
end

.wrapper(args = ARGV) ⇒ Object

Calls any command with GIT_SSH set.

Examples of argv:

[key_path, git, fetch, origin]
[key_path, some/script, that, calls, git]


10
11
12
# File 'lib/git-ssh-wrapper/cli.rb', line 10

def self.wrapper(args=ARGV)
  new(*args).call
end

Instance Method Details

#binObject



68
69
70
# File 'lib/git-ssh-wrapper/cli.rb', line 68

def bin
  File.basename($0)
end

#callObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/git-ssh-wrapper/cli.rb', line 32

def call
  if %w[--help -h help].include?(key)
    print_help
  end

  if %w[--version -v].include?(key)
    print_version
  end

  if key.nil? || command.empty?
    error
  elsif !File.exist?(key)
    error "private key not found: #{key.inspect}"
  end

  GitSSHWrapper.with_wrapper(:private_key_path => key) do |wrapper|
    wrapper.set_env
    system *command
    exit $?.exitstatus
  end

  exit 1
end

#error(message = nil) ⇒ Object



76
77
78
# File 'lib/git-ssh-wrapper/cli.rb', line 76

def error(message=nil)
  abort [message,usage].compact.join("\n")
end


56
57
58
59
60
61
# File 'lib/git-ssh-wrapper/cli.rb', line 56

def print_help
  puts "Run remote git commands using only the specified ssh private key."
  puts
  puts usage
  exit 0
end


63
64
65
66
# File 'lib/git-ssh-wrapper/cli.rb', line 63

def print_version
  puts "git-ssh-wrapper version #{GitSSHWrapper::VERSION}"
  exit 0
end

#usageObject



72
73
74
# File 'lib/git-ssh-wrapper/cli.rb', line 72

def usage
  "usage:\t#{bin} ssh.key command"
end