代码拉取完成,页面将自动刷新
package config
import (
"errors"
"fmt"
"gitee.com/tylf2018/go-micro-framework/pkg/common/util/fileutil"
"gitee.com/tylf2018/go-micro-framework/pkg/logger"
"github.com/spf13/pflag"
)
// ConfigOptions 获取主配置的配置文件
type ConfigOptions struct {
// 配置类型,native:从本地获取,nacos是从Nacos配置中心获取
Type string `mapstructure:"type" json:"type"`
// 本地配置文件路径,如果是nacos,则不需要写
NativePath string `mapstructure:"native-path" json:"native-path"`
// 本地配置文件名称,如果是nacos,则不需要写
NativeName string `mapstructure:"native-name" json:"native-name"`
// 本地配置文件类型,如果是nacos,则不需要写,支持的类型:ini、json、yaml
NativeType string `mapstructure:"native-type" json:"native-type"`
// nacos配置中心的配置
Nacos NacosConfig `mapstructure:"nacos" json:"nacos"`
}
type NacosConfig struct {
Host string `mapstructure:"host" json:"host"`
Port int `mapstructure:"port" json:"port"`
Namespace string `mapstructure:"namespace" json:"namespace"`
User string `mapstructure:"user" json:"user"`
Password string `mapstructure:"password" json:"password"`
DataId string `mapstructure:"dataid" json:"dataid"`
Group string `mapstructure:"group" json:"group"`
}
func NewConfigOptions() *ConfigOptions {
// 默认从本地文件获取
return &ConfigOptions{
Type: "init",
NativePath: fileutil.GetCurrentDirectory(),
NativeName: "main-config",
NativeType: "yaml",
}
}
func (c *ConfigOptions) Validate() error {
switch c.Type {
case "native", "init":
if len(c.NativeName) == 0 || c.NativeType == "" {
return errors.New("native_name is empty")
}
if len(c.NativeType) == 0 || c.NativeType == "" {
return errors.New("native_type is empty")
}
if c.NativeType != "ini" && c.NativeType != "json" && c.NativeType != "yaml" {
return errors.New(fmt.Sprintf("not supported native_type: %v", c.NativeType))
}
case "nacos":
if len(c.Nacos.Host) == 0 || c.Nacos.Host == "" {
return errors.New("nacos.host is empty")
}
if c.Nacos.Port == 0 {
return errors.New("nacos.port is empty")
}
if len(c.Nacos.Namespace) == 0 || c.Nacos.Namespace == "" {
return errors.New("nacos.namespace is empty")
}
if len(c.Nacos.User) == 0 || c.Nacos.User == "" {
return errors.New("nacos.user is empty")
}
if len(c.Nacos.Password) == 0 || c.Nacos.Password == "" {
return errors.New("nacos.password is empty")
}
if len(c.Nacos.DataId) == 0 || c.Nacos.DataId == "" {
return errors.New("nacos.dataid is empty")
}
if len(c.Nacos.Group) == 0 || c.Nacos.Group == "" {
return errors.New("nacos.group is empty")
}
default:
// 不支持的类型,不返回错误,以便后续能从命令行获取
logger.ErrorF("config read not supported type: %v", c.Type)
}
return nil
}
func (c *ConfigOptions) AddFlags(fs *pflag.FlagSet) {
// type
fs.StringVar(&c.Type, "type", c.Type, "the type of read configuration,must in [native,nacos],default : native")
// nativePath
fs.StringVar(&c.NativePath, "native-path", c.NativePath, "the config file path of native")
// nativeName
fs.StringVar(&c.NativeName, "native-name", c.NativeName, "the config file name of native")
// nativeType
fs.StringVar(&c.NativeType, "native-type", c.NativeType, "the config file type of native,must in [ini,json,yaml]")
// Nacos host
fs.StringVar(&c.Nacos.Host, "nacos.host", c.Nacos.Host, "the nacos host ,it is must when type is nacos ")
// Nacos port
fs.IntVar(&c.Nacos.Port, "nacos.port", c.Nacos.Port, "the nacos port ,it is must when type is nacos ")
// Nacos namespace
fs.StringVar(&c.Nacos.Namespace, "nacos.namespace", c.Nacos.Namespace, "the nacos namespace ,it is must when type is nacos ")
// Nacos user
fs.StringVar(&c.Nacos.User, "nacos-user", c.Nacos.User, "the nacos user ,it is must when type is nacos ")
// Nacos password
fs.StringVar(&c.Nacos.Password, "nacos-pass", c.Nacos.Password, "the nacos password ,it is must when type is nacos ")
// Nacos dataId
fs.StringVar(&c.Nacos.DataId, "nacos-dataid", c.Nacos.DataId, "the nacos dataid ,it is must when type is nacos ")
// Nacos group
fs.StringVar(&c.Nacos.Group, "nacos-group", c.Nacos.Group, "the nacos group ,it is must when type is nacos ")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。