Class: Sys
- Inherits:
-
Object
- Object
- Sys
- 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 '].sort
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#memory ⇒ Object
readonly
Returns the value of attribute memory.
Instance Method Summary collapse
-
#add_history(value) ⇒ Object
CACHE READLINE METHODS.
- #add_history_str(mode, value) ⇒ Object
- #add_users(path, data) ⇒ Object
-
#create_config(configure_path) ⇒ Object
creates all ghedsh local stuff.
- #execute_bash(exp) ⇒ Object
- #get_login_token(path) ⇒ Object
-
#initialize ⇒ Sys
constructor
A new instance of Sys.
- #load_assig_db(path) ⇒ Object
-
#load_config(configure_path, argv_token) ⇒ Object
Loading initial configure, if ghedsh path doesnt exist, call the create method.
-
#load_config_user(configure_path, user) ⇒ Object
loading configure with –user mode.
- #load_users(path) ⇒ Object
- #login(token) ⇒ Object
- #parse ⇒ Object
- #quit_history(value) ⇒ Object
- #save_cache(path, data) ⇒ Object
- #save_db(path, data) ⇒ Object
- #save_token(path, token) ⇒ Object
- #save_users(path, data) ⇒ Object
- #search_rexp(list, exp) ⇒ Object
-
#set_loguin_data_sh(config, configure_path) ⇒ Object
initial program configure.
- #showcachelist(list, exp) ⇒ Object
- #write_initial_memory ⇒ Object
- #write_memory ⇒ Object
Constructor Details
#initialize ⇒ Sys
Returns a new instance of Sys.
14 15 16 |
# File 'lib/actions/system.rb', line 14 def initialize() @memory=[] end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/actions/system.rb', line 10 def client @client end |
#memory ⇒ Object (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
19 20 21 22 |
# File 'lib/actions/system.rb', line 19 def add_history(value) @memory.push(value) self.write_memory end |
#add_history_str(mode, value) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/actions/system.rb', line 29 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
119 120 121 122 123 124 |
# File 'lib/actions/system.rb', line 119 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
178 179 180 181 182 183 184 185 |
# File 'lib/actions/system.rb', line 178 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 |
#execute_bash(exp) ⇒ Object
200 201 202 |
# File 'lib/actions/system.rb', line 200 def execute_bash(exp) system(exp) end |
#get_login_token(path) ⇒ Object
133 134 135 136 137 |
# File 'lib/actions/system.rb', line 133 def get_login_token(path) json=File.read("#{path}/ghedsh-users.json") us=JSON.parse(json) return us["login"] end |
#load_assig_db(path) ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/actions/system.rb', line 166 def load_assig_db(path) if (File.exist?(path))==true json = File.read("#{path}/db/assignments.json") else #path="/db/assignments.json" #json = File.read(path) 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
60 61 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 |
# File 'lib/actions/system.rb', line 60 def load_config(configure_path,argv_token) if File.exist?(configure_path) if argv_token==nil token=self.get_login_token(configure_path) else token=argv_token end json = File.read("#{configure_path}/ghedsh-cache.json") config=JSON.parse(json) if token!=nil @client=self.login(token) config["User"]=@client.login 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/actions/system.rb', line 92 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.login(userFound["#{user}"]) config["User"]=@client.login 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_users(path) ⇒ Object
113 114 115 116 117 |
# File 'lib/actions/system.rb', line 113 def load_users(path) json=File.read("#{path}/ghedsh-users.json") users=JSON.parse(json) return users end |
#login(token) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/actions/system.rb', line 139 def login(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 |
#parse ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/actions/system.rb', line 209 def parse() = {:user => nil, :token => nil, :path => nil} parser = OptionParser.new do|opts| opts. = "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| [:token] = token; end opts.on('-c', '--configpath path', 'Give your own path for GHEDSH configuration files') do |path| [:configpath] = path; end opts.on('-u', '--user user', 'Change your user from your users list') do |user| [: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 end |
#quit_history(value) ⇒ Object
24 25 26 27 |
# File 'lib/actions/system.rb', line 24 def quit_history(value) @memory.pop(value) self.write_memory end |
#save_cache(path, data) ⇒ Object
188 189 190 |
# File 'lib/actions/system.rb', line 188 def save_cache(path,data) File.write("#{path}/ghedsh-cache.json",data.to_json) end |
#save_db(path, data) ⇒ Object
192 193 194 |
# File 'lib/actions/system.rb', line 192 def save_db(path,data) File.write("#{path}/db/assignments.json", data.to_json) end |
#save_token(path, token) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/actions/system.rb', line 126 def save_token(path,token) json=File.read("#{path}/ghedsh-users.json") login=JSON.parse(json) login["login"]=token File.write("#{path}/ghedsh-users.json",login.to_json) end |
#save_users(path, data) ⇒ Object
196 197 198 |
# File 'lib/actions/system.rb', line 196 def save_users(path,data) File.write("#{path}/ghedsh-users.json",data.to_json) end |
#search_rexp(list, exp) ⇒ Object
204 205 206 207 |
# File 'lib/actions/system.rb', line 204 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
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/actions/system.rb', line 150 def set_loguin_data_sh(config,configure_path) puts "Insert you Access Token: " token = gets.chomp us=self.login(token) userhash=Hash.new if us!=nil puts "Login succesful as #{us.login}\n" config["User"]=us.login self.add_users(configure_path,"#{config["User"]}"=>token) self.save_token(configure_path,token) @client=us return config end end |
#showcachelist(list, exp) ⇒ Object
242 243 244 245 246 247 248 249 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 282 283 284 285 286 287 |
# File 'lib/actions/system.rb', line 242 def showcachelist(list,exp) print "\n" rlist=[] =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_memory ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/actions/system.rb', line 50 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_memory ⇒ Object
43 44 45 46 47 48 |
# File 'lib/actions/system.rb', line 43 def write_memory history=(LIST+@memory).sort comp = proc { |s| history.grep( /^#{Regexp.escape(s)}/ ) } Readline.completion_append_character = "" Readline.completion_proc = comp end |