Class: Sys

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

Constant Summary collapse

LIST =
%w[clear exit repos new_repo new_team rm_team rm_repo new_issue issues clone invite_outside_collaborators
rm_cloned new_eval foreach foreach_try files commits orgs invite_member_from_file invite_member people teams cd open bash].sort

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSys

Returns a new instance of Sys.



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

def initialize
  @memory = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#memoryObject (readonly)

Returns the value of attribute memory.



13
14
15
# File 'lib/actions/system.rb', line 13

def memory
  @memory
end

Instance Method Details

#add_history(value) ⇒ Object

CACHE READLINE METHODS



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

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

#add_history_str(mode, value) ⇒ Object



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

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

#add_users(path, data) ⇒ Object



153
154
155
156
157
158
# File 'lib/actions/system.rb', line 153

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



319
320
321
322
# File 'lib/actions/system.rb', line 319

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

#create_config(configure_path) ⇒ Object

creates all ghedsh local stuff



306
307
308
309
310
311
312
313
# File 'lib/actions/system.rb', line 306

def create_config(configure_path)
  con = { User: nil, user_url: nil, Org: nil, org_url: nil, Repo: nil, repo_url: nil, Team: nil, team_url: 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 "Configuration files created in #{configure_path}"
end

#create_temp(path) ⇒ Object



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

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

#createTempFile(data) ⇒ Object



358
359
360
361
362
363
# File 'lib/actions/system.rb', line 358

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

#execute_bash(exp) ⇒ Object



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

def execute_bash(exp)
  system(exp)
end

#get_login_token(path) ⇒ Object



167
168
169
170
171
# File 'lib/actions/system.rb', line 167

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

#load_assig_db(path) ⇒ Object



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

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)
  config
end

#load_clonefile(path) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/actions/system.rb', line 271

def load_clonefile(path)
  if File.exist?(path) == true
    if File.exist?("#{path}/ghedsh-clonedfiles")
      files = File.read("#{path}/ghedsh-clonedfiles")
      files = files.delete('['); files = files.delete(']')
      files = files.split(',')
      files
    else
      File.write("#{path}/ghedsh-clonedfiles", '')
      []
    end
  end
end

#load_config(configure_path, argv_token) ⇒ Object

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



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
109
110
111
112
# File 'lib/actions/system.rb', line 84

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

    if !token.nil?
      @client = (token)
      config['User'] = @client.
      config['user_url'] = @client.web_endpoint.to_s << @client..to_s
      userslist = load_users(configure_path)

      if userslist['users'].detect { |f| f[(config['User']).to_s] }.nil?
        add_users(configure_path, (config['User']).to_s => token)
      end
      save_token(configure_path, argv_token) unless argv_token.nil?
      return config
    else
      return set_loguin_data_sh(config, configure_path)
    end
  else
    create_config(configure_path)
    load_config(configure_path, argv_token)
  end
end

#load_config_user(configure_path, user) ⇒ Object

loading configure with –user mode



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/actions/system.rb', line 115

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

#load_groups(path) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/actions/system.rb', line 250

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)
  config
end

#load_memory(path, config) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/actions/system.rb', line 54

def load_memory(path, config)
  if File.exist?("#{path}/ghedsh-#{config['User']}-history")
    source = File.read("#{path}/ghedsh-#{config['User']}-history")
    s = source.split("\n")
    s.each do |i|
      Readline::HISTORY.push(i)
    end
  else
    File.write("#{path}/ghedsh-#{config['User']}-history", '')
  end
end

#load_people_db(path) ⇒ Object



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

def load_people_db(path)
  if File.exist?(path) == true
    if File.exist?("#{path}/ghedsh-people.json")
      json = File.read("#{path}/ghedsh-people.json")
    else
      con = { orgs: [] }
      File.write("#{path}/ghedsh-people.json", con.to_json)
      json = File.read("#{path}/ghedsh-people.json")
    end
  end
  config = JSON.parse(json)
  config
end

#load_script(path) ⇒ Object



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

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

#load_users(path) ⇒ Object



138
139
140
141
142
# File 'lib/actions/system.rb', line 138

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

#loadfile(path) ⇒ Object



410
411
412
413
414
415
416
417
418
419
# File 'lib/actions/system.rb', line 410

