Class: Sys

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

Constant Summary collapse

LIST =
['repos', 'exit', 'orgs','help', 'people','teams', 'cd ', 'cd repo ','commits','forks', 'add_team_member ','new_team ','rm_team ','new_repository ','new_assignment ','clone ', 'issues',
'version', 'cat ', 'groups', 'files', 'assignments','new_issue ', 'open_issue', 'new_','open_', 'close_issue', 'new_group ', 'rm_group', 'rm_', 'do ', 'info','make','add_repo',
'add_group','rm_repository '].sort

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSys

Returns a new instance of Sys.



16
17
18
# File 'lib/actions/system.rb', line 16

def initialize()
  @memory=[]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/actions/system.rb', line 10

def client
  @client
end

#memoryObject (readonly)

Returns the value of attribute memory.



11
12
13
# File 'lib/actions/system.rb', line 11

def memory
  @memory
end

Instance Method Details

#add_history(value) ⇒ Object

CACHE READLINE METHODS



21
22
23
24
# File 'lib/actions/system.rb', line 21

def add_history(value)
  @memory.push(value)
  self.write_memory
end

#add_history_str(mode, value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/actions/system.rb', line 31

def add_history_str(mode,value)
  if mode==1
    value.each do |i|
      @memory.push(i[0])
      self.write_memory
    end
  end
  if mode==2
    value.each do |i|
      @memory.push(i)
      self.write_memory
    end
  end
end

#add_users(path, data) ⇒ Object



151
152
153
154
155
156
# File 'lib/actions/system.rb', line 151

def add_users(path,data)
  json=File.read("#{path}/ghedsh-users.json")
  users=JSON.parse(json)
  users["users"].push(data)
  File.write("#{path}/ghedsh-users.json",users.to_json)
end

#clear_cache(path) ⇒ Object



275
276
277
278
# File 'lib/actions/system.rb', line 275

def clear_cache(path)
  con={:User=>nil,:Org=>nil,:Repo=>nil,:Team=>nil,:TeamID=>nil}
  File.write("#{path}/ghedsh-cache.json",con.to_json)
end

#create_config(configure_path) ⇒ Object

creates all ghedsh local stuff



261
262
263
264
265
266
267
268
# File 'lib/actions/system.rb', line 261

def create_config(configure_path)
  con={:User=>nil,:Org=>nil,:Repo=>nil,:Team=>nil,:TeamID=>nil}
  us={:login=>nil, :users=>[]}
  FileUtils.mkdir_p(configure_path)
  File.write("#{configure_path}/ghedsh-cache.json",con.to_json)
  File.write("#{configure_path}/ghedsh-users.json",us.to_json)
  puts "Confiration files created in #{configure_path}"
end

#create_temp(path) ⇒ Object



240
241
242
243
244
# File 'lib/actions/system.rb', line 240

def create_temp(path)
  if (File.exist?(path))==false
    FileUtils.mkdir_p(path)
  end
end

#createTempFile(data) ⇒ Object



330
331
332
333
334
335
# File 'lib/actions/system.rb', line 330

def createTempFile(data)
  tempfile="temp.txt"
  path="#{ENV['HOME']}/.ghedsh/#{tempfile}"
  File.write(path,data)
  return path
end

#execute_bash(exp) ⇒ Object



288
289
290
# File 'lib/actions/system.rb', line 288

def execute_bash(exp)
  system(exp)
end

#get_login_token(path) ⇒ Object



165
166
167
168
169
# File 'lib/actions/system.rb', line 165

def (path)
  json=File.read("#{path}/ghedsh-users.json")
  us=JSON.parse(json)
  return us["login"]
end

#load_assig_db(path) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/actions/system.rb', line 198

def load_assig_db(path)
  if (File.exist?(path))==true
    if File.exist?("#{path}/assignments.json")
      json = File.read("#{path}/assignments.json")
    else
      #{"Organization":[{"name":null,"assignments":[{"name":null,"teams":{"teamid":null}}]}]}
      con={:orgs=>[]}
      File.write("#{path}/assignments.json",con.to_json)
      json = File.read("#{path}/assignments.json")
    end
  end
  config=JSON.parse(json)
  return config
end

#load_config(configure_path, argv_token) ⇒ Object

Loading initial configure, if ghedsh path doesnt exist, call the create method



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/actions/system.rb', line 62

