Top Level Namespace

Constant Summary collapse

REQUIRED_PLUGINS =

The plugins you are not allowed to unload or reload. To reload these, you need to restart the application.

["load", "unload", "ezycli"]

Instance Method Summary collapse

Instance Method Details

#application_startObject



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cli.rb', line 63

def application_start
Dir["#{File.expand_path(File.dirname(__FILE__))}/plugins/*.rb"].sort.each do |file|
    puts "#{debug}Loading application plugin #{File.basename(file, ".rb")}"
  require file
  @plugin_counter += 1
  @plugin_array.push(File.basename(file))
  begin
    @plugin_help.push(plugin_help) unless @plugin_help.include? plugin_help
  rescue NameError => e
    # Do nothing if there's no such method
  end
  begin
    @plugin_commands.merge!(plugin_commands)
  rescue NameError => e
    # Do nothing if theunlessre's no such method
  end
  begin
    @plugin_associations.merge!(plugin_associations)
  rescue NameError => e
    # Do nothing if there's no such method
  end
end
begin
  load_history
rescue NoMethodError => e
  puts "#{error}Unable to load history!"
end
puts "#{info}Attempting to load user plugins from #{File.expand_path(File.dirname("~/.ezycli/plugins"))}"
Dir["#{File.expand_path("~/.ezycli/plugins")}/*.rb"].sort.each do |file|
    puts "#{debug}Loading user plugin #{File.basename(file, ".rb")}"
  require file
  @plugin_counter += 1
  @plugin_array.push(File.basename(file))
  begin
    @plugin_help.push(plugin_help) unless @plugin_help.include? plugin_help
  rescue NameError => e
    # Do nothing if there's no such method
  end
  begin
    @plugin_commands.merge!(plugin_commands)
  rescue NameError => e
    # Do nothing if there's no such method
  end
  begin
    @plugin_associations.merge!(plugin_associations)
  rescue NameError => e
    # Do nothing if there's no such method
  end
end
File.open("#{File.expand_path("~/.ezycli/plugs/php")}/hello-world.php", 'w') { |file| file.write("<?php \necho 'Hello, World!';\n") } unless File.file? "#{File.expand_path("~/.ezycli/plugs/php")}/hello-world.php"
File.open("#{File.expand_path("~/.ezycli/plugins")}/test.rb", 'w') { |file| file.write("def test_function\nputs 'This is a test plugin that was automatically generated when you first ran ezyCLI'\nend\n\n############ THESE FUNCTIONS MUST EXIST ############\n\ndef plugin_commands\n# This is where you tell ezyCLI about the plugins it's to use\nreturn {\"testme\" => \"test_function\"}\nend\n\ndef plugin_associations\n# This is where you tell ezyCLI about which plugins belong to which file. I'll eventually fix that.\nreturn \"test\" => [\"testme\"]\nend") } unless File.file? "#{File.expand_path("~/.ezycli/plugins")}/test.rb"
end

#cli_cmd(cmd) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/plugins/ezycli.rb', line 24

def cli_cmd(cmd)
  begin
    case
    when (cmd.include?("help"))
      puts "help         - Provides this help text"
      puts "version      - Shows the version of ezyCLI"
      puts "exit         - Exits ezyCLI"
      puts @plugin_help
    when (cmd.include?("exit"))
      @newline = 0
      exit 0
    when (cmd.include?("version"))
      puts "ezyCLI version 0.2.3 DEV"
    when (cmd.empty?)
      # blank command...
    else
      c = cmd.split(" ")
      if c.count == 1
      begin
        send(@plugin_commands["#{cmd}"])
      rescue ArgumentError => e
        puts "#{warning}Wrong number of arguments given"
      rescue TypeError => e
        puts "#{warning}Unknown command: #{cmd}"
      end
      else
        args = Array.new
        begin
        c[1..-1].each do |arg|
          args.push(arg)
        end
        send(@plugin_commands["#{c[0]}"], *args)
      rescue ArgumentError => e
        puts "#{warning}Wrong number of arguments given"
      rescue TypeError => e
        puts "#{warning}Unknown command: #{cmd}"
      end
      end
    end
  rescue NoMethodError => e
    # Nothing doing
  end
