Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用了这个库几个月,想问下能不能支持使用环境变量方式 #3

Closed
DarkiT opened this issue Mar 27, 2023 · 4 comments · Fixed by #4
Closed

使用了这个库几个月,想问下能不能支持使用环境变量方式 #3

DarkiT opened this issue Mar 27, 2023 · 4 comments · Fixed by #4

Comments

@DarkiT
Copy link

DarkiT commented Mar 27, 2023

使用了这个库几个月,想问下能不能支持使用环境变量方式,因为我把这个库作为第三方库使用的时候发现需要重复读取几次配置文件。我的程序本来就使用了viper读取配置文件,默认配置文件里面已经包含了您这个库的配置参数,现在把您的库作为一个依赖库的话,就必须再读取一次配置文件才行。能不能增加一个函数,直接使用环境变量参数,这样就可以不用重复读取几次配置文件了。

@eleven26
Copy link
Owner

func NewWithViper(viper *viper.Viper) (*Goss, error) {}

这种方式可否满足你的需要?

@DarkiT
Copy link
Author

DarkiT commented Mar 27, 2023

func NewWithViper(viper *viper.Viper) (*Goss, error) {}

这种方式可否满足你的需要?

可以,作为一个Golang新手,我有一个疑问,如果其他程序是把配置文件解析到了结构体中,类似这样

type DriverType struct {
        Driver  string  `yaml:"driver"`
        Aliyun  Aliyun  `yaml:"aliyun"`
        Tencent Tencent `yaml:"tencent"`
        Qiniu   Qiniu   `yaml:"qiniu"`
        Huawei  Huawei  `yaml:"huawei"`
        S3      S3      `yaml:"s3"`
        Minio   Minio   `yaml:"minio"`
}
type Aliyun struct {
        Endpoint        string `yaml:"endpoint"`
        Bucket          string `yaml:"bucket"`
        AccessKeyID     string `yaml:"access_key_id"`
        AccessKeySecret string `yaml:"access_key_secret"`
}
type Tencent struct {
        URL       string `yaml:"url"`
        SecretID  string `yaml:"secret_id"`
        SecretKey string `yaml:"secret_key"`
}
type Qiniu struct {
        Bucket    string `yaml:"bucket"`
        AccessKey string `yaml:"access_key"`
        SecretKey string `yaml:"secret_key"`
        Zone      string `yaml:"zone"`
        Domain    string `yaml:"domain"`
        Private   string `yaml:"private"`
}
type Huawei struct {
        Endpoint  string `yaml:"endpoint"`
        Location  string `yaml:"location"`
        Bucket    string `yaml:"bucket"`
        AccessKey string `yaml:"access_key"`
        SecretKey string `yaml:"secret_key"`
}
type S3 struct {
        Endpoint  string `yaml:"endpoint"`
        Region    string `yaml:"region"`
        Bucket    string `yaml:"bucket"`
        AccessKey string `yaml:"access_key"`
        SecretKey string `yaml:"secret_key"`
}
type Minio struct {
        Endpoint  string `yaml:"endpoint"`
        Bucket    string `yaml:"bucket"`
        AccessKey string `yaml:"access_key"`
        SecretKey string `yaml:"secret_key"`
        UseSsl    bool   `yaml:"use_ssl"`
}

您提供的这种方式是不是就不能使用了?

@eleven26 eleven26 linked a pull request Mar 28, 2023 that will close this issue
@eleven26
Copy link
Owner

eleven26 commented Mar 28, 2023

办法是有的,就是不太优雅,你可以参考一下下面的这种写法:

type Minio struct {
	Endpoint  string `yaml:"endpoint"`
	Bucket    string `yaml:"bucket"`
	AccessKey string `yaml:"access_key"`
	SecretKey string `yaml:"secret_key"`
	UseSsl    bool   `yaml:"use_ssl"`
}

func newViper(name string, value interface{}) *viper.Viper {
	v := viper.New()

	r := reflect.ValueOf(value)
	for i := 0; i < r.NumField(); i++ {
		v.Set(name+"."+r.Type().Field(i).Tag.Get("yaml"), r.Field(i).Interface())
	}

	return v
}

func TestMinio(t *testing.T) {
	m := Minio{
		Endpoint:  "aa",
		Bucket:    "bb",
		AccessKey: "cc",
		SecretKey: "dd",
		UseSsl:    true,
	}

	v := newViper("minio", m)

	assert.Equal(t, "aa", v.GetString("minio.endpoint"))
	assert.Equal(t, "bb", v.GetString("minio.bucket"))
	assert.Equal(t, "cc", v.GetString("minio.access_key"))
	assert.Equal(t, "dd", v.GetString("minio.secret_key"))
	assert.Equal(t, true, v.GetBool("minio.use_ssl"))
}

可以根据配置的结构体和 oss 类型来创建一个 *viper.Viper 类型的对象。

这里用到了反射,不过一般来说应用内只会初始化一次,所以问题应该不大。

@DarkiT
Copy link
Author

DarkiT commented Mar 28, 2023

明白了,感谢解答。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants