Class: LTools::Tool

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/core/opts.rb,
lib/core/tool.rb

Direct Known Subclasses

Nmap, Tshark

Defined Under Namespace

Classes: ToolOpts

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Tool

Returns a new instance of Tool.



21
22
23
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
# File 'lib/core/tool.rb', line 21

def initialize(*args)
  tool, *args = args

  # 检测参数类型进行变换,支持一下格式:
  #   args = 'echo 1231'
  #   args = ['echo', '1231']
  #   args = [['echo', '1231']]
  case tool
  when String
    if args.empty? and tool.include? ' '
      tool, *args = Shellwords.split(tool)
    end
  when Array
    tool, *args = tool
  else
    raise
  end

  @cmd = args.unshift(tool).map(&:to_s)
  @process = ChildProcess.build(*@cmd)

  @stdout, @stdout_w = IO.pipe
  @process.io.stdout = @stdout_w 

  @stderr, @stderr_w = IO.pipe
  @process.io.stderr = @stderr_w

  @verbose = true if LTools.verbose
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



15
16
17
# File 'lib/core/tool.rb', line 15

def cmd
  @cmd
end

#stderrObject (readonly)

Returns the value of attribute stderr.



15
16
17
# File 'lib/core/tool.rb', line 15

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



15
16
17
# File 'lib/core/tool.rb', line 15

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



15
16
17
# File 'lib/core/tool.rb', line 15

def stdout
  @stdout
end

#verboseObject

Returns the value of attribute verbose.



16
17
18
# File 'lib/core/tool.rb', line 16

def verbose
  @verbose
end

Class Method Details

.check?Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/core/tool.rb', line 97

def self.check?
  system "hash #{@name} 2>&-"
  $?.success?
end

.inherited(child_class) ⇒ Object



93
94
95
# File 'lib/core/tool.rb', line 93

def self.inherited(child_class)
  LTools.add_tool(child_class)
end

Instance Method Details

#commandObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/core/tool.rb', line 74

def command
  @cmd.map{|str|
    if str.include?(' ')
      if str.include? '"'
        "'#{str}'"
      else
        "\"#{str}\""
      end
    else
      str
    end
  }.join ' '
end

#io_inherit!Object



88
89
90
91
# File 'lib/core/tool.rb', line 88

def io_inherit!
  @inherit = true
  @process.io.inherit!
end

#startObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/core/tool.rb', line 51

def start
  @process.duplex    = true unless @inherit
  @process.start
  @stdin = @process.io.stdin unless @inherit
  if @verbose
    puts "[#{'+'.light_green.bold}] #{self.class.to_s.light_yellow} (pid: #{pid})"
    puts "  #{?$.light_red.bold} #{command}"
    puts
  end
end

#stopObject



68
69
70
71
72
# File 'lib/core/tool.rb', line 68

def stop
  @process.stop
  @stdout_w.close
  @stderr_w.close
end

#waitObject



62
63
64
65
66
# File 'lib/core/tool.rb', line 62

def wait
  @process.wait
  @stdout_w.close
  @stderr_w.close
end