github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/dhclient/iface.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dhclient
     6  
     7  import (
     8  	"fmt"
     9  	"regexp"
    10  
    11  	"github.com/vishvananda/netlink"
    12  )
    13  
    14  // Interfaces takes an RE and returns a
    15  // []netlink.Link that matches it, or an error. It is an error
    16  // for the returned list to be empty.
    17  func Interfaces(ifName string) ([]netlink.Link, error) {
    18  	ifRE, err := regexp.CompilePOSIX(ifName)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	ifnames, err := netlink.LinkList()
    24  	if err != nil {
    25  		return nil, fmt.Errorf("can not get list of link names: %v", err)
    26  	}
    27  
    28  	var filteredIfs []netlink.Link
    29  	for _, iface := range ifnames {
    30  		if ifRE.MatchString(iface.Attrs().Name) {
    31  			filteredIfs = append(filteredIfs, iface)
    32  		}
    33  	}
    34  
    35  	if len(filteredIfs) == 0 {
    36  		return nil, fmt.Errorf("no interfaces match %s", ifName)
    37  	}
    38  	return filteredIfs, nil
    39  }