feat: refactor []string config parser

This commit is contained in:
mzz2017
2023-02-01 12:30:26 +08:00
parent 0b85d99571
commit 43b39093a3
3 changed files with 20 additions and 19 deletions

View File

@ -11,7 +11,6 @@ import (
"github.com/v2rayA/dae/pkg/logger"
"os"
"os/signal"
"strings"
"syscall"
)
@ -60,12 +59,12 @@ func Run() (err error) {
nodeList = append(nodeList, nodes...)
}
if param.Global.LanInterface == "" && param.Global.WanInterface == "" {
if len(param.Global.LanInterface) == 0 && len(param.Global.WanInterface) == 0 {
return fmt.Errorf("LanInterface and WanInterface cannot both be empty")
}
// New ControlPlane.
onlyBindLanInterface := param.Global.WanInterface == ""
onlyBindLanInterface := len(param.Global.WanInterface) == 0
t, err := control.NewControlPlane(
log,
nodeList,
@ -81,20 +80,14 @@ func Run() (err error) {
}
// Bind to links.
if param.Global.LanInterface != "" {
ifnames := strings.Split(param.Global.LanInterface, ",")
for _, ifname := range ifnames {
if err = t.BindLan(ifname); err != nil {
return fmt.Errorf("BindLan: %v: %w", ifname, err)
}
for _, ifname := range param.Global.LanInterface {
if err = t.BindLan(ifname); err != nil {
return fmt.Errorf("BindLan: %v: %w", ifname, err)
}
}
if param.Global.WanInterface != "" {
ifnames := strings.Split(param.Global.WanInterface, ",")
for _, ifname := range ifnames {
if err = t.BindWan(ifname); err != nil {
return fmt.Errorf("BindWan: %v: %w", ifname, err)
}
for _, ifname := range param.Global.WanInterface {
if err = t.BindWan(ifname); err != nil {
return fmt.Errorf("BindWan: %v: %w", ifname, err)
}
}

View File

@ -17,8 +17,8 @@ type Global struct {
CheckUrl string `mapstructure:"check_url" default:"https://connectivitycheck.gstatic.com/generate_204"`
CheckInterval time.Duration `mapstructure:"check_interval" default:"15s"`
DnsUpstream string `mapstructure:"dns_upstream" default:"1.1.1.1:53"`
LanInterface string `mapstructure:"lan_interface"`
WanInterface string `mapstructure:"wan_interface"`
LanInterface []string `mapstructure:"lan_interface"`
WanInterface []string `mapstructure:"wan_interface"`
}
type Group struct {

View File

@ -111,10 +111,18 @@ func paramParser(to reflect.Value, section *config_parser.Section, ignoreType []
}
} else {
// String value.
if field.Val.Kind() == reflect.Interface {
switch field.Val.Kind() {
case reflect.Interface:
// Field is interface{}, we can assign.
field.Val.Set(reflect.ValueOf(itemVal.Val))
} else {
case reflect.Slice:
// Field is not interface{}, we can decode.
vPointerNew := reflect.New(field.Val.Type().Elem())
if !common.FuzzyDecode(vPointerNew.Interface(), itemVal.Val) {
return fmt.Errorf("failed to parse \"%v.%v\": value \"%v\" cannot be convert to %v", section.Name, itemVal.Key, itemVal.Val, field.Val.Type().Elem().String())
}
field.Val.Set(reflect.Append(field.Val, vPointerNew.Elem()))
default:
// Field is not interface{}, we can decode.
if !common.FuzzyDecode(field.Val.Addr().Interface(), itemVal.Val) {
return fmt.Errorf("failed to parse \"%v.%v\": value \"%v\" cannot be convert to %v", section.Name, itemVal.Key, itemVal.Val, field.Val.Type().String())