Class: CreateRepo::Github

Inherits:
Object
  • Object
show all
Defined in:
lib/create-repo/github.rb

Instance Method Summary collapse

Constructor Details

#initializeGithub

Returns a new instance of Github.



5
6
7
# File 'lib/create-repo/github.rb', line 5

def initialize
  setup  
end

Instance Method Details

#create_repositoryObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/create-repo/github.rb', line 47

def create_repository
  @repo = @client.create_repository(@repo_name,@options)
  puts `echo "\033[0;32mRepository created!\033[0m"` if @repo
  puts `sudo git init`
  puts `sudo git remote add origin #{@repo[:html_url]}.git` 
  puts `sudo git add --all`
  puts `sudo git commit -m "Set up remote repository"`
  puts `sudo git push -u origin master`
  puts `echo "\033[0;32mAll set now!\033[0m"`
end

#get_repo_infoObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/create-repo/github.rb', line 31

def get_repo_info
  print "Repository Name (default: #{cwd=Pathname.new(Dir.getwd).basename.to_s}) : "
  @repo_name = !(name=STDIN.gets.chomp).empty? ? name : cwd
  if repository_exists?
    puts `echo "\033[1;31mRepository already exists! Choose a different name\033[0m"`
    get_repo_info
  end
  puts "Description: (Press enter to skip)"
  @repo_desc = STDIN.gets.chomp
  puts "Is this a private repository? (y) or (n)"
  @isPrivate = ['y','Y'].include?(STDIN.gets.chomp)
  @options = {}
  @options['description'] = @repo_desc  if !@repo_desc.empty?
  @options['private'] = 'true' if @isPrivate
end

#loginObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/create-repo/github.rb', line 16

def 
  puts "\nLogging in..."
  begin
    @client = Octokit::Client.new \
      :login    => "#{@username}",
      :password => "#{@password}" ; nil
    user = @client.user
    user. 
  rescue
    puts `echo "\033[1;31mLogin failed! Try again\033[0m"`
    setup
    
  end
end

#repository_exists?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/create-repo/github.rb', line 58

def repository_exists?
  repo = @client.repository(@username+"/"+@repo_name); nil
  true
  rescue
    false
end

#setupObject



9
10
11
12
13
14
# File 'lib/create-repo/github.rb', line 9

def setup
  print "Github Username: "
  @username = STDIN.gets.chomp
  print "Github Password: "
  @password = STDIN.noecho(&:gets).chomp
end