Class: Salmon::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/salmon/salmon.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/salmon/salmon.rb', line 8

def self.run
    app = Application.new
    @options = app.parse_options(ARGV)
    source_github = app.config_github(@options.source)
    destination_github = app.config_github(@options.dest)

    repos = app.get_source_repos(source_github)

    abort("Retrieved no repositories") if repos.nil? or repos.empty?

    app.clone_repositories(destination_github, repos)
end

Instance Method Details

#clone_repositories(github, repos) ⇒ Object



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

def clone_repositories(github, repos)
    temp_dir = Dir.mktmpdir(@options.temp_path)
    i = 1

    repos.each do |repo|
        repo.org = (@options.dest.type == :organization) ? @options.dest.name : nil
        repo.user = (@options.dest.type == :user) ? @options.dest.name : nil
        repo.created = false
        # TODO: error handling
        puts "Working on #{repo.name}:  #{i}/#{repos.length}"
        local_repo = "#{temp_dir}/#{repo.name}"
        eat_output = @options.verbose ? '' : ' > /dev/null'
        clone_cmd = "git clone #{repo.ssh_url} #{local_repo}#{eat_output}"

        system(clone_cmd)
        abort('Clone failed!') if $? != 0

        puts "Creating repo on #{github.site}" if @options.verbose
        begin
            new_repo = github.repos.create(repo)
            repo.created = true
        rescue Github::Error::UnprocessableEntity => ue
            if ue.http_status_code == 422
                puts "Repo already exists." if @options.verbose
                new_repo = github.repos.get(@options.dest.name, repo.name)
            else
                puts(ue)
            end
        rescue StandardError => se
            puts(se)
        end

        if not new_repo.nil?
            Dir.chdir(local_repo) do
                system("git remote add clone #{new_repo['ssh_url']} #{eat_output}")
                system("git push --all clone #{eat_output}")
                system("git push --tags clone #{eat_output}") if @options.push_tags
                puts "Pushed" if !@options.verbose
            end
        else
            puts "Repo wasn't created, can't push. :("
        end

        i += 1
    end
end

#config_github(user_config) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/salmon/salmon.rb', line 25

def config_github(user_config)
    gh = Github.new do |config|
        user_config.marshal_dump.each do |key, value|
            config.send("#{key}=", value) if config.respond_to? "#{key}="
        end
        config.auto_pagination = true
        config.per_page = 100
    end
     = gh.users.get(user: user_config.name)
    user_config.type = .type.downcase.to_sym
    gh
end

#get_source_repos(github) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/salmon/salmon.rb', line 38

def get_source_repos(github)
    puts "Getting list of repos for #{@options.source.type}: #{@options.source.name}" if @options.verbose
    begin
        key = @options.source.type == :user ? :user : :org
        repos = github.repos.list("#{key}" => @options.source.name)
    rescue StandardError => e
        abort("Error getting list: #{e}")
    end
    repos
end

#parse_options(args) ⇒ Object



21
22
23
# File 'lib/salmon/salmon.rb', line 21

def parse_options(args)
    @options = Salmon::Config.parse_options(args)
end