Skip to content

Commit

Permalink
Refacto after ChatGPT for clearer visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Toinux committed May 25, 2023
1 parent ec41e1c commit 49c2460
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions lib/facters/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,51 @@ func GetNetFacts(f facter.IFacter) error {
for _, ipAddr := range v.Addrs {
var labelIPAddr, labelNetmask string
isIPv4 := reIPv4.MatchString(ipAddr.Addr)
isFirstFound := false

var netmaskBits uint64

if isIPv4 {
labelIPAddr = fmt.Sprintf("ipaddress_%v", ifName)
labelNetmask = fmt.Sprintf("netmask_%v", ifName)

if addr4idx == 0 {
isFirstFound = true
}
} else {
labelIPAddr = fmt.Sprintf("ipaddress6_%v", ifName)
if addr6idx == 0 {
isFirstFound = true
}
}

splitted := strings.Split(ipAddr.Addr, "/")
if len(splitted) > 0 {
if len(splitted) > 0 && isFirstFound {
f.Add(labelIPAddr, splitted[0])
}
if len(splitted) > 1 && isIPv4 {
netmaskBits, err := strconv.ParseUint(splitted[1], 10, 32)
if err == nil {
netmaskStr, err := common.ConvertNetmask(uint8(netmaskBits))

if isIPv4 {
if len(splitted) > 1 {
netmaskBits, err = strconv.ParseUint(splitted[1], 10, 32)
if err == nil {
f.Add(labelNetmask, netmaskStr)
netmaskStr, err := common.ConvertNetmask(uint8(netmaskBits))
if err == nil {
if isFirstFound {
f.Add(labelNetmask, netmaskStr)
} else {
f.Add(fmt.Sprintf("%s_%d", labelNetmask, addr4idx), netmaskStr)
}
}
}
}
}

if isIPv4 {
f.Add(fmt.Sprintf("ipaddress_%v_%d", ifName, addr4idx), splitted[0])
if !isFirstFound {
f.Add(fmt.Sprintf("%s_%d", labelIPAddr, addr4idx), splitted[0])
}
addr4idx++
} else {
f.Add(fmt.Sprintf("ipaddress6_%v_%d", ifName, addr6idx), splitted[0])
if !isFirstFound {
f.Add(fmt.Sprintf("%s_%d", labelIPAddr, addr6idx), splitted[0])
}
addr6idx++
}
}
Expand Down

0 comments on commit 49c2460

Please sign in to comment.