ghs 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/ghs +39 -46
  2. metadata +3 -3
data/bin/ghs CHANGED
@@ -1,64 +1,57 @@
1
- #!/usr/bin/env ruby -wKU
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
2
3
  require "yaml"
3
- require "pp"
4
- require 'optparse'
4
+ require 'trollop'
5
5
  require 'open-uri'
6
6
 
7
- # Command line options
8
- app = Hash.new
9
- options = OptionParser.new do |opts|
10
- opts.on("-b", "--best", "If multiple repos are found, choose the best one.") do |opt|
11
- app['best'] = opt
12
- end
13
- end
14
-
15
- begin
16
- options.parse!(ARGV)
17
- rescue OptionParser::ParseError => e
18
- STDERR.puts "#{$0}: #{e}"
7
+ def find_user
8
+ x = `git config github.user`
9
+ x.chomp
19
10
  end
20
11
 
21
- if ARGV.length < 1
22
- STDERR.puts "#{$0}: You need to specify a repo to search for."
23
- exit
12
+ def get_repo(user, repo)
13
+ begin
14
+ YAML.load(open("http://github.com/api/v2/yaml/repos/show/#{user}/#{repo}").read)
15
+ rescue
16
+ nil
17
+ end
24
18
  end
25
19
 
26
- repo = ARGV[0]
27
-
28
- # STEP 1: Search for the correct repo
29
- search = open("http://github.com/api/v2/yaml/repos/search/#{repo}").read
30
- results = YAML.load(search) # y is now a hash containing the yaml data.
31
-
32
- if results["repositories"].length > 1 && !app['best']
33
- STDERR.puts "#{$0}: Multiple repos found, not sure which one you wanted.\nTo let me make the choice, rerun with the --best flag."
34
- exit
35
- end
36
-
37
- repo_name = results["repositories"][0]["name"] || results["repositories"][0][:name]
38
- username = results["repositories"][0]["username"] || results["repositories"][0][:username]
20
+ def find_user_repo(repo)
21
+ user = find_user
22
+ return nil if user == ""
23
+ get_repo(user, repo)
24
+ end
39
25
 
40
- unless repo_name && username
41
- STDERR.puts "#{$0}: Couldn't find the url."
42
- exit
43
- end
26
+ def find_generic_repo(repo)
27
+ results = YAML.load(open("http://github.com/api/v2/yaml/repos/search/#{repo}").read)
28
+ Trollop::die "Couldn't find a url" if results["repositories"].empty?
29
+ Trollop::die "Multiple repos found, not sure which one you wanted.\nTo let me make the choice, rerun with the --best flag" unless results["repositories"].size == 1 || OPTS[:best]
30
+ user = results["repositories"][0][:username]
31
+ get_repo(user, repo)
32
+ end
44
33
 
45
- # STEP 2: Get the correct url
46
- show = open("http://github.com/api/v2/yaml/repos/show/#{username}/#{repo_name}").read
47
- repo_info = YAML.load(show) # y is now a hash containing the yaml data.
48
- url = repo_info["repository"][:url]
34
+ OPTS = Trollop::options do
35
+ banner <<-EOS
36
+ Find the clone url for any repo on github, quickly.
37
+ Usage: #{$0} [repo name]
38
+ Use it to clone a repo: git clone `ghs [reponame]`
39
+ EOS
40
+ opt :best, "If multiple repos are found, choose the first one."
41
+ end
49
42
 
50
- if !url
51
- STDERR.puts "#{$0}: Couldn't find the url."
52
- exit
53
- end
43
+ Trollop::die("no repo specified") if ARGV.empty?
44
+ result = find_user_repo(ARGV[0]) || find_generic_repo(ARGV[0])
45
+ Trollop::die "couldn't find a url." unless result
46
+ result = result["repository"]
54
47
 
55
48
  STDERR.puts "
56
49
  ==
57
50
 
58
- Matched repository: #{repo_name}
59
- Description: #{repo_info["repository"][:description]}
51
+ Matched repository: #{result[:name]}
52
+ Description: #{result[:description]}
60
53
 
61
54
  ==
62
55
  "
63
56
 
64
- puts "#{url}.git"
57
+ puts "#{result[:url]}.git"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghs
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Aditya Bhargava