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

config: Support environment variables #120

Merged
merged 2 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (c *Config) LoadDefaultConfig() (err error) {
}

c.InitHTTPClient()
c.readCredentialFromEnv()
return
}

Expand Down Expand Up @@ -227,6 +228,12 @@ func (c *Config) LoadConfigFromContent(content []byte) (err error) {
return
}

func (c *Config) readCredentialFromEnv() (err error) {
c.AccessKeyID = os.Getenv(EnvAccessKeyID)
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
22 changes: 19 additions & 3 deletions config/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions config/contract_test.go
Original file line number Diff line number Diff line change
@@ -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)
}