Class: LearnDuplicate

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

Constant Summary collapse

GITHUB_ORG =
'https://api.github.com/repos/learn-co-curriculum/'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ LearnDuplicate

Returns a new instance of LearnDuplicate.



8
9
10
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
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
# File 'lib/learn_duplicate.rb', line 8

def initialize(opts={})
  # For non-interactive mode
  if opts[:ni]
    validate_repo = ->(url) do
      encoded_url = URI.encode(url).slice(0, url.length)
      check_existing = Faraday.get URI.parse(encoded_url)
      if check_existing.body.include? '"Not Found"'
        raise IOError, "Could not connect to #{url}"
      end
      url
    end

    @old_repo = validate_repo.call(GITHUB_ORG + opts[:source_name])

    if opts[:destination].length >= 100
      raise ArgumentError, 'Repository names must be shorter than 100 characters'
    end

    @repo_name = opts[:destination]

    if (!opts[:dryrun])
      begin
        create_new_repo
        puts ''
        puts 'To access local folder, change directory into ' + @repo_name + '/'
        puts "Repository available at #{GITHUB_ORG}" + @repo_name
      rescue => e
        STDERR.puts(e.message)
      end
    else
      puts "DRY RUN: Would execute copy of: #{@old_repo} to #{@repo_name}"
    end

    exit
  end

  @repo_name = ''
  @old_repo = ''

  puts 'Note: You must have write access to the learn-co-curriculum org on GitHub to use this tool'

  loop do
    puts 'What is the name of the repository you would like to copy? Paste exactly as is shown in the URL (i.e. advanced-hashes-hashketball)'
    @old_repo = gets.strip

    url = GITHUB_ORG + @old_repo
    encoded_url = URI.encode(url).slice(0, url.length)
    check_existing = Faraday.get URI.parse(encoded_url)

    # careful - will hit rate limit on GitHub if abused
    if check_existing.body.include? '"Not Found"'
      puts 'Provided repository name is not a valid learn-co-curriculum repository. Please try again.'
    else
      break
    end
  end

  loop do
    puts ''
    puts 'Old repository: ' + @old_repo
    puts 'What is the name of the repository you would like to create?'

    @repo_name = gets.strip.gsub(/\s+/, '-').downcase
    if @repo_name.length >= 100
      puts 'Repository names must be shorter than 100 characters'
    else
      url = GITHUB_ORG + @repo_name
      encoded_url = URI.encode(url).slice(0, url.length)
      check_existing = Faraday.get URI.parse(encoded_url)


      if check_existing.body.include? '"Not Found"'
        break
      else
        puts 'A repository with that name already exists or you may have hit a rate limit'
        puts GITHUB_ORG + @repo_name
        puts ''
      end
    end
  end

  create_new_repo
  puts ''
  puts 'To access local folder, change directory into ' + @repo_name + '/'
  puts "Repository available at #{GITHUB_ORG}" + @repo_name
end