def loadfile(path)
  if File.exist?(path)
    mem = File.read(path)
    mem = mem.split("\n")
    mem
  else
    puts 'File not found'
    nil
  end
end

#login(token) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/actions/system.rb', line 173

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

  user
end

#open_url(url) ⇒ Object



421
422
423
424
425
426
427
# File 'lib/actions/system.rb', line 421

def open_url(url)
  if RUBY_PLATFORM.downcase.include?('darwin')
    system("open #{url}")
  elsif RUBY_PLATFORM.downcase.include?('linux')
    system("xdg-open #{url}")
  end
end

#quit_history(value) ⇒ Object



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

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

#refresh_clonefile(path, list) ⇒ Object



267
268
269
# File 'lib/actions/system.rb', line 267

def refresh_clonefile(path, list)
  File.write("#{path}/ghedsh-clonedfiles", list) if File.exist?(path) == true
end

#remove_temp(path) ⇒ Object



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

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

#return_deep(path) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/actions/system.rb', line 144

def return_deep(path)
 json = File.read("#{path}/ghedsh-cache.json")
 cache = JSON.parse(json)
 deep = User
 return deep = Team unless cache['Team'].nil?
 return deep = Organization unless cache['Org'].nil?
 deep
end

#save_assigs(path, data) ⇒ Object



297
298
299
# File 'lib/actions/system.rb', line 297

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

#save_cache(path, data) ⇒ Object



315
316
317
# File 'lib/actions/system.rb', line 315

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

#save_db(path, data) ⇒ Object



324
325
326
# File 'lib/actions/system.rb', line 324

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

#save_groups(path, data) ⇒ Object



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

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

#save_memory(path, config) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/actions/system.rb', line 66

def save_memory(path, config)
  mem = Readline::HISTORY.to_a
  me = ''
  mem.each do |i|
    me = me + i.to_s + "\n"
  end
  File.write("#{path}/ghedsh-#{config['User']}-history", me)
end

#save_people(path, data) ⇒ Object



301
302
303
# File 'lib/actions/system.rb', line 301

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

#save_token(path, token) ⇒ Object



160
161
162
163
164
165
# File 'lib/actions/system.rb', line 160

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



328
329
330
# File 'lib/actions/system.rb', line 328

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

#search_rexp(list, exp) ⇒ Object



336
337
338
339
# File 'lib/actions/system.rb', line 336

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

#search_rexp_peoplehash(list, exp) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/actions/system.rb', line 341

def search_rexp_peoplehash(list, exp)
  found = []
  yes = false
  list.each do |i|
    i.each do |j|
      unless j[1].nil?
        yes = true if j[1] =~ /#{exp}/
      end
    end
    if yes == true
      found.push(i)
      yes = false
    end
  end
  found
end

#set_loguin_data_sh(config, configure_path) ⇒ Object

initial program configure



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/actions/system.rb', line 185

def set_loguin_data_sh(config, configure_path)
  prompt = TTY::Prompt.new(enable_color: true)
  username = prompt.ask('Username:', required: true)
  passwd = prompt.ask('Password:', echo: false)

  client = Octokit::Client.new \
    login: username,
    password: passwd
  response = client.create_authorization(scopes: ['user', 'repo', 'admin:org', 'admin:public_key', 'admin:repo_hook', 'admin:org_hook', 'gist', 'notifications', 'delete_repo', 'admin:gpg_key'],
                                         note: 'GitHub Education Shell auth token')
  token = response[:token]
  us = (token)
  userhash = {}

  unless us.nil?
    puts Rainbow("Successful login as #{us.}\n").green
    config['User'] = us.
    config['user_url'] = us.web_endpoint << us.

    add_users(configure_path, (config['User']).to_s => token)
    save_token(configure_path, token)
    @client = us
    return config
  end
end

#showcachelist(list, exp) ⇒ Object



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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/actions/system.rb', line 365

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

  unless exp.nil?
    if exp =~ /^\//
      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)
        allpages = false if op == 'a'
        counter = 0
      end
      puts i
      rlist.push(i)
      counter += 1
    else

      if i.match(exp)
        puts i
        rlist.push(i)
        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



75
76
77
78
79
80
# File 'lib/actions/system.rb', line 75

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



47
48
49
50
51
52
# File 'lib/actions/system.rb', line 47

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