Instantly share code, notes, and snippets.

@obfusk /gists.rb
Last active Dec 15, 2015

Embed
What would you like to do?
list github repos and gists (w/ desc, home, branches)
#!/usr/bin/ruby
require 'io/console'
require 'json'
require 'open-uri'
# --
pag = ->(data; l, n, r) {
(l = data.meta['link']) &&
(n = l.split(',').grep(/rel="next"/).first) &&
(r = l.match(%r{<(https://[^>]*)>})) && r[1]
}
jcat = ->(pages) { pages.map { |x| JSON.load x } .flatten 1 }
paginate = ->(url, f) {
pages = []
while url; pages << data = f[url]; url = pag[data]; end
pages
}
prompt = ->(prompt) {
STDERR.print prompt; STDERR.flush
line = STDIN.noecho { |i| i.gets }; STDERR.puts
line and line.chomp
}
# --
verbose = if ARGV.first == '-v' then ARGV.shift; true else false end
ARGV.each do |user|
if user.include? '@'
au, user = user.split '@'; au = user if au.empty?
ap = prompt["password for #{user}: "]
opts = { http_basic_authentication: [au, ap] }
else
opts = {}
end
url = "https://api.github.com/users/#{user}/gists"
rs = jcat[paginate[url, ->(u) { open u, opts }]]
puts "#{user}[#{rs.size}]/"
rs.each do |repo|
puts " #{ repo['id'] }"
if verbose
puts " desc: #{ repo['description'] }"
end
end
end
#!/usr/bin/ruby
require 'io/console'
require 'json'
require 'open-uri'
# --
pag = ->(data; l, n, r) {
(l = data.meta['link']) &&
(n = l.split(',').grep(/rel="next"/).first) &&
(r = l.match(%r{<(https://[^>]*)>})) && r[1]
}
jcat = ->(pages) { pages.map { |x| JSON.load x } .flatten 1 }
paginate = ->(url, f) {
pages = []
while url; pages << data = f[url]; url = pag[data]; end
pages
}
prompt = ->(prompt) {
STDERR.print prompt; STDERR.flush
line = STDIN.noecho { |i| i.gets }; STDERR.puts
line and line.chomp
}
# --
verbose = if ARGV.first == '-v' then ARGV.shift; true else false end
ARGV.each do |user|
if user.include? '@'
au, user = user.split '@'; au = user if au.empty?
ap = prompt["password for #{user}: "]
opts = { http_basic_authentication: [au, ap] }
else
opts = {}
end
url = "https://api.github.com/users/#{user}/repos"
rs = jcat[paginate[url, ->(u) { open u, opts }]]
puts "#{user}[#{rs.size}]/"
rs.each do |repo|
puts " #{ repo['name'] }"
if verbose
puts " desc: #{ repo['description'] }"
puts " home: #{ repo['homepage'] }"
puts " branches:"
system "git ls-remote #{ repo['clone_url'] }" +
" | grep -v HEAD | sed 's!^! !'"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment