class Inspec::Resources::Port

def info

def info
  return @cache if !@cache.nil?
  # abort if os detection has not worked
  return @cache = [] if @port_manager.nil?
  # query ports
  cache = @port_manager.info || []
  cache.select! { |x| x['port'] == @port } unless @port.nil?
  cache.select! { |x| x['address'] == @ip } unless @ip.nil?
  @cache = cache
end

def initialize(*args)

def initialize(*args)
  args.unshift(nil) if args.length <= 1 # add the ip address to the front
  @ip = args[0]
  @port = if args[1].nil?
            nil
          else
            args[1].to_i
          end
  @cache = nil
  @port_manager = port_manager_for_os
  return skip_resource 'The `port` resource is not supported on your OS yet.' if @port_manager.nil?
end

def port_manager_for_os

def port_manager_for_os
  os = inspec.os
  if os.linux?
    LinuxPorts.new(inspec)
  elsif os.aix?
    # AIX: see http://www.ibm.com/developerworks/aix/library/au-lsof.html#resources
    #      and https://www-01.ibm.com/marketing/iwm/iwm/web/reg/pick.do?source=aixbp
    AixPorts.new(inspec)
  elsif os.darwin?
    # Darwin: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/lsof.8.html
    LsofPorts.new(inspec)
  elsif os.windows?
    WindowsPorts.new(inspec)
  elsif ['freebsd'].include?(os[:family])
    FreeBsdPorts.new(inspec)
  elsif os.solaris?
    SolarisPorts.new(inspec)
  elsif os.hpux?
    HpuxPorts.new(inspec)
  end
end

def to_s

def to_s
  "Port #{@port}"
end