github-create 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in github-create.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/bin/ghcreate ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path '../lib/github-create', File.dirname(__FILE__)
3
+ require 'optparse'
4
+
5
+ access = :public
6
+ repoName = nil
7
+ remoteName = "origin"
8
+
9
+ OptionParser.new do |option|
10
+ option.banner = "github-create - to help you create github repos for your projects"
11
+
12
+ option.on("-p", "--private", "create private repo") do
13
+ access = :private
14
+ end
15
+
16
+ option.on("--clear", "--clear", "clear github username, stored in $HOME/.github-create") do
17
+ GithubCreate.resetCredentials
18
+ exit
19
+ end
20
+
21
+ option.on("-c NAME", "--c NAME", "specifying the name of the repo to create") do |n|
22
+ repoName = n
23
+ end
24
+
25
+ option.on("-remote NAME", "--remote NAME", "set the origin name. If not specified origin or github is used") do |name|
26
+ remoteName = name
27
+ end
28
+
29
+ end.parse!
30
+
31
+ # parse the t hash to check if there are any repos to be created
32
+ unless repoName.nil?
33
+ puts "repo: " << repoName
34
+ puts "remote: " << remoteName
35
+ GithubCreate.setupRepo repoName, remoteName, access
36
+ end
@@ -0,0 +1,21 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require "github-create/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "github-create"
7
+ s.version = Github::Create::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Akash Manohar"]
10
+ s.email = ["akash@akash.im"]
11
+ s.homepage = "http://akash.im"
12
+ s.summary = "github-create v0.1"
13
+ s.description = "github-create helps you setup github repositories for your projects from command-line"
14
+
15
+ s.rubyforge_project = "github-create"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,5 @@
1
+ module Github
2
+ module Create
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,153 @@
1
+ require 'etc'
2
+ require 'httparty'
3
+
4
+ class GithubCreate
5
+
6
+ include HTTParty
7
+ base_uri = "http://github.com/api/v2/json"
8
+
9
+ def self.resetCredentials
10
+ if File.exists? configFilePath
11
+ begin
12
+ File.delete configFilePath
13
+ puts "Username in $HOME/.github-create cleared"
14
+ rescue
15
+ puts "Oops! could not delete file"
16
+ end
17
+ end
18
+ end
19
+
20
+ def self.setupRepo(repo, remote, access)
21
+ createLocalRepo unless checkIfLocalRepoExists
22
+
23
+ pw = getCredentials
24
+
25
+ # if cannot create repo return
26
+ unless createRepo(remote, access, pw)
27
+ return
28
+ end
29
+
30
+ return
31
+ # get remote url of the repo
32
+ remoteUrl = getRemoteUrl(repo, pw)
33
+
34
+ # if remoteUrl is nil return
35
+ if remoteUrl.nil?
36
+ puts "something went wrong..."
37
+ return
38
+ end
39
+
40
+ # setup remote in local repo
41
+ unless setupRemote
42
+ return
43
+ end
44
+
45
+ puts "setup done"
46
+ end
47
+
48
+
49
+ def self.createRepo(repo, access, pw)
50
+ username = readCredentialsFromFile
51
+ result = makeCreateRequest username, pw, repo, access
52
+ puts result
53
+ puts result.class
54
+ end
55
+
56
+ def self.createLocalRepo
57
+ system('git init')
58
+ puts "Create local repo: git init"
59
+ end
60
+
61
+ def self.checkIfLocalRepoExists
62
+ return false if `git status -s`.length == 0
63
+ return true
64
+ end
65
+
66
+ def self.setupRemote(remoteName, remoteUrl)
67
+ # check for remote and create it
68
+ if checkRemoteExists(remoteName)
69
+ if remoteName == "origin"
70
+ remoteName = "github"
71
+ addRemote(remoteName, remoteUrl) unless checkRemoteExists(remoteName)
72
+ else
73
+ puts "Seems like remote with that name already exists!"
74
+ puts "Here's the remote url of the repo to add it yourself: " << remoteUrl
75
+ return false
76
+ end
77
+ else
78
+ addRemote(remoteName, remoteUrl)
79
+ end
80
+
81
+ # control flow reaches here only if remote is added, so display msg
82
+ puts "Added remote " << remoteName << " with url " << remoteUrl
83
+ return true
84
+ end
85
+
86
+ def self.checkIfRemoteExists(remote)
87
+ return true if system("git remote show "<< remote)
88
+ return false
89
+ end
90
+
91
+ def self.addRemote(remoteName, remoteUrl)
92
+ return true if system("git remote add " << remoteName << " " << remoteUrl)
93
+ return false
94
+ end
95
+
96
+ def self.getRemoteUrl(repo)
97
+ # TODO
98
+ end
99
+
100
+ # this returns the password for use in the other methods
101
+ def self.getCredentials
102
+ username = readCredentialsFromFile
103
+ if username.nil?
104
+ username = createConfigFile
105
+ end
106
+ print "Password for " << username << ": "
107
+ return gets.chomp
108
+ end
109
+
110
+ # only stores the github username
111
+ def self.readCredentialsFromFile
112
+ if File.exists? configFilePath
113
+ username = File.open(configFilePath, "r").readlines.join ""
114
+ return username
115
+ else
116
+ return nil
117
+ end
118
+ end
119
+
120
+ def self.createConfigFile
121
+ f = File.open configFilePath, "w+"
122
+ print "Enter your github username: "
123
+ username = gets.chomp
124
+ f << username
125
+ f.close
126
+ return username
127
+ end
128
+
129
+ def self.configFilePath
130
+ userDir = Etc.getpwuid.dir
131
+ filePath = userDir << "/" << ".github-create"
132
+ end
133
+
134
+ def self.makeCreateRequest(username, pw, repoName, access)
135
+ if access == :public
136
+ accessKey = 1
137
+ else
138
+ accessKey = 0
139
+ end
140
+
141
+ options = {
142
+ :query=>{
143
+ :name=>repoName,
144
+ :public=>accessKey
145
+ }
146
+ }
147
+
148
+ basic_auth username, pw
149
+
150
+ self.post('http://github.com/api/v2/json/repos/create', options)
151
+ end
152
+
153
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github-create
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Akash Manohar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-28 00:00:00 +05:30
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: github-create helps you setup github repositories for your projects from command-line
18
+ email:
19
+ - akash@akash.im
20
+ executables:
21
+ - ghcreate
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - Gemfile
29
+ - Rakefile
30
+ - bin/ghcreate
31
+ - github-create.gemspec
32
+ - lib/github-create.rb
33
+ - lib/github-create/version.rb
34
+ has_rdoc: true
35
+ homepage: http://akash.im
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ requirements: []
56
+
57
+ rubyforge_project: github-create
58
+ rubygems_version: 1.5.0
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: github-create v0.1
62
+ test_files: []
63
+