end

#cli_is_readyObject

What? you thought that the CLI also wouldn’t be a plugin?



4
5
6
7
# File 'lib/plugins/ezycli.rb', line 4

def cli_is_ready
  puts "#{info}Loaded #{@plugin_counter} plugin(s)!"
  puts "#{info}Welcome to the ezyCLI! _stuck? try 'help'"
end

#cli_loopObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/plugins/ezycli.rb', line 9

def cli_loop
  loop {
      begin
        input = Readline.readline("ezyCLI> ", true)
        if input.instance_of? String
          cli_cmd(input)
        else
          exit 0
        end
        rescue SystemExit, Interrupt => e
          exit 0
      end
  }
end

#colorize(text, color = "default", bgColor = "default") ⇒ Object

COLORS!



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cli.rb', line 12

def colorize(text, color = "default", bgColor = "default")
    colors = {"default" => "38","black" => "30","red" => "31","green" => "32","brown" => "33", "blue" => "34", "purple" => "35",
     "cyan" => "36", "gray" => "37", "dark gray" => "1;30", "light red" => "1;31", "light green" => "1;32", "yellow" => "1;33",
      "light blue" => "1;34", "light purple" => "1;35", "light cyan" => "1;36", "white" => "1;37"}
    bgColors = {"default" => "0", "black" => "40", "red" => "41", "green" => "42", "brown" => "43", "blue" => "44",
     "purple" => "45", "cyan" => "46", "gray" => "47", "dark gray" => "100", "light red" => "101", "light green" => "102",
     "yellow" => "103", "light blue" => "104", "light purple" => "105", "light cyan" => "106", "white" => "107"}
    color_code = colors[color]
    bgColor_code = bgColors[bgColor]
    return "\033[#{bgColor_code};#{color_code}m#{text}\033[0m"
end

#debugObject



34
35
36
# File 'lib/cli.rb', line 34

def debug
  return colorize("[DEBUG] ", "blue")
end

#errorObject



31
32
33
# File 'lib/cli.rb', line 31

def error
  return colorize("[ERROR] ", "red")
end

#exit_handlerObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cli.rb', line 38

def exit_handler
  if !@newline
    puts ""
  end
  begin
    save_history
  rescue NoMethodError => e
    puts "#{error}Unable to save history!"
  end
  puts "#{debug}Exiting ezyCLI"
  puts "#{info}Have a great day!"
end

#infoObject

Color mappings



25
26
27
# File 'lib/cli.rb', line 25

def info
  return colorize("[INFO] ", "green")
end

#load_historyObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/plugins/history.rb', line 8

def load_history
  if File.file?("#{File.expand_path("~/.ezycli")}/.ezycli_history")
    conf = YAML.load_file "#{File.expand_path("~/.ezycli")}/.ezycli_history"
    conf.each do |line|
      if line.present? and !line.include? "exit"
        Readline::HISTORY.push(line)
      end
    end
  else
    puts "#{info}Initialized empty history"
  end
end

#load_plugin(plugin_name) ⇒ Object

Yes, the ability to load plugins is itself a plugin. You may also not unload the plugin loader. This is a hardcoded dep for the application to run correctly



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/plugins/load.rb', line 3

def load_plugin(plugin_name)
  if @plugin_associations["plugin_name"]
    puts "#{error}Cowardly refusing to load a plugin that's already loaded"
  else
    begin
      silence_warnings {load "#{File.expand_path("~/.ezycli/plugins")}/"+plugin_name+".rb"}
      begin
        @plugin_help.push(plugin_help) unless @plugin_help.include? plugin_help
      rescue NameError => e
        # Do nothing if there's no such method
      end
      begin
        @plugin_commands.merge!(plugin_commands)
      rescue NameError => e
        # Do nothing if there's no such method
      end
      begin
        @plugin_associations.merge!(plugin_associations)
      rescue NameError => e
        # Do nothing if there's no such method
      end
      puts "#{info}Loaded plugin #{plugin_name}"
    rescue Exception => e
      puts "#{warning}Failed to load plugin #{plugin_name}"
    end
  end
