Class: Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInterface

Returns a new instance of Interface.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/interface.rb', line 30

def initialize
  @sysbh=Sys.new()
  @repos_list=[]; @orgs_repos=[]; @teams_repos=[]

  options=@sysbh.parse

  trap("SIGINT") { throw :ctrl_c }
  catch :ctrl_c do
    begin
      if options[:user]==nil && options[:token]==nil &&  options[:path]!=nil
        self.run(options[:path],options[:token],options[:user])
      else
        self.run("#{ENV['HOME']}/.ghedsh",options[:token],options[:user])
      end
    rescue SystemExit, Interrupt
      raise
    rescue Exception => e
      puts "exit"
    #  puts e
    end
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



25
26
27
# File 'lib/interface.rb', line 25

def client
  @client
end

#configObject

Returns the value of attribute config.



24
25
26
# File 'lib/interface.rb', line 24

def config
  @config
end

#deepObject

Returns the value of attribute deep.



26
27
28
# File 'lib/interface.rb', line 26

def deep
  @deep
end

#memoryObject

Returns the value of attribute memory.



27
28
29
# File 'lib/interface.rb', line 27

def memory
  @memory
end

#optionObject (readonly)

Returns the value of attribute option.



23
24
25
# File 'lib/interface.rb', line 23

def option
  @option
end

#orgs_listObject (readonly)

Returns the value of attribute orgs_list.



28
29
30
# File 'lib/interface.rb', line 28

def orgs_list
  @orgs_list
end

#orgs_reposObject (readonly)

Returns the value of attribute orgs_repos.



28
29
30
# File 'lib/interface.rb', line 28

def orgs_repos
  @orgs_repos
end

#repos_listObject (readonly)

Returns the value of attribute repos_list.



28
29
30
# File 'lib/interface.rb', line 28

def repos_list
  @repos_list
end

#sysbhObject (readonly)

Returns the value of attribute sysbh.



23
24
25
# File 'lib/interface.rb', line 23

def sysbh
  @sysbh
end

#teamlistObject (readonly)

Returns the value of attribute teamlist.



28
29
30
# File 'lib/interface.rb', line 28

def teamlist
  @teamlist
end

#teams_reposObject (readonly)

Returns the value of attribute teams_repos.



28
29
30
# File 'lib/interface.rb', line 28

def teams_repos
  @teams_repos
end

Instance Method Details

#cd(path) ⇒ Object

Go to the path, depends with the scope if you are in user scope, first searchs Orgs then Repos, etc.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/interface.rb', line 112

def cd(path)
  case
  when @deep==USER
    @orgs_list=Organizations.new.read_orgs(@client)
    aux=@orgs_list
    if aux.one?{|aux| aux==path}
      @config["Org"]=path
      @teamlist=Teams.new.read_teamlist(@client,@config)
      @sysbh.add_history_str(1,@teamlist)
      @deep=2
    else
      puts "\nNo organization is available with that name"
      self.set(path)
    end
  when @deep == ORGS
    aux=@teamlist
    if aux[path]!=nil
      @config["Team"]=path
      @config["TeamID"]=@teamlist[path]
      @deep=TEAM
    else
      puts "\nNo team is available with that name"
      self.set(path)
    end
  when @deep == TEAM
    self.set(path)
  end
end

#cdback(returnall) ⇒ Object

Go back to any level



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/interface.rb', line 81

def cdback(returnall)
  if returnall!=true
    case
      when @deep == ORGS
        @config["Org"]=nil
        @deep=1
        @orgs_repos=[]
      when @deep == ORGS_REPO
        @config["Repo"]=nil
        @deep=2
      when @deep == USER_REPO
        @config["Repo"]=nil
        @deep=1
      when @deep == TEAM
        @config["Team"]=nil
        @config["TeamID"]=nil
        @teams_repos=[]
        @deep=2
    end
  else
    @config["Org"]=nil
    @config["Repo"]=nil
    @config["Team"]=nil
    @config["TeamID"]=nil
    @deep=1
    @orgs_repos=[]; @teams_repos=[]
  end
end

#collaboratorsObject



255
256
257
258
259
260
# File 'lib/interface.rb', line 255

def collaborators()
  case
  when @deep==ORGS_REPO
    Repositories.show_collaborators(@client,@config,1)
  end
end

#commitsObject



237
238
239
240
241
242
243
244
245
246
# File 'lib/interface.rb', line 237

def commits()
  c=Repositories.new
  case
  when @deep==ORGS_REPO
    c.show_commits(@client,@config,1)
  when @deep==USER_REPO
    c.show_commits(@client,@config,2)
  end
  print "\n"
