-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add env and config file as default config
- Loading branch information
Showing
22 changed files
with
513 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package configfile | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/qiniu/go-sdk/v7/internal/env" | ||
) | ||
|
||
type profileConfig struct { | ||
AccessKey string `toml:"access_key"` | ||
SecretKey string `toml:"secret_key"` | ||
BucketURL interface{} `toml:"bucket_url"` | ||
DisableSecureProtocol bool `toml:"disable_secure_protocol"` | ||
} | ||
|
||
var profileConfigs map[string]*profileConfig | ||
|
||
var ErrInvalidBucketUrl = errors.New("invalid bucket url") | ||
|
||
func CredentialsFromConfigFile() (string, string, error) { | ||
profile, err := getProfile() | ||
if err != nil || profile == nil { | ||
return "", "", err | ||
} else if profile.AccessKey == "" || profile.SecretKey == "" { | ||
return "", "", nil | ||
} | ||
return profile.AccessKey, profile.SecretKey, nil | ||
} | ||
|
||
func BucketURLsFromConfigFile() ([]string, error) { | ||
profile, err := getProfile() | ||
if err != nil || profile == nil { | ||
return nil, err | ||
} else if profile.BucketURL == "" { | ||
return nil, nil | ||
} | ||
switch u := profile.BucketURL.(type) { | ||
case string: | ||
return strings.Split(u, ","), nil | ||
case []interface{}: | ||
var bucketUrls []string | ||
for _, v := range u { | ||
if s, ok := v.(string); ok { | ||
bucketUrls = append(bucketUrls, s) | ||
} else { | ||
return nil, ErrInvalidBucketUrl | ||
} | ||
} | ||
return bucketUrls, nil | ||
} | ||
return nil, ErrInvalidBucketUrl | ||
} | ||
|
||
func DisableSecureProtocolFromConfigFile() (bool, error) { | ||
profile, err := getProfile() | ||
if err != nil || profile == nil { | ||
return false, err | ||
} | ||
return profile.DisableSecureProtocol, nil | ||
} | ||
|
||
func getProfile() (*profileConfig, error) { | ||
if err := load(); err != nil { | ||
return nil, err | ||
} | ||
profileName := env.ProfileFromEnvironment() | ||
if profileName == "" { | ||
profileName = "default" | ||
} | ||
profile, ok := profileConfigs[profileName] | ||
if !ok || profile == nil { | ||
return nil, nil | ||
} | ||
return profile, nil | ||
} | ||
|
||
func load() error { | ||
if profileConfigs != nil { | ||
return nil | ||
} | ||
configFilePath := env.ConfigFileFromEnvironment() | ||
if configFilePath == "" { | ||
configFilePath = getDefaultConfigFilePath() | ||
} | ||
_, err := toml.DecodeFile(configFilePath, &profileConfigs) | ||
return err | ||
} | ||
|
||
func getDefaultConfigFilePath() string { | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
homeDir = "" | ||
} | ||
return filepath.Join(homeDir, ".qiniu", "config.toml") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
//go:build unit | ||
// +build unit | ||
|
||
package configfile | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestLoad(t *testing.T) { | ||
file, err := os.CreateTemp("", "config.toml") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(file.Name()) | ||
defer file.Close() | ||
|
||
_, err = file.WriteString(` | ||
[default] | ||
access_key = "QINIU_ACCESS_KEY_1" | ||
secret_key = "QINIU_SECRET_KEY_1" | ||
[private-cloud] | ||
access_key = "QINIU_ACCESS_KEY_2" | ||
secret_key = "QINIU_SECRET_KEY_2" | ||
bucket_url = "https://uc.qbox.me" | ||
disable_secure_protocol = true | ||
[private-cloud-2] | ||
access_key = "QINIU_ACCESS_KEY_3" | ||
secret_key = "QINIU_SECRET_KEY_3" | ||
bucket_url = ["https://uc.qbox.me", "https://uc.qiniuapi.com"] | ||
`) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
os.Setenv("QINIU_CONFIG_FILE", file.Name()) | ||
defer os.Unsetenv("QINIU_CONFIG_FILE") | ||
if err = load(); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
profileConfigs = nil | ||
}() | ||
if len(profileConfigs) != 3 { | ||
t.Fatal("Unexpected profile configs") | ||
} | ||
if profileConfigs["default"].AccessKey != "QINIU_ACCESS_KEY_1" { | ||
t.Fatal("Unexpected access key") | ||
} | ||
if profileConfigs["default"].SecretKey != "QINIU_SECRET_KEY_1" { | ||
t.Fatal("Unexpected secret key") | ||
} | ||
if profileConfigs["default"].BucketURL != nil { | ||
t.Fatal("Unexpected bucket url") | ||
} | ||
if profileConfigs["default"].DisableSecureProtocol { | ||
t.Fatal("Unexpected disable secure protocol") | ||
} | ||
if profileConfigs["private-cloud"].AccessKey != "QINIU_ACCESS_KEY_2" { | ||
t.Fatal("Unexpected access key") | ||
} | ||
if profileConfigs["private-cloud"].SecretKey != "QINIU_SECRET_KEY_2" { | ||
t.Fatal("Unexpected secret key") | ||
} | ||
if profileConfigs["private-cloud"].BucketURL.(string) != "https://uc.qbox.me" { | ||
t.Fatal("Unexpected bucket url") | ||
} | ||
if !profileConfigs["private-cloud"].DisableSecureProtocol { | ||
t.Fatal("Unexpected disable secure protocol") | ||
} | ||
if profileConfigs["private-cloud-2"].AccessKey != "QINIU_ACCESS_KEY_3" { | ||
t.Fatal("Unexpected access key") | ||
} | ||
if profileConfigs["private-cloud-2"].SecretKey != "QINIU_SECRET_KEY_3" { | ||
t.Fatal("Unexpected secret key") | ||
} | ||
if bucketUrls, ok := profileConfigs["private-cloud-2"].BucketURL.([]interface{}); !ok { | ||
t.Fatal("Unexpected bucket url") | ||
} else { | ||
if bucketUrls[0].(string) != "https://uc.qbox.me" { | ||
t.Fatal("Unexpected bucket url") | ||
} | ||
if bucketUrls[1].(string) != "https://uc.qiniuapi.com" { | ||
t.Fatal("Unexpected bucket url") | ||
} | ||
} | ||
if profileConfigs["private-cloud-2"].DisableSecureProtocol { | ||
t.Fatal("Unexpected disable secure protocol") | ||
} | ||
|
||
os.Setenv("QINIU_PROFILE", "private-cloud") | ||
defer os.Unsetenv("QINIU_PROFILE") | ||
|
||
accessKey, secretKey, err := CredentialsFromConfigFile() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if accessKey != "QINIU_ACCESS_KEY_2" { | ||
t.Fatal("Unexpected access key") | ||
} | ||
if secretKey != "QINIU_SECRET_KEY_2" { | ||
t.Fatal("Unexpected secret key") | ||
} | ||
|
||
bucketUrls, err := BucketURLsFromConfigFile() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(bucketUrls) != 1 { | ||
t.Fatal("Unexpected bucket urls") | ||
} | ||
if bucketUrls[0] != "https://uc.qbox.me" { | ||
t.Fatal("Unexpected bucket url") | ||
} | ||
|
||
os.Setenv("QINIU_PROFILE", "private-cloud-2") | ||
defer os.Unsetenv("QINIU_PROFILE") | ||
|
||
accessKey, secretKey, err = CredentialsFromConfigFile() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if accessKey != "QINIU_ACCESS_KEY_3" { | ||
t.Fatal("Unexpected access key") | ||
} | ||
if secretKey != "QINIU_SECRET_KEY_3" { | ||
t.Fatal("Unexpected secret key") | ||
} | ||
|
||
bucketUrls, err = BucketURLsFromConfigFile() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(bucketUrls) != 2 { | ||
t.Fatal("Unexpected bucket urls") | ||
} | ||
if bucketUrls[0] != "https://uc.qbox.me" { | ||
t.Fatal("Unexpected bucket url") | ||
} | ||
if bucketUrls[1] != "https://uc.qiniuapi.com" { | ||
t.Fatal("Unexpected bucket url") | ||
} | ||
} |
Oops, something went wrong.