class Sys

Constants

LIST

Attributes

client[R]
memory[R]

Public Class Methods

new() click to toggle source
# File lib/actions/system.rb, line 17
def initialize
  @memory = []
end

Public Instance Methods

add_history(value) click to toggle source

CACHE READLINE METHODS

# File lib/actions/system.rb, line 22
def add_history(value)
  @memory.push(value)
  write_memory
end
add_history_str(mode, value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
createTempFile(data) click to toggle source
# File lib/actions/system.rb, line 358
def createTempFile(data)
  tempfile = 'temp.txt'
  path = "#{ENV['HOME']}/.ghedsh/#{tempfile}"
  File.write(path, data)
  path
end
create_config(configure_path) click to toggle source

creates all ghedsh local stuff

# 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) click to toggle source
# File lib/actions/system.rb, line 285
def create_temp(path)
  FileUtils.mkdir_p(path) if File.exist?(path) == false
end
execute_bash(exp) click to toggle source
# File lib/actions/system.rb, line 332
def execute_bash(exp)
  system(exp)
end
get_login_token(path) click to toggle source
# File lib/actions/system.rb, line 167
def get_login_token(path)
  json = File.read("#{path}/ghedsh-users.json")
  us = JSON.parse(json)
  us['login']
end
load_assig_db(path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

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

    if !token.nil?
      @client = login(token)
      config['User'] = @client.login
      config['user_url'] = @client.web_endpoint.to_s << @client.login.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) click to toggle source

loading configure with –user mode

# 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 = login(userFound[user.to_s])
      config['User'] = @client.login
      config['user_url'] = @client.web_endpoint.to_s << @client.login.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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/actions/system.rb, line 173
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 StandardError
    puts 'Oauth error'
  end

  user
end
open_url(url) click to toggle source
# 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) click to toggle source
# File lib/actions/system.rb, line 27
def quit_history(value)
  @memory.pop(value)
  write_memory
end
refresh_clonefile(path, list) click to toggle source
# 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) click to toggle source
# File lib/actions/system.rb, line 289
def remove_temp(path)
  system("rm -rf #{path}") if File.exist?(path) == true
end
return_deep(path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/actions/system.rb, line 160
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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

initial program configure

# 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 = login(token)
  userhash = {}

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

    add_users(configure_path, (config['User']).to_s => token)
    save_token(configure_path, token)
    @client = us
    return config
  end
end
showcachelist(list, exp) click to toggle source
# 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_memory() click to toggle source
# 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_memory() click to toggle source
# 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