summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-09-12 04:42:22 +0800
committerKeuin <[email protected]>2022-09-12 04:42:22 +0800
commit2b1e0dbeab6227f67e2ced780f325938202d51c3 (patch)
treeecc51f22f47ae7e75f4c75206a173ff22c21305e /main.go
parent25fc31b21eae2ae117180b72363f240d51008bee (diff)
Improve config `allowed_network_types`.v0.3.0
- Rename values to "ipv4", "ipv6" and "any". - Validate them when parsing.
Diffstat (limited to 'main.go')
-rw-r--r--main.go14
1 files changed, 13 insertions, 1 deletions
diff --git a/main.go b/main.go
index d6a433e..c130301 100644
--- a/main.go
+++ b/main.go
@@ -9,13 +9,16 @@ import (
"context"
"fmt"
"github.com/akamensky/argparse"
+ "github.com/keuin/slbr/bilibili"
"github.com/keuin/slbr/common"
"github.com/keuin/slbr/logging"
"github.com/keuin/slbr/recording"
+ "github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"log"
"os"
"os/signal"
+ "reflect"
"sync"
"syscall"
)
@@ -104,7 +107,16 @@ func getTasks() (tasks []recording.TaskConfig) {
return
}
var gc GlobalConfig
- err = viper.Unmarshal(&gc)
+ netType := reflect.TypeOf(bilibili.IP64)
+ err = viper.Unmarshal(&gc, func(conf *mapstructure.DecoderConfig) {
+ conf.DecodeHook = func(from reflect.Value, to reflect.Value) (interface{}, error) {
+ if to.Type() == netType &&
+ bilibili.IpNetType(from.String()).GetDialNetString() == "" {
+ return nil, fmt.Errorf("invalid IpNetType: %v", from.String())
+ }
+ return from.Interface(), nil
+ }
+ })
if err != nil {
err = fmt.Errorf("cannot parse config file \"%v\": %w", configFile, err)
return