class A4Tools::Command

Attributes

args[RW]
error[RW]
input[RW]
output[RW]
shell[RW]
status[R]

Public Class Methods

new(args, input=nil, output=nil, error=nil, shell=nil) click to toggle source
# File lib/net_shell/command.rb, line 8
def initialize(args, input=nil, output=nil, error=nil, shell=nil)
  @input = input || StandardInput.new
  @output = output || StandardOutput.new
  @error = error || StandardOutput.new
  @args = args
  @shell = shell
end

Public Instance Methods

built_in_methods() click to toggle source
# File lib/net_shell/command.rb, line 27
def built_in_methods
  @shell.built_ins
end
get_option_set() click to toggle source
# File lib/net_shell/command.rb, line 40
def get_option_set
  if args.length <= 1 or is_built_in?
    tab_complete_built_in.map { |opt| opt.to_s || "" }
  else
    []
  end
end
is_built_in?() click to toggle source
# File lib/net_shell/command.rb, line 16
def is_built_in?
  built_in_methods.include? args[0].to_sym
end
make_commandline(args) click to toggle source
# File lib/net_shell/command.rb, line 53
def make_commandline(args)
  (args.map do |arg|
    if arg.match(/\s/).nil? then
      arg
    else
      arg.shellescape
    end
  end).join(" ")
end
run() click to toggle source
# File lib/net_shell/command.rb, line 91
def run
  return "" if args.empty? or args[0].nil?
  return run_built_in if is_built_in?
  return run_external
end
run_built_in() click to toggle source
# File lib/net_shell/command.rb, line 20
def run_built_in
  built_in = @shell.built_in(args[0]).new(@shell)
  built_in.execute(@input, @output, @error, args)

  @status = built_in.status
end
run_external() click to toggle source
# File lib/net_shell/command.rb, line 63
def run_external
  begin
    cmdline = make_commandline(args)
    Open3.popen3(cmdline) do |stdin, stdout, stderr, wait_thr|
      begin
        pid = wait_thr[:pid]

        stdin.write(@input.read)
        stdin.close

        @status = wait_thr.value

        @output.write(stdout.read)
        stdout.close
        stderr.close
      rescue Errno::EPIPE
        @error.write("-netshell: #{args[0]}: #{stderr.read}\n")
        @status = 127
      end
    end
  rescue Errno::ENOENT
    @output.write("-netshell: #{args[0]}: command not found\n")
    @status = 127
  end

  @status
end
tab_complete() click to toggle source
# File lib/net_shell/command.rb, line 48
def tab_complete
  fragment = args.last || ""
  get_option_set.select { |opt| opt.to_s.start_with? fragment }
end
tab_complete_built_in() click to toggle source
# File lib/net_shell/command.rb, line 31
def tab_complete_built_in
  case args.length
  when 0..1
    built_in_methods
  else
    @shell.tab_complete_built_in(args)
  end
end