config/init.go

61 lines
1.2 KiB
Go
Raw Normal View History

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 (
"errors"
2024-11-05 17:06:26 +08:00
"github.com/jessevdk/go-flags"
"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
)
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
}
// 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()
}