Class: Git::Open::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/git/open.rb

Class Method Summary collapse

Class Method Details

.executeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/git/open.rb', line 6

def self.execute
  require "slop"
  options = Slop.parse do |option|
    option.on "-h".freeze, "--help".freeze, "Show this help".freeze
    option.on "-v".freeze, "--version".freeze, "print the version".freeze do
      puts_and_exit "git-open version #{Git::Open::VERSION}"
    end
  end

  show_help(options) if options[:h] || options[:help] || ARGV.include?("help".freeze)

  abort("Not a git repo".freeze) if `git rev-parse --is-inside-work-tree`.chomp! != "true".freeze

  remotes = `git remote`.split("\n".freeze)

  abort("No remote found".freeze) if remotes.empty?

  url = if remotes.find { |remote| remote == "origin".freeze }
    `git config --get remote.origin.url`.chomp!
  else
    `git config --get remote.#{remotes.first}.url`.chomp!
  end

  require "git/remote/parser"; require "launchy"
  Launchy.open(open_url = Git::Remote::Parser.new.parse(url).html_url) do |exception|
    puts "Attempted to open `#{open_url.to_s}' and failed because #{exception}"
  end
end

.puts_and_exit(message) ⇒ Object



39
40
41
42
# File 'lib/git/open.rb', line 39

def self.puts_and_exit(message)
  puts(message)
  exit
end

.show_help(options) ⇒ Object



35
36
37
# File 'lib/git/open.rb', line 35

def self.show_help(options)
  puts_and_exit(options)
end