end

#plug_exec(language, plugin_name) ⇒ Object

WARNING: DO NOT MODIFY THIS FILE! I CANNOT BE MORE EXPLICIT! WARNING: THIS FILE LOADS THE RIGHT PLUG FOR THE LANGUAGE. *DO NOT TOUCH ME*!



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/plugins/plugs.rb', line 3

def plug_exec(language, plugin_name)
  io = IO.popen("#{language} #{File.expand_path("~/.ezycli/plugs")}/php/#{plugin_name}.php")
  trap("INT") {
    puts "#{info}Ctrl-C received, cleaning up"
    Process.kill("INT", io.pid)
  }
  io.each do |line|
    if line.chomp.include? "Could not open input file"
      puts "#{error}Failed to run #{language} plug #{plugin_name}"
    else
      puts line.chomp
    end
  end
  trap('INT', 'DEFAULT')
end

#plug_loader(language, plugin_name) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/plugins/plugs.rb', line 19

def plug_loader(language, plugin_name)
  case
  when (language == "php")
    puts "#{warning}Plugs are limited to output only at this time"
    plug_exec("php", plugin_name)
  else
    puts "#{error}Couldn't find suitable plug language for #{language}"
  end
end

#plugin_associationsObject



42
43
44
45
# File 'lib/plugins/load.rb', line 42

def plugin_associations
  # see the above comment.
  return "load" => ["load"]
end

#plugin_commandsObject

THESE FUNCTIONS MUST EXIST ############



23
24
25
26
# File 'lib/plugins/history.rb', line 23

def plugin_commands
  # I know, I know. I'll clean it up later. shhhhh
  return {"load" => "load_plugin"}
end

#plugin_helpObject

THESE FUNCTIONS MUST EXIST ############



33
34
35
# File 'lib/plugins/load.rb', line 33

def plugin_help
  return ["load         - Loads a plugin"]
end

#reload_plugin(plugin_name) ⇒ Object



1
2
3
4
# File 'lib/plugins/reload.rb', line 1

def reload_plugin(plugin_name)
  unload_plugin plugin_name
  load_plugin plugin_name
end

#save_historyObject



3
4
5
6
# File 'lib/plugins/history.rb', line 3

def save_history
  File.open("#{File.expand_path("~/.ezycli")}/.ezycli_history", 'w') { |f| f.write(YAML.dump(Readline::HISTORY.to_a))}
  puts "#{info}Wrote history to #{File.expand_path("~/.ezycli")}/.ezycli_history"
end

#silence_warningsObject



1
2
3
4
5
6
7
# File 'lib/plugins/silence.rb', line 1

def silence_warnings
    original_verbosity = $VERBOSE
    $VERBOSE = nil
    result = yield
    $VERBOSE = original_verbosity
    return result
end

#unload_plugin(plugin_name) ⇒ Object

Yes, the ability to unload plugins is itself a plugin. You may also not unload the plugin unloader. This is a hardcoded dep for the application to run correctly



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/plugins/unload.rb', line 3

def unload_plugin(plugin_name)
  if REQUIRED_PLUGINS.include? plugin_name
    puts "#{error}Cowardly refusing to unload a core plugin"
  else
    begin
      @plugin_associations["#{plugin_name}"].each do |ext|
        @plugin_commands.delete("#{ext}")
        @plugin_help.reject! {|e| e == ext}
      end
      @plugin_associations.delete("#{plugin_name}")
      puts "#{info}Unloaded plugin #{plugin_name}"
    rescue Exception => e
      puts "#{warning}Failed to unload plugin #{plugin_name}"
    end
  end
end

#warningObject



28
29
30
# File 'lib/cli.rb', line 28

def warning
  return colorize("[WARNING] ", "yellow")
end