Class: A4Tools::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, input = nil, output = nil, error = nil, shell = nil) ⇒ Command

Returns a new instance of Command.



8
9
10
11
12
13
14
# 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

Instance Attribute Details

#argsObject

Returns the value of attribute args.



5
6
7
# File 'lib/net_shell/command.rb', line 5

def args
  @args
end

#errorObject

Returns the value of attribute error.



5
6
7
# File 'lib/net_shell/command.rb', line 5

def error
  @error
end

#inputObject

Returns the value of attribute input.



5
6
7
# File 'lib/net_shell/command.rb', line 5

def input
  @input
end

#outputObject

Returns the value of attribute output.



5
6
7
# File 'lib/net_shell/command.rb', line 5

def output
  @output
end

#shellObject

Returns the value of attribute shell.



5
6
7
# File 'lib/net_shell/command.rb', line 5

def shell
  @shell
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/net_shell/command.rb', line 6

def status
  @status
end

Instance Method Details

#built_in_methodsObject



27
28
29
# File 'lib/net_shell/command.rb', line 27

def built_in_methods
  @shell.built_ins
end

#get_option_setObject



40
41
42
43
44
45
46
# 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?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/net_shell/command.rb', line 16

def is_built_in?
  built_in_methods.include? args[0].to_sym
end

#make_commandline(args) ⇒ Object



53
54
55
56
57
58
59
60
61
# 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

#runObject



91
92
93
94
95
# 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_inObject



20
21
22
23
24
25
# 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_externalObject



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/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_completeObject



48
49
50
51
# 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_inObject



31
32
33
34
35
36
37
38
# 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