Class: GithubCreate

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

Class Method Summary collapse

Class Method Details

.addRemote(remoteName, remoteUrl) ⇒ Object



92
93
94
# File 'lib/github-create.rb', line 92

def self.addRemote(remoteName, remoteUrl)
  system("git remote add " << remoteName << " " << remoteUrl)
end

.checkIfLocalRepoExistsObject



64
65
66
# File 'lib/github-create.rb', line 64

def self.checkIfLocalRepoExists
  system("git status -s > /dev/null 2>&1")
end

.checkIfRemoteExists(remote) ⇒ Object



88
89
90
# File 'lib/github-create.rb', line 88

def self.checkIfRemoteExists(remote)
  return system("git remote show "<< remote << "> /dev/null 2>&1")
end

.configFilePathObject



134
135
136
137
# File 'lib/github-create.rb', line 134

def self.configFilePath
  userDir = Etc.getpwuid.dir
  filePath = userDir << "/" << ".github-create"
end

.createConfigFileObject



125
126
127
128
129
130
131
132
# File 'lib/github-create.rb', line 125

def self.createConfigFile
  f = File.open configFilePath, "w+"
  print "Enter your github username: "
  username = gets.chomp
  f << username
  f.close
  return username
end

.createLocalRepoObject



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

def self.createLocalRepo
  system("git init")
  puts "Create local repo: git init"
end

.createRepo(repo, access, pw) ⇒ Object



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

def self.createRepo(repo, access, pw)
  username = readCredentialsFromFile
  result = makeCreateRequest username, pw, repo, access

  if result.has_key?('error')
    puts "Error: " << result["error"]
    return nil
  else
    puts "Repository is at " << result["repository"]["url"]
    return result["repository"]["url"]
  end
  
end

.getCredentialsObject

this returns the password for use in the other methods



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/github-create.rb', line 101

def self.getCredentials
  username = readCredentialsFromFile
  if username.nil?
    username = createConfigFile
  end
  print "Github password for " << username << ": "

  system "stty -echo"
  pw = gets.chomp
  system "stty echo"
  puts
  return pw
end

.getRemoteUrl(repo) ⇒ Object



96
97
98
# File 'lib/github-create.rb', line 96

def self.getRemoteUrl(repo)
  # TODO. not required right now
end

.makeCreateRequest(username, pw, repoName, access) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/github-create.rb', line 139

def self.makeCreateRequest(username, pw, repoName, access)
  if access == :public
    accessKey = 1
  else
    accessKey = 0
  end

  options = {
    :query=>{
      :name=>repoName,
      :public=>accessKey
    }
  }

  basic_auth username, pw
  self.post('http://github.com/api/v2/json/repos/create', options)
end

.readCredentialsFromFileObject

only stores the github username



116
117
118
119
120
121
122
123
# File 'lib/github-create.rb', line 116

def self.readCredentialsFromFile
  if File.exists? configFilePath
    username = File.open(configFilePath, "r").readlines.join ""
    return username
  else
    return nil
  end
end

.resetCredentialsObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/github-create.rb', line 9

def self.resetCredentials
  if File.exists? configFilePath
    begin
      File.delete configFilePath
      puts "Username in $HOME/.github-create cleared"
    rescue
      puts "Oops! could not config delete file"
    end
  end
end

.setupRemote(remoteName, remoteUrl) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/github-create.rb', line 68

def self.setupRemote(remoteName, remoteUrl)
  # check for remote and create it
  if checkIfRemoteExists(remoteName)
    if remoteName == "origin"
      remoteName = "github"
      addRemote(remoteName, remoteUrl) unless checkIfRemoteExists(remoteName)
    else
      puts "Seems like remote with that name already exists!"
      puts "Here's the remote url of the repo to add it yourself: " << remoteUrl
      return false
    end
  else
    addRemote(remoteName, remoteUrl)
  end
    
  # control flow reaches here only if remote is added, so display msg
  puts "Added remote " << remoteName << " with url " << remoteUrl
  return true
end

.setupRepo(repo, remote, access) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/github-create.rb', line 20

def self.setupRepo(repo, remote, access)
  createLocalRepo unless checkIfLocalRepoExists

  pw = getCredentials

  # if cannot create repo return
  repoUrl = createRepo(repo, access, pw)
  if repoUrl.nil?
    puts "Oops! couldn't create repo"
    return
  end

  remoteUrl = repoUrl.sub("https://", "git@").sub("/",":") << ".git"

  # remote url of the repo
  # remoteUrl = getRemoteUrl(repo, pw)

  # setup remote in local repo
  unless setupRemote(remote, remoteUrl)
    return
  end
  
end