Skip to content

Commit

Permalink
starting to refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Chadwick committed Jul 26, 2011
1 parent 75f90ca commit dc89365
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 58 deletions.
59 changes: 1 addition & 58 deletions bin/offline
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,6 @@ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])

require "rubygems" # ruby1.9 doesn't "require" it though
require "bundler/setup"
require "thor"
require 'offline'

class OfflineApp < Thor
include Offline::Helpers

desc "mirror GITHUB_USER", "mirror the repositories of a given github user"
method_option :only, :type => :array, :required => false
method_option :without, :type => :array, :required => false
method_option :password, :type => :string, :required => false, :aliases => '-p'
method_option :output, :type => :string, :default => 'mirror', :required => false, :aliases => '-o'
def mirror(user)
mirror_directory = "#{options[:output]}/#{user}"
Pathname.new(mirror_directory).mkpath
reaper = Offline::Github.new(user, options[:password])
all_repos = reaper.repositories.map {|r| r["name"] }
repos = all_repos & (options[:only] || all_repos) # TODO: Might be a better way of doing this
repos = (repos) - Array(options[:without])
reaper.repositories.each do |repo|
next unless repos.include?(repo["name"])
puts "Mirroring: #{repo["name"]}"
target_directory = Pathname.new("#{mirror_directory}/#{repo["name"]}.git")
if target_directory.exist?
run("cd #{target_directory} && git fetch")
else
run("git clone --mirror git@github.com:#{repo["owner"]}/#{repo["name"]}.git #{target_directory}")
end
puts "" # blank line
end
end

# TODO: Dry up
# Yes I did just copy and paste
desc "clone GITHUB_USER", "clone the repositories of a given github user"
method_option :only, :type => :array, :required => false
method_option :without, :type => :array, :required => false
method_option :password, :type => :string, :required => false, :aliases => '-p'
method_option :output, :type => :string, :default => 'clone', :required => false, :aliases => '-o'
def clone(user)
clone_directory = "#{options[:output]}/#{user}"
Pathname.new(clone_directory).mkpath
reaper = Offline::Github.new(user, options[:password])
all_repos = reaper.repositories.map {|r| r["name"] }
repos = all_repos & (options[:only] || all_repos) # TODO: Might be a better way of doing this
repos = (repos) - Array(options[:without])
reaper.repositories.each do |repo|
next unless repos.include?(repo["name"])
puts "Cloning: #{repo["name"]}"
target_directory = Pathname.new("#{clone_directory}/#{repo["name"]}")
if target_directory.exist?
run("cd #{target_directory} && git fetch")
else
run("git clone git@github.com:#{repo["owner"]}/#{repo["name"]}.git #{target_directory}")
end
puts "" # blank line
end
end
end

OfflineApp.start
Offline::App.start
2 changes: 2 additions & 0 deletions lib/offline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
require 'pathname'
require 'bundler/setup'

require "thor"
require 'httparty'

require 'offline/helpers'
require 'offline/app'
require 'offline/github'

module Offline
Expand Down
57 changes: 57 additions & 0 deletions lib/offline/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Offline
class App < Thor
include Offline::Helpers

desc "mirror GITHUB_USER", "mirror the repositories of a given github user"
method_option :only, :type => :array, :required => false
method_option :without, :type => :array, :required => false
method_option :password, :type => :string, :required => false, :aliases => '-p'
method_option :output, :type => :string, :default => 'mirror', :required => false, :aliases => '-o'
def mirror(user)
mirror_directory = "#{options[:output]}/#{user}"
Pathname.new(mirror_directory).mkpath
reaper = Offline::Github.new(user, options[:password])
all_repos = reaper.repositories.map {|r| r["name"] }
repos = all_repos & (options[:only] || all_repos) # TODO: Might be a better way of doing this
repos = (repos) - Array(options[:without])
reaper.repositories.each do |repo|
next unless repos.include?(repo["name"])
puts "Mirroring: #{repo["name"]}"
target_directory = Pathname.new("#{mirror_directory}/#{repo["name"]}.git")
if target_directory.exist?
run("cd #{target_directory} && git fetch")
else
run("git clone --mirror git@github.com:#{repo["owner"]}/#{repo["name"]}.git #{target_directory}")
end
puts "" # blank line
end
end

# TODO: Dry up
# Yes I did just copy and paste
desc "clone GITHUB_USER", "clone the repositories of a given github user"
method_option :only, :type => :array, :required => false
method_option :without, :type => :array, :required => false
method_option :password, :type => :string, :required => false, :aliases => '-p'
method_option :output, :type => :string, :default => 'clone', :required => false, :aliases => '-o'
def clone(user)
clone_directory = "#{options[:output]}/#{user}"
Pathname.new(clone_directory).mkpath
reaper = Offline::Github.new(user, options[:password])
all_repos = reaper.repositories.map {|r| r["name"] }
repos = all_repos & (options[:only] || all_repos) # TODO: Might be a better way of doing this
repos = (repos) - Array(options[:without])
reaper.repositories.each do |repo|
next unless repos.include?(repo["name"])
puts "Cloning: #{repo["name"]}"
target_directory = Pathname.new("#{clone_directory}/#{repo["name"]}")
if target_directory.exist?
run("cd #{target_directory} && git fetch")
else
run("git clone git@github.com:#{repo["owner"]}/#{repo["name"]}.git #{target_directory}")
end
puts "" # blank line
end
end
end
end
16 changes: 16 additions & 0 deletions lib/offline/github/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Offline
module Github
class Base
include Offline::Helpers

def initialize(user, options)

end

private
def clone(source, target="", mirror=false)
run("git clone #{"--mirror" if mirror} #{source} #{target}")
end
end
end
end
6 changes: 6 additions & 0 deletions lib/offline/github/clone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Offline
module Github
class Clone < Base
end
end
end
6 changes: 6 additions & 0 deletions lib/offline/github/mirror.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Offline
module Github
class Mirror < Base
end
end
end
15 changes: 15 additions & 0 deletions spec/offline/app_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe Offline::App do
context "Thor" do
it "should respond to start" do
Offline::App.should respond_to(:start)
end
end

context "#mirror" do
it "should respond to start" do
Offline::App.should respond_to(:start)
end
end
end

0 comments on commit dc89365

Please sign in to comment.