diff --git a/config/config.go b/config/config.go index eff3b75..4e158ee 100644 --- a/config/config.go +++ b/config/config.go @@ -170,6 +170,7 @@ func (c *Config) LoadDefaultConfig() (err error) { } c.InitHTTPClient() + c.readCredentialFromEnv() return } @@ -227,6 +228,12 @@ func (c *Config) LoadConfigFromContent(content []byte) (err error) { return } +func (c *Config) readCredentialFromEnv() (err error) { + c.AccessKeyID = os.Getenv(EnvAccessKeyID) + c.SecretAccessKey = os.Getenv(EnvSecretAccessKey) + return nil +} + // InitHTTPClient : After modifying Config.HTTPSettings, you should always call this to initialize the HTTP Client. func (c *Config) InitHTTPClient() { var emptySettings HTTPClientSettings diff --git a/config/contract.go b/config/contract.go index 2d80025..ba465e4 100644 --- a/config/contract.go +++ b/config/contract.go @@ -42,12 +42,28 @@ log_level: warn ` -// DefaultConfigFile is the filename of default config file. -const DefaultConfigFile = "~/.qingstor/config.yaml" +const ( + // DefaultConfigFile is the filename of default config file. + DefaultConfigFile = "~/.qingstor/config.yaml" + + // EnvConfigPath is config environment variable. + EnvConfigPath = "QINGSTOR_CONFIG_PATH" + + // EnvAccessKeyID is config envrionment variable. + EnvAccessKeyID = "QINGSTOR_ACCESS_KEY_ID" + + // EnvSecretAccessKey is config envrionment variable. + EnvSecretAccessKey = "QINGSTOR_SECRET_ACCESS_KEY" +) // GetUserConfigFilePath returns the user config file path. func GetUserConfigFilePath() string { - return strings.Replace(DefaultConfigFile, "~/", getHome()+"/", 1) + configFile := DefaultConfigFile + configPath := os.Getenv(EnvConfigPath) + if configPath != "" { + configFile = configPath + } + return strings.Replace(configFile, "~/", getHome()+"/", 1) } // InstallDefaultUserConfig will install default config file. diff --git a/config/contract_test.go b/config/contract_test.go new file mode 100644 index 0000000..6c808a0 --- /dev/null +++ b/config/contract_test.go @@ -0,0 +1,32 @@ +// +------------------------------------------------------------------------- +// | Copyright (C) 2016 Yunify, Inc. +// +------------------------------------------------------------------------- +// | Licensed under the Apache License, Version 2.0 (the "License"); +// | you may not use this work except in compliance with the License. +// | You may obtain a copy of the License in the LICENSE file, or at: +// | +// | http://www.apache.org/licenses/LICENSE-2.0 +// | +// | Unless required by applicable law or agreed to in writing, software +// | distributed under the License is distributed on an "AS IS" BASIS, +// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// | See the License for the specific language governing permissions and +// | limitations under the License. +// +------------------------------------------------------------------------- + +package config + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetUserConfigFilePath(t *testing.T) { + value := "/xxx/config.yaml" + os.Setenv(EnvConfigPath, value) + path := GetUserConfigFilePath() + assert.Equal(t, path, value) + os.Unsetenv(EnvConfigPath) +}