Class: GhqTransfer::Core

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Core

Returns a new instance of Core.



7
8
9
# File 'lib/ghq_transfer.rb', line 7

def initialize(options)
  @options = options
end

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ghq_transfer.rb', line 11

def run
  puts valth? ? '*** VALTH!! ***' : '*** dry run ***'

  ghq_root = Pathname(`ghq root`.chomp)

  Dir.glob(ghq_root.join('*')).each do |src|
    next unless File.ftype(src) == 'directory'

    puts "[DEBUG] #{src}" if @options[:debug]

    Dir.chdir(src)

    origin_url = `git config --get remote.origin.url`

    next if origin_url.empty?

    origin_url.chomp!

    host, user, repo = if /^git@.+/ === origin_url
                         extract_paths_from_ssh(origin_url)
                       else
                         extract_paths_from_https(origin_url)
                       end

    begin
      dest_dir = ghq_root.join(host, user)
      dest_path = dest_dir.join(repo)

      puts "#{src} -> #{dest_path}"

      if valth?
        FileUtils.mkdir_p(dest_dir)

        FileUtils.mv(src, dest_path)
      end
    rescue
      puts "[SKIP] #{src}"
    end
  end
end