gritano 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,246 @@
1
+ require "terminal-table"
2
+
3
+ module Gritano
4
+ module Console
5
+ class Executor < Gritano::Console::Base
6
+
7
+ def initialize(stdin = STDIN, home_dir = Etc.getpwuid.dir, repo_path = Etc.getpwuid.dir)
8
+ @stdin = stdin
9
+ @home_dir = home_dir
10
+ @repo_path = repo_path
11
+ @ssh_path = File.join(@home_dir, '.ssh')
12
+ super(@stdin, @home_dir)
13
+ end
14
+
15
+ before_each_command do
16
+ check_git
17
+ check_gritano
18
+ ActiveRecord::Base.establish_connection(
19
+ YAML::load(File.open(File.join(@home_dir, '.gritano', 'database.yml'))))
20
+ end
21
+
22
+ add_command "user:list" do |argv|
23
+ users = User.all
24
+ msg = Terminal::Table.new do |t|
25
+ t << ['user']
26
+ t << :separator
27
+ users.each do |user|
28
+ t.add_row [user.login]
29
+ end
30
+ end
31
+ msg = "there is no user registered" if users.count == 0
32
+ return [true, msg]
33
+ end
34
+
35
+ add_command "user:key:list", "username" do |argv|
36
+ login, = argv
37
+ user = User.find_by_login(login)
38
+ if user
39
+ keys = user.keys
40
+ msg = Terminal::Table.new do |t|
41
+ t << ['keys']
42
+ t << :separator
43
+ keys.each do |key|
44
+ t.add_row [key.name]
45
+ end
46
+ end
47
+ msg = "there is no key registered" if keys.count == 0
48
+ return [true, msg]
49
+ else
50
+ return [false, "User #{login} is not registered"]
51
+ end
52
+ end
53
+
54
+ add_command "user:repo:list", "username" do |argv|
55
+ login, = argv
56
+ user = User.find_by_login(login)
57
+ if user
58
+ repos = user.repositories
59
+ msg = Terminal::Table.new do |t|
60
+ t << ['repositories']
61
+ t << :separator
62
+ repos.each do |repo|
63
+ t.add_row [repo.name]
64
+ end
65
+ end
66
+ msg = "there is no repository registered" if repos.count == 0
67
+ return [true, msg]
68
+ else
69
+ return [false, "User #{login} is not registered"]
70
+ end
71
+ end
72
+
73
+ add_command "user:add", "username" do |argv|
74
+ login, = argv
75
+ user = User.new(login: login)
76
+ return [true, "User #{login} added."] if user.save
77
+ return [false, "#{user.errors.full_messages.join(", ")}."]
78
+ end
79
+
80
+ add_command "user:rm", "username" do |argv|
81
+ login, = argv
82
+ user = User.find_by_login(login)
83
+ if user
84
+ if user.destroy
85
+ return [true, "User #{login} removed."]
86
+ end
87
+ end
88
+ return [false, "User #{login} could not be removed."]
89
+ end
90
+
91
+ add_command "user:key:add", "username keyname < key.pub" do |argv|
92
+ login, key_name, key_file = argv
93
+ user = User.find_by_login(login)
94
+ if user
95
+ key = user.keys.create(name: key_name, key: @stdin.read)
96
+ if key.valid?
97
+ File.open(File.join(@ssh_path, 'authorized_keys'), 'w').write(Key.authorized_keys)
98
+ return [true, "Key added successfully."]
99
+ end
100
+ end
101
+ return [false, "Key could not be added."]
102
+ end
103
+
104
+ add_command "user:key:rm", "username keyname" do |argv|
105
+ login, key_name = argv
106
+ key = Key.where(name: key_name).includes(:user).where("users.login" => login).limit(1)[0]
107
+ if key
108
+ if key.destroy
109
+ File.open(File.join(@ssh_path, 'authorized_keys'), 'w').write(Key.authorized_keys)
110
+ return [true, "Key removed successfully."]
111
+ end
112
+ end
113
+ return [false, "Key could not be removed."]
114
+ end
115
+
116
+ add_command "user:admin:add", "username" do |argv|
117
+ login, = argv
118
+ user = User.find_by_login(login)
119
+ if user
120
+ user.admin = true
121
+ if user.save
122
+ return [true, "Now, user #{login} is an administrator"]
123
+ end
124
+ end
125
+ return [false, "User #{login} could not be modified"]
126
+ end
127
+
128
+ add_command "user:admin:rm", "username" do |argv|
129
+ login, = argv
130
+ user = User.find_by_login(login)
131
+ if user
132
+ user.admin = false
133
+ if user.save
134
+ return [true, "Now, user #{login} is not an administrator"]
135
+ end
136
+ end
137
+ return [false, "User #{login} could not be modified"]
138
+ end
139
+
140
+ add_command "repo:list" do |argv|
141
+ repos = Repository.all
142
+ msg = Terminal::Table.new do |t|
143
+ t << ['repositories']
144
+ t << :separator
145
+ repos.each do |repo|
146
+ t.add_row [repo.name]
147
+ end
148
+ end
149
+ msg = "there is no repository registered" if repos.count == 0
150
+ return [true, msg]
151
+ end
152
+
153
+ add_command "repo:add", "reponame.git [username1 username2 ...]*" do |argv|
154
+ name, user_login = argv
155
+ repo = Repository.new(name: name, path: @repo_path)
156
+ if repo.save
157
+ if user_login
158
+ argv[1..-1].each do |login|
159
+ user = User.find_by_login(login)
160
+ if user
161
+ user.add_access(repo, :read)
162
+ user.add_access(repo, :write)
163
+ end
164
+ end
165
+ end
166
+ return [true, "Repository #{name} created successfully."]
167
+ end
168
+ return [false, "Repository #{name} could not be created."]
169
+ end
170
+
171
+ add_command "repo:user:list", "reponame.git" do |argv|
172
+ name, = argv
173
+ repo = Repository.find_by_name(name)
174
+ if repo
175
+ users = repo.users
176
+ msg = Terminal::Table.new do |t|
177
+ t << ['user', 'permission']
178
+ t << :separator
179
+ users.each do |user|
180
+ permissions = ""
181
+ user.permissions.find_by_repository_id(repo.id) do |p|
182
+ permissions += "r" if p.is(:read)
183
+ permissions += "w" if p.is(:write)
184
+ end
185
+ t.add_row [user.login, permissions]
186
+ end
187
+ end
188
+ msg = "No user have access to this repository" if users.count == 0
189
+ return [true, msg]
190
+ end
191
+ return [false, "Repository #{name} doesn't exist."]
192
+ end
193
+
194
+ add_command "repo:rm", "reponame.git" do |argv|
195
+ name, = argv
196
+ repo = Repository.find_by_name(name)
197
+ if repo
198
+ if repo.destroy
199
+ return [true, "Repository #{name} removed successfully."]
200
+ end
201
+ end
202
+ return [false, "Repository #{name} could not be removed."]
203
+ end
204
+
205
+ add_command "repo:read:add", "reponame.git username" do |argv|
206
+ repo_name, login = argv
207
+ user = User.find_by_login(login)
208
+ repo = Repository.find_by_name(repo_name)
209
+ if repo and user
210
+ return [true, "User #{login} has read access to #{repo_name}."] if user.add_access(repo, :read)
211
+ end
212
+ return [false, "An error occurred. Permissions was not modified."]
213
+ end
214
+
215
+ add_command "repo:write:add", "reponame.git username" do |argv|
216
+ repo_name, login = argv
217
+ user = User.find_by_login(login)
218
+ repo = Repository.find_by_name(repo_name)
219
+ if repo and user
220
+ return [true, "User #{login} has write access to #{repo_name}."] if user.add_access(repo, :write)
221
+ end
222
+ return [false, "An error occurred. Permissions was not modified."]
223
+ end
224
+
225
+ add_command "repo:read:rm", "reponame.git username" do |argv|
226
+ repo_name, login = argv
227
+ user = User.find_by_login(login)
228
+ repo = Repository.find_by_name(repo_name)
229
+ if repo and user
230
+ return [true, "User #{login} has not read access to #{repo_name}."] if user.remove_access(repo, :read)
231
+ end
232
+ return [false, "An error occurred. Permissions was not modified."]
233
+ end
234
+
235
+ add_command "repo:write:rm", "reponame.git username" do |argv|
236
+ repo_name, login = argv
237
+ user = User.find_by_login(login)
238
+ repo = Repository.find_by_name(repo_name)
239
+ if repo and user
240
+ return [true, "User #{login} has not write access to #{repo_name}."] if user.remove_access(repo, :write)
241
+ end
242
+ return [false, "An error occurred. Permissions was not modified."]
243
+ end
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,35 @@
1
+ module Gritano
2
+ module Console
3
+ class Gritano < Gritano::Console::Base
4
+
5
+ def initialize(stdin = STDIN, home_dir = Etc.getpwuid.dir, repo_path = Etc.getpwuid.dir)
6
+ @stdin = stdin
7
+ @home_dir = home_dir
8
+ @repo_path = repo_path
9
+ @ssh_path = File.join(@home_dir, '.ssh')
10
+ super(@stdin, @home_dir)
11
+ end
12
+
13
+ add_command "help" do |argv|
14
+ Gritano.commands = Gritano.commands.merge(Installer.commands).merge(Executor.commands)
15
+ [true, Gritano.help]
16
+ end
17
+
18
+ add_command "version" do |argv|
19
+ version = "v#{File.open(File.join(File.dirname(__FILE__), '..', '..', '..', 'VERSION')).readlines.join}"
20
+ [true, version]
21
+ end
22
+
23
+ def method_missing(meth, *args, &block)
24
+ params = [meth.to_s.gsub("_", ":")] + args[0]
25
+ begin
26
+ installer = Installer.new
27
+ installer.execute(params)
28
+ rescue
29
+ executor = Executor.new(@stdin, @home_dir, @repo_path)
30
+ executor.execute(params)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ module Gritano
2
+ module Console
3
+ class Installer < Gritano::Console::Base
4
+
5
+ def initialize(stdin = STDIN, home_dir = Etc.getpwuid.dir)
6
+ @home_dir = home_dir
7
+ @stdin = stdin
8
+ super(@stdin, @home_dir)
9
+ end
10
+
11
+ before_each_command do
12
+ check_git
13
+ end
14
+
15
+ add_command "setup:prepare" do |argv|
16
+ Dir.mkdir(File.join(@home_dir, '.gritano')) unless File.exist?(File.join(@home_dir, '.gritano'))
17
+ Dir.mkdir(File.join(@home_dir, '.ssh')) unless File.exist?(File.join(@home_dir, '.ssh'))
18
+ File.open(File.join(@home_dir, '.gritano', 'database.yml'), "w") do |f|
19
+ f.write("adapter: sqlite3\ndatabase: #{File.join(Etc.getpwuid.dir, '.gritano', 'database.db')}\n")
20
+ end
21
+ if File.exist?(File.join(@home_dir, '.gritano', 'database.db'))
22
+ FileUtils.rm(File.join(@home_dir, '.gritano', 'database.db'))
23
+ end
24
+ return [true, "configuration has been generated"]
25
+ end
26
+
27
+ add_command "setup:install" do |argv|
28
+ ActiveRecord::Base.establish_connection(
29
+ YAML::load(File.open(File.join(@home_dir, '.gritano', 'database.yml'))))
30
+ ActiveRecord::Migrator.migrate(
31
+ File.join(File.dirname(__FILE__),'..', '..', '..', 'db/migrate'),
32
+ ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
33
+ [true, "gritano has been installed"]
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,95 +1,100 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe Gritano::Console do
3
+ describe Gritano::Console::Executor do
4
4
 
5
5
  before(:each) do
6
6
  stdin = double()
7
7
  stdin.stub(:read).and_return("Your SSHKEY here...")
8
- @console = Gritano::Console.new(stdin)
8
+ @console = Gritano::Console::Executor.new(stdin)
9
9
  end
10
10
 
11
11
  it "should respond to gritano user:add igorbonadio" do
12
12
  @console.should_receive(:user_add)
13
- @console.execute("user:add igorbonadio".split(' '))
13
+ @console.execute_without_filters("user:add igorbonadio".split(' '))
14
14
  end
15
15
 
16
16
  it "should respond to gritano user:rm igorbonadio" do
17
17
  @console.should_receive(:user_rm)
18
- @console.execute("user:rm igorbonadio".split(' '))
18
+ @console.execute_without_filters("user:rm igorbonadio".split(' '))
19
19
  end
20
20
 
21
21
  it "should respond to gritano user:key:add username keyname < key.pub" do
22
22
  @console.should_receive(:user_key_add)
23
- @console.execute("user:key:add igorbonadio keyname".split(' '))
23
+ @console.execute_without_filters("user:key:add igorbonadio keyname".split(' '))
24
24
  end
25
25
 
26
26
  it "should respond to gritano user:key:rm username keyname" do
27
27
  @console.should_receive(:user_key_rm)
28
- @console.execute("user:key:rm username keyname".split(' '))
28
+ @console.execute_without_filters("user:key:rm username keyname".split(' '))
29
29
  end
30
30
 
31
31
  it "should respond to gritano user:list" do
32
32
  @console.should_receive(:user_list)
33
- @console.execute("user:list".split(' '))
33
+ @console.execute_without_filters("user:list".split(' '))
34
34
  end
35
35
 
36
36
  it "should respond to gritano user:key:list username" do
37
37
  @console.should_receive(:user_key_list)
38
- @console.execute("user:key:list username".split(' '))
38
+ @console.execute_without_filters("user:key:list username".split(' '))
39
39
  end
40
40
 
41
41
  it "should respond to gritano user:repo:list username" do
42
42
  @console.should_receive(:user_repo_list)
43
- @console.execute("user:repo:list username".split(' '))
43
+ @console.execute_without_filters("user:repo:list username".split(' '))
44
44
  end
45
45
 
46
46
  it "should respond to gritano repo:user:list reponame.git" do
47
47
  @console.should_receive(:repo_user_list)
48
- @console.execute("repo:user:list reponame.git".split(' '))
48
+ @console.execute_without_filters("repo:user:list reponame.git".split(' '))
49
49
  end
50
50
 
51
51
  it "should respond to gritano user:admin:add igorbonadio" do
52
52
  @console.should_receive(:user_admin_add)
53
- @console.execute("user:admin:add igorbonadio".split(' '))
53
+ @console.execute_without_filters("user:admin:add igorbonadio".split(' '))
54
54
  end
55
55
 
56
56
  it "should respond to gritano user:admin:rm igorbonadio" do
57
57
  @console.should_receive(:user_admin_rm)
58
- @console.execute("user:admin:rm igorbonadio".split(' '))
58
+ @console.execute_without_filters("user:admin:rm igorbonadio".split(' '))
59
59
  end
60
60
 
61
61
  it "should respond to gritano repo:add tmp/reponame.git" do
62
62
  @console.should_receive(:repo_add)
63
- @console.execute("repo:add tmp/reponame.git".split(' '))
63
+ @console.execute_without_filters("repo:add tmp/reponame.git".split(' '))
64
64
  end
65
65
 
66
66
  it "should respond to gritano repo:rm tmp/reponame.git" do
67
67
  @console.should_receive(:repo_rm)
68
- @console.execute("repo:rm tmp/reponame.git".split(' '))
68
+ @console.execute_without_filters("repo:rm tmp/reponame.git".split(' '))
69
69
  end
70
70
 
71
71
  it "should respond to gritano repo:read:add igorbonadio tmp/reponame.git" do
72
72
  @console.should_receive(:repo_read_add)
73
- @console.execute("repo:read:add igorbonadio tmp/reponame.git".split(' '))
73
+ @console.execute_without_filters("repo:read:add igorbonadio tmp/reponame.git".split(' '))
74
74
  end
75
75
 
76
76
  it "should respond to gritano repo:write:add igorbonadio tmp/reponame.git" do
77
77
  @console.should_receive(:repo_write_add)
78
- @console.execute("repo:write:add igorbonadio tmp/reponame.git".split(' '))
78
+ @console.execute_without_filters("repo:write:add igorbonadio tmp/reponame.git".split(' '))
79
79
  end
80
80
 
81
81
  it "should respond to gritano repo:read:rm igorbonadio tmp/reponame.git" do
82
82
  @console.should_receive(:repo_read_rm)
83
- @console.execute("repo:read:rm igorbonadio tmp/reponame.git".split(' '))
83
+ @console.execute_without_filters("repo:read:rm igorbonadio tmp/reponame.git".split(' '))
84
84
  end
85
85
 
86
86
  it "should respond to gritano repo:write:rm igorbonadio tmp/reponame.git" do
87
87
  @console.should_receive(:repo_write_rm)
88
- @console.execute("repo:write:rm igorbonadio tmp/reponame.git".split(' '))
88
+ @console.execute_without_filters("repo:write:rm igorbonadio tmp/reponame.git".split(' '))
89
89
  end
90
90
 
91
91
  it "should respond to gritano repo:list" do
92
92
  @console.should_receive(:repo_list)
93
- @console.execute("repo:list".split(' '))
93
+ @console.execute_without_filters("repo:list".split(' '))
94
+ end
95
+
96
+ it "should respond to gritano help" do
97
+ @console.should_receive(:help)
98
+ @console.execute_without_filters("help".split(' '))
94
99
  end
95
100
  end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Gritano::Console::Gritano do
4
+
5
+ before(:each) do
6
+ stdin = double()
7
+ stdin.stub(:read).and_return("Your SSHKEY here...")
8
+ @console = Gritano::Console::Gritano.new(stdin)
9
+ end
10
+
11
+ it "should respond to gritano version" do
12
+ @console.should_receive(:version)
13
+ @console.execute("version".split(' '))
14
+ end
15
+
16
+ it "should respond to gritano help" do
17
+ @console.should_receive(:help)
18
+ @console.execute("help".split(' '))
19
+ end
20
+ end