def load_config(configure_path,argv_token)
  if File.exist?(configure_path)
    if argv_token==nil
      token=self.(configure_path)
    else
      token=argv_token
    end
    json = File.read("#{configure_path}/ghedsh-cache.json")
    config=JSON.parse(json)

    if token!=nil
      @client=self.(token)
      config["User"]=@client.
      userslist=self.load_users(configure_path)

      if userslist["users"].detect {|f| f["#{config["User"]}"] }==nil
        self.add_users(configure_path,"#{config["User"]}"=>token)
      end
      if argv_token!=nil
        self.save_token(configure_path,argv_token)
      end
      return config
    else
      return self.set_loguin_data_sh(config,configure_path)
    end
  else
    self.create_config(configure_path)
    load_config(configure_path,argv_token)
  end
end

#load_config_user(configure_path, user) ⇒ Object

loading configure with –user mode



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/actions/system.rb', line 94

def load_config_user(configure_path, user)
  if File.exist?(configure_path)
    list=self.load_users(configure_path)
    userFound=list["users"].detect {|f| f["#{user}"]}
    if userFound!=nil
      self.clear_cache(configure_path)
      json = File.read("#{configure_path}/ghedsh-cache.json")
      config=JSON.parse(json)
      @client=self.(userFound["#{user}"])
      config["User"]=@client.
      self.save_token(configure_path,userFound["#{user}"])
      return config
    else
      puts "User not found"
      return nil
    end
  else
    puts "No user's history is available"
    return nil
  end
end

#load_groups(path) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/actions/system.rb', line 223

def load_groups(path)
  if (File.exist?(path))==true
    if File.exist?("#{path}/groups.json")
      json = File.read("#{path}/groups.json")
    else
      con={:orgs=>[]}
      File.write("#{path}/groups.json",con.to_json)
      json = File.read("#{path}/groups.json")
    end
  else
    #path="/db/assignments.json"
    #json = File.read(path)
  end
    config=JSON.parse(json)
    return config
end

#load_script(path) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/actions/system.rb', line 213

def load_script(path)
  if (File.exist?(path))==true
    script = File.read("#{path}")
    return script.split("\n")
  else
    puts "No script is found with that name"
    return []
  end
end

#load_users(path) ⇒ Object



116
117
118
119
120
# File 'lib/actions/system.rb', line 116

def load_users(path)
  json=File.read("#{path}/ghedsh-users.json")
  users=JSON.parse(json)
  return users
end

#login(token) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/actions/system.rb', line 171

def (token)
  begin
    user=Octokit::Client.new(:access_token =>token) #per_page:100
    user.auto_paginate=true #show all pages of any query
  rescue
    puts "Oauth error"
  end
  return user
end

#parseObject



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
# File 'lib/actions/system.rb', line 297

def parse()
  options = {:user => nil, :token => nil, :path => nil}

  parser = OptionParser.new do|opts|
  	opts.banner = "Usage: ghedsh [options]\nWith no options it runs with default configuration. Configuration files are being set in #{ENV['HOME']}/.ghedsh\n"
  	opts.on('-t', '--token token', 'Provides a github access token by argument.') do |token|
  		options[:token] = token;
  	end
    opts.on('-c', '--configpath path', 'Give your own path for GHEDSH configuration files') do |path|
      options[:configpath] = path;
    end
  	opts.on('-u', '--user user', 'Change your user from your users list') do |user|
  		options[:user] = user;
  	end
    opts.on('-v', '--version', 'Show the current version of GHEDSH') do
      puts "GitHub Education Shell v#{Ghedsh::VERSION}"
      exit
    end
  	opts.on('-h', '--help', 'Displays Help') do
  		puts opts
  		exit
  	end
  end

  begin
    parser.parse!
  rescue
    puts "Argument error. Use ghedsh -h or ghedsh --help for more information about the usage of this program"
    exit
  end
  return options
end

#quit_history(value) ⇒ Object



26
27
28
29
# File 'lib/actions/system.rb', line 26

def quit_history(value)
  @memory.pop(value)
  self.write_memory
end

#remove_temp(path) ⇒ Object



246
247
248
249
250
# File 'lib/actions/system.rb', line 246

def remove_temp(path)
  if (File.exist?(path))==true
    system("rm -rf #{path}")
  end
end

