-
Notifications
You must be signed in to change notification settings - Fork 2
/
clone_all.rb
executable file
·192 lines (148 loc) · 4.12 KB
/
clone_all.rb
1
2
3
4
5
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
34
35
36
37
38
39
40
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env ruby
# encoding: utf-8
##########################################################
###
## File: clone_all.rb
## Desc: Clone all the repos of a github user
## By: Dewayne VanHoozer ([email protected])
#
require 'debug_me' # A tool to print the labeled value of variables.
include DebugMe
require 'pathname' # STDLIB
require 'github_api' # Ruby client for the official GitHub API
Github.configure do |c|
c.auto_pagination = true
end
me = Pathname.new(__FILE__).realpath
my_dir = me.parent
my_name = me.basename.to_s
$options = {
verbose: false,
debug: false,
user: ENV['GITHUB_ACCOUNT'],
out_dirname: Pathname.pwd
}
def verbose?
$options[:verbose]
end
def debug?
$options[:debug]
end
usage = <<EOS
Clone all the github repos for a given user account
Usage: #{my_name} [options] [user_name]
Where:
options Do This
-h or --help Display this message
-v or --verbose Display progress
-d or --debug Kill the bugs
-o or --dirname The directory into which to
out_dirname clone the repos.
Default: #{$options[:out_dirname]}
user_name The name of the github user
Default: #{$options[:user]}
EOS
# Check command line for Problems with Parameters
$errors = []
$warnings = []
# Get the next ARGV parameter after param_index
def get_next_parameter(param_index)
unless Fixnum == param_index.class
param_index = ARGV.find_index(param_index)
end
next_parameter = nil
if param_index+1 >= ARGV.size
$errors << "#{ARGV[param_index]} specified without parameter"
else
next_parameter = ARGV[param_index+1]
ARGV[param_index+1] = nil
end
ARGV[param_index] = nil
return next_parameter
end # def get_next_parameter(param_index)
# Get $options[:out_filename]
def get_out_dirname(param_index)
dirname_str = get_next_parameter(param_index)
$options[:out_dirname] = Pathname.new( dirname_str ) unless dirname_str.nil?
end # def get_out_dirname(param_index)
# Display global warnings and errors arrays and exit if necessary
def abort_if_errors
unless $warnings.empty?
STDERR.puts
STDERR.puts "The following warnings were generated:"
STDERR.puts
$warnings.each do |w|
STDERR.puts "\tWarning: #{w}"
end
STDERR.print "\nAbort program? (y/N) "
answer = (gets).chomp.strip.downcase
$errors << "Aborted by user" if answer.size>0 && 'y' == answer[0]
end
unless $errors.empty?
STDERR.puts
STDERR.puts "Correct the following errors and try again:"
STDERR.puts
$errors.each do |e|
STDERR.puts "\t#{e}"
end
STDERR.puts
exit(-1)
end
end # def abort_if_errors
# Display the usage info
if ARGV.include?('-h') ||
ARGV.include?('--help')
puts usage
exit
end
%w[ -v --verbose ].each do |param|
if ARGV.include? param
$options[:verbose] = true
ARGV[ ARGV.index(param) ] = nil
end
end
%w[ -d --debug ].each do |param|
if ARGV.include? param
$options[:debug] = true
ARGV[ ARGV.index(param) ] = nil
end
end
%w[ -o --outdir ].each do |param|
get_out_dirname( ARGV.index(param) ) if ARGV.include?(param)
unless $options[:out_dirname].nil?
unless $options[:out_dirname].exist?
$errors << "Directory does not exist: #{$options[:out_dirname].parent}"
end
end
end
ARGV.compact!
if ARGV.size > 1
$errors << "Only one user at a time."
end
abort_if_errors
$options[:user] = ARGV.first unless ARGV.empty?
######################################################
# Local methods
######################################################
# Main
if verbose?
at_exit do
puts
puts "Done."
puts
end
end
pp $options if verbose? || debug?
repos = Github.repos.list user: $options[:user]
repos.each do |r|
out_path = Pathname.new "#{$options[:out_dirname]}/#{r.name}"
if out_path.exist?
puts "Skipping #{r.name} ..." if verbose?
next
end
puts "Cloning #{r.name} ..." if verbose?
command = "git clone #{r.clone_url} " + out_path.to_s
puts command if verbose? || debug?
system command unless debug?
puts
end