end

#get_teamlist(data) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/interface.rb', line 229

def get_teamlist(data)
  list=Array.new
  for i in 0..data.size-1
    list.push(@teamlist[data[i]])
  end
  return list
end

#helpObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/interface.rb', line 64

def help()
  h=HelpM.new()
  case
    when @deep == USER
      h.user()
    when @deep == ORGS
      h.org()
    when @deep == ORGS_REPO
      h.org_repo()
    when @deep == USER_REPO
      h.user_repo()
    when @deep == TEAM
      h.orgs_teams()
  end
end

#orgsObject



183
184
185
186
187
188
# File 'lib/interface.rb', line 183

def orgs()
  case
  when @deep==USER
    @sysbh.add_history_str(2,Organizations.new.show_orgs(@client,@config))
  end
end

#peopleObject



190
191
192
193
194
195
196
197
# File 'lib/interface.rb', line 190

def people()
  case
  when @deep==ORGS
    @sysbh.add_history_str(2,Organizations.new.show_organization_members_bs(@client,@config))
  when @deep==TEAM
    @sysbh.add_history_str(2,Teams.new.show_team_members_bs(@client,@config))
  end
end

#promptObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/interface.rb', line 53

def prompt()
  case
    when @deep == USER then return @config["User"]+"> "
    when @deep == USER_REPO then return @config["User"]+">"+ "\e[31m#{@config["Repo"]}\e[0m"+"> "
    when @deep == ORGS then return @config["User"]+">"+ "\e[34m#{@config["Org"]}\e[0m"+"> "
    when @deep == TEAM then return @config["User"]+">"+"\e[34m#{@config["Org"]}\e[0m"+">"+@config["Team"]+"> "
    when @deep == TEAM_REPO then return @config["User"]+">"+"\e[34m#{@config["Org"]}\e[0m"+">"+@config["Team"]+">"+"\e[31m#{@config["Repo"]}\e[0m"+"> "
    when @deep == ORGS_REPO then return @config["User"]+">"+"\e[34m#{@config["Org"]}\e[0m"+">"+"\e[31m#{@config["Repo"]}\e[0m"+"> "
  end
end

#reposObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/interface.rb', line 199

def repos()
  repo=Repositories.new()
  case
    when @deep == USER
      if @repos_list.empty?
        list=repo.show_repos(@client,@config,USER,nil)
        @sysbh.add_history_str(2,list)
        @repos_list=list
      else
        @sysbh.showcachelist(@repos_list,nil)
      end
    when @deep ==ORGS
      if @orgs_repos.empty?
        list=repo.show_repos(@client,@config,ORGS,nil)
        @sysbh.add_history_str(2,list)
        @orgs_repos=list
      else
        @sysbh.showcachelist(@orgs_repos,nil)
      end
    when @deep==TEAM
      if @teams_repos.empty?
        list=repo.show_repos(@client,@config,TEAM,nil)
        @sysbh.add_history_str(2,list)
        @teams_repos=list
      else
        @sysbh.showcachelist(@teams_repos,nil)
      end
  end
end

#run(config_path, argv_token, user) ⇒ Object

Main program



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/interface.rb', line 263