#return_deep(path) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/actions/system.rb', line 122

def return_deep(path)
 json = File.read("#{path}/ghedsh-cache.json")
 cache=JSON.parse(json)
 deep=1
 case
   when cache["Team"]!=nil
     if cache["Repo"]!=nil
       deep=5
     else
       deep=4
     end
   when cache["Team"]==nil
     if cache["Org"]!=nil
       if cache["Repo"]!=nil
         deep=3
       else
         deep=2
       end
     else
       if cache["Repo"]!=nil
        deep=10
       else
         deep=1
       end
     end
 end
 return deep
end

#save_assigs(path, data) ⇒ Object



256
257
258
# File 'lib/actions/system.rb', line 256

def save_assigs(path,data)
  File.write("#{path}/assignments.json",data.to_json)
end

#save_cache(path, data) ⇒ Object



271
272
273
# File 'lib/actions/system.rb', line 271

def save_cache(path,data)
  File.write("#{path}/ghedsh-cache.json",data.to_json)
end

#save_db(path, data) ⇒ Object



280
281
282
# File 'lib/actions/system.rb', line 280

def save_db(path,data)
  File.write("#{path}/db/assignments.json", data.to_json)
end

#save_groups(path, data) ⇒ Object



252
253
254
# File 'lib/actions/system.rb', line 252

def save_groups(path,data)
  File.write("#{path}/groups.json",data.to_json)
end

#save_token(path, token) ⇒ Object



158
159
160
161
162
163
# File 'lib/actions/system.rb', line 158

def save_token(path,token)
  json=File.read("#{path}/ghedsh-users.json")
  =JSON.parse(json)
  ["login"]=token
  File.write("#{path}/ghedsh-users.json",.to_json)
end

#save_users(path, data) ⇒ Object



284
285
286
# File 'lib/actions/system.rb', line 284

def save_users(path,data)
  File.write("#{path}/ghedsh-users.json",data.to_json)
end

#search_rexp(list, exp) ⇒ Object



292
293
294
295
# File 'lib/actions/system.rb', line 292

def search_rexp(list,exp)
  list= list.select{|o| o.match(/#{exp}/)}
  return list
end

#set_loguin_data_sh(config, configure_path) ⇒ Object

initial program configure



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/actions/system.rb', line 182

def set_loguin_data_sh(config,configure_path)
  puts "Insert you Access Token: "
  token = gets.chomp
  us=self.(token)
  userhash=Hash.new

  if us!=nil
    puts "Login succesful as #{us.}\n"
    config["User"]=us.
    self.add_users(configure_path,"#{config["User"]}"=>token)
    self.save_token(configure_path,token)
    @client=us
    return config
  end
end

#showcachelist(list, exp) ⇒ Object



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
# File 'lib/actions/system.rb', line 337

def showcachelist(list,exp)
  print "\n"
  rlist=[]
  options=Hash.new
  o=Organizations.new
  regex=false

  if exp!=nil
    if exp.match(/^\//)
      regex=true
      sp=exp.split('/')
      exp=Regexp.new(sp[1],sp[2])
    end
  end
  counter=0
  allpages=true

  list.each do |i|
    if regex==false
      if counter==100 && allpages==true
        op=Readline.readline("\nThere are more results. Show next repositories (press any key) or Show all repositories (press a): ",true)
        if op=="a"
          allpages=false
        end
        counter=0
      end
      puts i
      rlist.push(i)
      counter=counter+1
    else

    if i.match(exp)
        puts i
        rlist.push(i)
        counter=counter+1
      end
    end
  end

  if rlist.empty?
    puts "No repository matches with that expression"
  else
    print "\n"
    puts "Repositories found: #{rlist.size}"
  end
end

#write_initial_memoryObject



52
53
54
55
56
57
58
# File 'lib/actions/system.rb', line 52

def write_initial_memory
  history=LIST+memory
  comp = proc { |s| LIST.grep( /^#{Regexp.escape(s)}/ ) }

  Readline.completion_append_character = ""
  Readline.completion_proc = comp
end

#write_memoryObject



45
46
47
48
49
50
# File 'lib/actions/system.rb', line 45

def write_memory
  history=(LIST+@memory).sort
  comp = proc { |s| history.grep( /^#{Regexp.escape(s)}/ ) }
  Readline.completion_append_character = ""
  Readline.completion_proc = comp
end