Class: Interface

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

Instance Method Summary collapse

Constructor Details

#initializeInterface

Returns a new instance of Interface.



8
# File 'lib/interface.rb', line 8

def initialize; end

Instance Method Details

#parseObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/interface.rb', line 10

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 StandardError
    puts 'Argument error. Use ghedsh -h or ghedsh --help for more information about the usage of this program'
    exit
  end
  options
end

#runObject



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
92
93
94
95
96
97
98
99
100
101
# File 'lib/interface.rb', line 67

def run
  puts @shell_enviroment.client.say('GitHub Education Shell')
  sleep(1.5)
  @shell_enviroment.client.say.clear
  system('clear')
  
  loop do
    begin
      input = Readline.readline(@shell_enviroment.prompt, true)
      # handle ctrl-d (eof), equivalent to exit (saving configuration)
      if input.nil?
        @shell_enviroment.commands['exit'].call(nil)
        exit!(0)
      end
      input = input.strip.split
      command_name = input[0]
      input.shift
      command_params = input
      unless command_name.to_s.empty?
        if command_name.start_with?('!')
          command_name = command_name.delete('!')
          command_params.unshift(command_name)
          @shell_enviroment.commands['bash'].call(command_params)
        elsif @shell_enviroment.commands.key?(command_name)
          result = @shell_enviroment.commands[command_name].call(command_params)
        else
          puts Rainbow("-ghedsh: #{command_name}: command not found").yellow
        end
      end
    rescue StandardError => e
      puts e
    end
    break if result == 0
  end
end

#startObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/interface.rb', line 43

def start
  spinner = custom_spinner("Starting ghedsh CLI")
  options = parse

  trap('SIGINT') { puts; throw :ctrl_c }

  catch :ctrl_c do
    begin
      if options[:user].nil? && options[:token].nil? && !options[:path].nil?
        @shell_enviroment = ShellContext.new(options[:user], options[:path], options[:token])
      else
        @shell_enviroment = ShellContext.new(options[:user], "#{ENV['HOME']}/.ghedsh", options[:token])
      end
      spinner.stop
      run
    rescue SystemExit, Interrupt
      raise
    rescue Exception => e
      puts 'exit'
      puts e
    end
  end
end