func loadFromConfDir(c *libcni, max int) error {
	files, err := cnilibrary.ConfFiles(c.pluginConfDir, []string{".conf", ".conflist", ".json"})
	switch {
	case err != nil:
		return fmt.Errorf("failed to read config file: %v: %w", err, ErrRead)
	case len(files) == 0:
		return fmt.Errorf("no network config found in %s: %w", c.pluginConfDir, ErrCNINotInitialized)
	}
	sort.Strings(files)
	i := 0
	var networks []*Network
	for _, confFile := range files {
		var confList *cnilibrary.NetworkConfigList
		if strings.HasSuffix(confFile, ".conflist") {
			confList, err = cnilibrary.ConfListFromFile(confFile)
			if err != nil {
				return fmt.Errorf("failed to load CNI config list file %s: %v: %w", confFile, err, ErrInvalidConfig)
			}
		} else {
			conf, err := cnilibrary.ConfFromFile(confFile)
			if err != nil {
				return fmt.Errorf("failed to load CNI config file %s: %v: %w", confFile, err, ErrInvalidConfig)
			}
			if conf.Network.Type == "" {
				return fmt.Errorf("network type not found in %s: %w", confFile, ErrInvalidConfig)
			}
			confList, err = cnilibrary.ConfListFromConf(conf)
			if err != nil {
				return fmt.Errorf("failed to convert CNI config file %s to CNI config list: %v: %w", confFile, err, ErrInvalidConfig)
			}
		}
		if len(confList.Plugins) == 0 {
			return fmt.Errorf("CNI config list in config file %s has no networks, skipping: %w", confFile, ErrInvalidConfig)
		}
		networks = append(networks, &Network{
			cni:    c.cniConfig,
			config: confList,
			ifName: getIfName(c.prefix, i),
		})
		i++
		if i == max {
			break
		}
	}
	if len(networks) == 0 {
		return fmt.Errorf("no valid networks found in %s: %w", c.pluginDirs, ErrCNINotInitialized)
	}
	c.networks = append(c.networks, networks...)
	return nil
}
loadFromConfDir detects network config files from the
configured cni config directory and load them. max is
the maximum network config to load (max i<= 0 means no limit).
package cni
import (
	"fmt"
	"sort"
	"strings"
	cnilibrary "github.com/containernetworking/cni/libcni"
)
type Opt func(c *libcni) error
func WithInterfacePrefix(prefix string) Opt {
	return func(c *libcni) error {
		c.prefix = prefix
		return nil
	}
}
func WithPluginDir(dirs []string) Opt {
	return func(c *libcni) error {
		c.pluginDirs = dirs
		c.cniConfig = &cnilibrary.CNIConfig{Path: dirs}
		return nil
	}
}
func WithPluginConfDir(dir string) Opt {
	return func(c *libcni) error {
		c.pluginConfDir = dir
		return nil
	}
}
func WithPluginMaxConfNum(max int) Opt {
	return func(c *libcni) error {
		c.pluginMaxConfNum = max
		return nil
	}
}
func WithMinNetworkCount(count int) Opt {
	return func(c *libcni) error {
		c.networkCount = count
		return nil
	}
}
func WithLoNetwork(c *libcni) error {
	loConfig, _ := cnilibrary.ConfListFromBytes([]byte(`{
"cniVersion": "0.3.1",
"name": "cni-loopback",
"plugins": [{
  "type": "loopback"
}]
}`))
	c.networks = append(c.networks, &Network{
		cni:    c.cniConfig,
		config: loConfig,
		ifName: "lo",
	})
	return nil
}
func WithConf(bytes []byte) Opt {
	return WithConfIndex(bytes, 0)
}
func WithConfIndex(bytes []byte, index int) Opt {
	return func(c *libcni) error {
		conf, err := cnilibrary.ConfFromBytes(bytes)
		if err != nil {
			return err
		}
		confList, err := cnilibrary.ConfListFromConf(conf)
		if err != nil {
			return err
		}
		c.networks = append(c.networks, &Network{
			cni:    c.cniConfig,
			config: confList,
			ifName: getIfName(c.prefix, index),
		})
		return nil
	}
}
func WithConfFile(fileName string) Opt {
	return func(c *libcni) error {
		conf, err := cnilibrary.ConfFromFile(fileName)
		if err != nil {
			return err
		}
		confList, err := cnilibrary.ConfListFromConf(conf)
		if err != nil {
			return err
		}
		c.networks = append(c.networks, &Network{
			cni:    c.cniConfig,
			config: confList,
			ifName: getIfName(c.prefix, 0),
		})
		return nil
	}
}
func WithConfListBytes(bytes []byte) Opt {
	return func(c *libcni) error {
		confList, err := cnilibrary.ConfListFromBytes(bytes)
		if err != nil {
			return err
		}
		i := len(c.networks)
		c.networks = append(c.networks, &Network{
			cni:    c.cniConfig,
			config: confList,
			ifName: getIfName(c.prefix, i),
		})
		return nil
	}
}
func WithConfListFile(fileName string) Opt {
	return func(c *libcni) error {
		confList, err := cnilibrary.ConfListFromFile(fileName)
		if err != nil {
			return err
		}
		i := len(c.networks)
		c.networks = append(c.networks, &Network{
			cni:    c.cniConfig,
			config: confList,
			ifName: getIfName(c.prefix, i),
		})
		return nil
	}
}
func WithDefaultConf(c *libcni) error {
	return loadFromConfDir(c, c.pluginMaxConfNum)
}
func WithAllConf(c *libcni) error {
	return loadFromConfDir(c, 0)
}