61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
// Package config ...
|
|
//
|
|
// Description : config ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2024-11-05 16:23
|
|
package config
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/jessevdk/go-flags"
|
|
"reflect"
|
|
)
|
|
|
|
// Init 初始化配置
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 16:25 2024/11/5
|
|
func Init(configReceiver any) error {
|
|
var (
|
|
err error
|
|
)
|
|
|
|
if err = checkConfigReceiver(configReceiver); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err = flags.Parse(configReceiver); nil != err {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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()
|
|
}
|