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



121
122
123
124
125
126
# File 'lib/actions/system.rb', line 121

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

#create_config(configure_path) ⇒ Object

creates all ghedsh local stuff



219
220
221
222
223
224
225
226
# File 'lib/actions/system.rb', line 219

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

#createTempFile(data) ⇒ Object



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

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

#execute_bash(exp) ⇒ Object



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

def execute_bash(exp)
  system(exp)
end

#get_login_token(path) ⇒ Object



135
136
137
138
139
# File 'lib/actions/system.rb', line 135

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

#load_assig_db(path) ⇒ Object



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

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
# 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
      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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/actions/system.rb', line 193

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



183
184
185
186
187
188
189
190
191
# File 'lib/actions/system.rb', line 183

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



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

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

#login(token) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/actions/system.rb', line 141

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



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/actions/system.rb', line 250

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

#save_assigs(path, data) ⇒ Object



214
215
216
# File 'lib/actions/system.rb', line 214

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

#save_cache(path, data) ⇒ Object



229
230
231
# File 'lib/actions/system.rb', line 229

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

#save_db(path, data) ⇒ Object



233
234
235
# File 'lib/actions/system.rb', line 233

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

#save_groups(path, data) ⇒ Object



210
211
212
# File 'lib/actions/system.rb', line 210

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

#save_token(path, token) ⇒ Object



128
129
130
131
132
133
# File 'lib/actions/system.rb', line 128

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



237
238
239
# File 'lib/actions/system.rb', line 237

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

#search_rexp(list, exp) ⇒ Object



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

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/actions/system.rb', line 152

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



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

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