Skip to content

Commit

Permalink
Add a #netcards array to return Hwinfo objects instead of a hash
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 3, 2019
1 parent e6dbaba commit 0fa47dd
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/lib/y2network/hwinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
require "y2network/driver"
require "y2network/udev_rule"

Yast.import "LanItems"

module Y2Network
class HardwareWrapper
def initialize
Expand All @@ -29,24 +31,35 @@ def initialize

# Returns the network devices hardware information
#
# @return [Array<Hwinfo>]
# @return [Array<Hwinfo>] Hardware information for netword devices
def netcards
return @netcards if @netcards
read_hardware
@netcards = ReadHardware("netcard").map do |attrs|
add_missing_attrs(attrs)
Hwinfo.new(attrs)
name = attrs["dev_name"]
extra_attrs = name ? extra_attrs_for(name) : {}
Hwinfo.new(attrs.merge(extra_attrs))
end
end

private

def add_missing_attrs(hash)
name = hash["dev_name"]
return unless name
# Add aditional attributes
#
# @param name [String] Device name
# @return [Hash] Hash containing extra attributes
def extra_attrs_for(name)
extra = {}
raw_dev_port = Yast::SCR.Read(
Yast::Path.new(".target.string"), "/sys/class_net/#{name}/dev_port"
).to_s.strip
hash["dev_port"] = raw_dev_port unless raw_dev_port.empty?
extra["dev_port"] = raw_dev_port unless raw_dev_port.empty?
extra
end

# Makes sure that the hardware information was read
def read_hardware
Yast::LanItems.ReadHw if Yast::LanItems.Hardware.empty?
end
end

Expand All @@ -73,6 +86,13 @@ def for(name)
hwinfo_from_hardware(name) || hwinfo_from_udev(name) || Hwinfo.new
end

# Returns the network devices hardware information
#
# @return [Array<Hwinfo>] Hardware information for netword devices
def netcards
hardware_wrapper.netcards
end

# Resets the hardware information
#
# It will be re-read the next time is needed.
Expand Down

0 comments on commit 0fa47dd

Please sign in to comment.