Skip to content

Instantly share code, notes, and snippets.

@emad-elsaid
Created February 28, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emad-elsaid/e8009fb67cbef452b6ad4a352fc887c4 to your computer and use it in GitHub Desktop.
Save emad-elsaid/e8009fb67cbef452b6ad4a352fc887c4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'tty-prompt'
require 'open-uri'
require 'json'
require 'colorize'
require 'colorized_string'
prompt = TTY::Prompt.new
user_name = prompt.ask("What is name of the user or organisation?")
type = prompt.select("What type is it?", user: "users", organisation: "orgs")
token = ENV['GITHUB_API_TOKEN']
unless token
puts "Can't find GITHUB_API_TOKEN in you environment!".light_red
puts "Use the follow URL to generate a token: https://github.com/settings/tokens".light_red
puts "Then add it to your .bashrc/.bash_profile/...etc as a variable GITHUB_API_TOKEN".light_red
exit
end
page = -1
loop do
page += 1
url = "https://api.github.com/#{type}/#{user_name}/repos?access_token=#{token}&page=#{page}"
repos = open(url).read
repos = JSON.load(repos)
break if repos.empty?
repos.each do |repo|
if Dir.exist?(repo["name"])
puts "Updating #{repo["name"]}...".light_blue
Dir.chdir(repo["name"]) do
system("git pull --all")
end
else
puts "Cloning #{repo["name"]}...".light_blue
system("git clone #{repo["ssh_url"]}")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment