Class: GithubCreate
Class Method Summary collapse
- .addRemote(remoteName, remoteUrl) ⇒ Object
- .checkIfLocalRepoExists ⇒ Object
- .checkIfRemoteExists(remote) ⇒ Object
- .configFilePath ⇒ Object
- .createConfigFile ⇒ Object
- .createLocalRepo ⇒ Object
- .createRepo(repo, access, pw) ⇒ Object
-
.getCredentials ⇒ Object
this returns the password for use in the other methods.
- .getRemoteUrl(repo) ⇒ Object
- .makeCreateRequest(username, pw, repoName, access) ⇒ Object
-
.readCredentialsFromFile ⇒ Object
only stores the github username.
- .resetCredentials ⇒ Object
- .setupRemote(remoteName, remoteUrl) ⇒ Object
- .setupRepo(repo, remote, access) ⇒ Object
Class Method Details
.addRemote(remoteName, remoteUrl) ⇒ Object
94 95 96 97 |
# File 'lib/github-create.rb', line 94 def self.addRemote(remoteName, remoteUrl) return true if system("git remote add " << remoteName << " " << remoteUrl) return false end |
.checkIfLocalRepoExists ⇒ Object
64 65 66 67 |
# File 'lib/github-create.rb', line 64 def self.checkIfLocalRepoExists return false if `git status -s`.length == 0 return true end |
.checkIfRemoteExists(remote) ⇒ Object
89 90 91 92 |
# File 'lib/github-create.rb', line 89 def self.checkIfRemoteExists(remote) return true if system("git remote show "<< remote) return false end |
.configFilePath ⇒ Object
132 133 134 135 |
# File 'lib/github-create.rb', line 132 def self.configFilePath userDir = Etc.getpwuid.dir filePath = userDir << "/" << ".github-create" end |
.createConfigFile ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/github-create.rb', line 123 def self.createConfigFile f = File.open configFilePath, "w+" print "Enter your github username: " username = gets.chomp f << username f.close return username end |
.createLocalRepo ⇒ Object
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 |
.getCredentials ⇒ Object
this returns the password for use in the other methods
104 105 106 107 108 109 110 111 |
# File 'lib/github-create.rb', line 104 def self.getCredentials username = readCredentialsFromFile if username.nil? username = createConfigFile end print "Password for " << username << ": " return gets.chomp end |
.getRemoteUrl(repo) ⇒ Object
99 100 101 |
# File 'lib/github-create.rb', line 99 def self.getRemoteUrl(repo) # TODO end |
.makeCreateRequest(username, pw, repoName, access) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/github-create.rb', line 137 def self.makeCreateRequest(username, pw, repoName, access) if access == :public accessKey = 1 else accessKey = 0 end = { :query=>{ :name=>repoName, :public=>accessKey } } basic_auth username, pw self.post('http://github.com/api/v2/json/repos/create', ) end |
.readCredentialsFromFile ⇒ Object
only stores the github username
114 115 116 117 118 119 120 121 |
# File 'lib/github-create.rb', line 114 def self.readCredentialsFromFile if File.exists? configFilePath username = File.open(configFilePath, "r").readlines.join "" return username else return nil end end |
.resetCredentials ⇒ Object
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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/github-create.rb', line 69 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 |