2024-11-05 17:06:26 +08:00
|
|
|
// Package config ...
|
|
|
|
//
|
|
|
|
// Description : config ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2024-11-05 16:23
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2024-11-05 18:04:27 +08:00
|
|
|
"errors"
|
2024-11-05 17:06:26 +08:00
|
|
|
"github.com/jessevdk/go-flags"
|
2024-11-05 18:04:27 +08:00
|
|
|
"reflect"
|
2024-11-05 17:06:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Init 初始化配置
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:25 2024/11/5
|
|
|
|
func Init(configReceiver any) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2024-11-05 18:04:27 +08:00
|
|
|
if err = checkConfigReceiver(configReceiver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-05 17:06:26 +08:00
|
|
|
if _, err = flags.Parse(configReceiver); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-05 18:04:27 +08:00
|
|
|
|
|
|
|
// checkConfigReceiver 校验配置接收者必须是指针
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:16 2024/11/5
|
|
|
|
func checkConfigReceiver(configReceiver any) error {
|
|
|
|
if nil == configReceiver {
|
|
|
|
return errors.New("config receiver is nil")
|
|
|
|
}
|
|
|
|
receiverType := reflect.TypeOf(configReceiver)
|
|
|
|
if receiverType.Kind() != reflect.Ptr {
|
|
|
|
return errors.New("config receiver is not a pointer")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cloneReceiver ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:16 2024/11/5
|
|
|
|
func cloneReceiver(configReceiver any) any {
|
|
|
|
dataType := reflect.TypeOf(configReceiver).Elem()
|
|
|
|
return reflect.New(dataType).Interface()
|
|
|
|
}
|