def run(config_path, argv_token,user)
  ex=1

  @sysbh.write_initial_memory()
  HelpM.new.welcome()

  t=Teams.new
  r=Repositories.new
  s=Sys.new
  # orden de búsqueda: ~/.ghedsh.json ./ghedsh.json ENV["ghedsh"] --configpath path/to/file.json

  #control de carga de parametros en el logueo de la aplicacion
  if user!=nil
    @config=s.load_config_user(config_path,user)
    @client=s.client
    if @config==nil
      ex=0
    end
  else
    @config=s.load_config(config_path,argv_token)
    @client=s.client
  end

  @deep=USER
  if @client!=nil
    @sysbh.add_history_str(2,Organizations.new.read_orgs(@client))
  end

  while ex != 0

    op=Readline.readline(self.prompt,true)
    opcd=op.split
    case
      when op == "exit" then ex=0
        s.save_cache(config_path,@config)
      when op == "help" then self.help()
      when op == "orgs" then self.orgs()
      when op == "cd .." then self.cdback(false)
      when op == "people" then self.people()
      when op == "teams" #then self.teams()
        if @deep==ORGS
          t.show_teams_bs(@client,@config)
        end
      when op == "commits" then self.commits()
      when op == "col" then self.collaborators()
      when op == "forks" then self.show_forks()
    end

    if opcd[0]=="cd" and opcd[1]!=".."
      if opcd[1]=="/"
        self.cdback(true)
      else
        if opcd[1]=="repo" and opcd.size>2
          self.set(opcd[2])
        else
          self.cd(opcd[1])
        end
      end
    end
    if opcd[0]=="set"
      self.set(opcd[1])
    end
    if opcd[0]=="repos" and opcd.size==1
      self.repos()
    end
    if opcd[0]=="repos" and opcd.size>1         ##Busca con expresion regular, si no esta en la cache realiza la consulta
      case
      when @deep==USER
        if @repos_list.empty?
          r.show_repos(@client,@config,@deep,opcd[1])
          @repos_list=r.get_repos_list(@client,@config,@deep)
        else
          @sysbh.showcachelist(@repos_list,opcd[1])
        end
      when @deep==ORGS
        if @orgs_repos.empty?
          r.show_repos(@client,@config,@deep,opcd[1])
          @orgs_repos=r.get_repos_list(@client,@config,@deep)
        else
          @sysbh.showcachelist(@orgs_repos,opcd[1])
        end
      when @deep==TEAM
        if @teams_repos.empty?
          r.show_repos(@client,@config,@deep,opcd[1])
          @teams_repos=r.get_repos_list(@client,@config,@deep)
        else
          @sysbh.showcachelist(@teams_repos,opcd[1])
        end
      end
    end
    if opcd[0]=="add_team_member"
      t.add_to_team(@client,@config,opcd[1])
    end
    if opcd[0]=="new_team" and opcd.size==2
      t.create_team(@client,@config,opcd[1])
      @teamlist=t.read_teamlist(@client,@config)
      @sysbh.add_history_str(1,@teamlist)
    end
    if opcd[0]=="rm_team"
      t.delete_team(@client,@teamlist[opcd[1]])
      self.quit_history(@teamlist[opcd[1]])
      @teamlist=t.read_teamlist(@client,@config)
      @sysbh.add_history_str(1,@teamlist)
    end
    if opcd[0]=="new_team" and opcd.size>2
      t.create_team_with_members(@client,@config,opcd[1],opcd[2..opcd.size])
      @teamlist=t.read_teamlist(@client,@config)
      @sysbh.add_history_str(1,@teamlist)
    end
    if opcd[0]=="new_repository" and opcd.size==2
      r.create_repository(@client,@config,opcd[1],@deep)
    end
    if opcd[0]=="new_assignment" and opcd.size>2
      case
      when @deep==ORGS
        r.create_repository_by_teamlist(@client,@config,opcd[1],opcd[2,opcd.size],self.get_teamlist(opcd[2,opcd.size]))
      end
    end

    if opcd[0]=="clone" and opcd.size==2
      r.clone_repo(@client,@config,opcd[1],@deep)
    end
    if op.match(/^!/)
      op=op.split("!")
      s.execute_bash(op[1])
    end
    if opcd[0]=="clone" and opcd.size>2
        #r.clone_repo(@client,@config,opcd[1])
    end
  end

end

#set(path) ⇒ Object

set in the given path repository, first search in the list, then do the github query if list is empty



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/interface.rb', line 142

def set(path)
  reposlist=Repositories.new()

  case
  when @deep==USER
    @config["Repo"]=path
    if @repos_list.empty? == false
      reposlist=@repos_list
    else
      reposlist=reposlist.get_repos_list(@client,@config,@deep)
    end
    if reposlist.one?{|aux| aux==path}
        @deep=USER_REPO
        puts "Set in #{@config["User"]} repository: #{path}\n\n"
    end
  when @deep==ORGS
    @config["Repo"]=path
    if @orgs_repos.empty? == false
      reposlist=@orgs_repos
    else
      reposlist=reposlist.get_repos_list(@client,@config,@deep)
    end
    if reposlist.one?{|aux| aux==path}
      @deep=ORGS_REPO
      puts "Set in #{@config["Org"]} repository: #{path}\n\n"
    end
  when @deep==TEAM
    @config["Repo"]=path
    if @teams_repos.empty? == false
      repostlist=@teams_repos
    else
      reposlist=reposlist.get_repos_list(@client,@config,@deep)
    end
    if reposlist.one?{|aux| aux==path}
      @deep=TEAM_REPO
      puts "Set in #{@config["Team"]} repository: #{path}\n\n"
    end
  end
  if @deep==USER || @deep==ORGS || @deep==TEAM then puts "No repository is available with that name\n\n" end
end

#show_forksObject



248
249
250
251
252
253
# File 'lib/interface.rb', line 248

def show_forks()
  case
  when @deep==ORGS_REPO
    Repositories.new.show_forks(@client,@config,1)
  end
end