Module: GithubDownloader

Defined in:
lib/github_downloader.rb,
lib/github_downloader/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.from_cmdline(args) ⇒ Object



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
# File 'lib/github_downloader.rb', line 32

def self.from_cmdline(args)
  show_error 'no options specified, try with --help and see all the available options' unless ARGV.size > 0

  options = Parser.new do |p|
    p.banner = 'GitHub Repo Downloader'
    p.version = VERSION
    p.option :username, 'Your GitHub user', :default => '', :short => 'u'
    p.option :password, 'Your GitHub password', :default => '', :short => 'p'
    p.option :organization, 'GitHub Organization (optional)', :default => '', :short => 'g'
    p.option :from, 'GitHub user from which we want to download (optional)', :default => '', :short => 'r'
    p.option :output, 'Output directory', :default => '.', :short => 'o'
  end.process!

  show_error 'no user specified, please check --help to see all available options' if options[:username].blank?
  show_error 'no password specified, please check --help to see all available options' if options[:password].blank?
  if (options[:organization].blank? && options[:from].blank?) || (!options[:organization].blank? && !options[:from].blank?)
    show_error 'either an organization or an user must be provided to download repositories, not both or none'
  end

  output_dir = options[:output]
  user = options[:username]
  pass = options[:password]
  organization = options[:organization]
  user_repo = options[:from]

  github = Github.new basic_auth: "#{user}:#{pass}" do |config|
    config.org = organization
    config.auto_pagination = true
  end

  FileUtils.makedirs output_dir if output_dir != '.'

# Either chose repos from an organization or an user
  all_repos = github.repos.all unless organization.blank?
  all_repos = github.repos.list user: user_repo unless user_repo.blank?

  all_repos.each do |repo|
    print "Downloading archive for #{repo.full_name}... "
    Dir.chdir output_dir do
      run "curl -i -u #{user}:#{pass} -o #{repo.name}-master-archived.zip -L #{repo.html_url}/archive/master.zip"
    end
  end
end

.run(command) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/github_downloader.rb', line 21

def self.run(command)
  output = `#{command} 2> github_downloader_error.log`
  if $?.exitstatus > 0
    print "Failed!\n".red
    puts output
    exit(-1)
  else
    print "Done!\n".green
  end
end

.show_error(error_string) ⇒ Object



15
16
17
18
19
# File 'lib/github_downloader.rb', line 15

def self.show_error(error_string)
  puts 'Error!'.red
  puts error_string
  exit
end