Merge pull request #406 from mugioka/imprv/convert-hyphen-included-in-repo-name-to-underbar

imprv: convert hyphen included in repo name to underbar with gatherOCIUsernamePassword.
This commit is contained in:
yxxhero 2022-10-03 15:08:25 +08:00 committed by GitHub
commit 3165551f9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 2 deletions

View File

@ -1498,6 +1498,20 @@ export MYOCIREGISTRY_USERNAME=spongebob
export MYOCIREGISTRY_PASSWORD=squarepants export MYOCIREGISTRY_PASSWORD=squarepants
``` ```
If `<registryName>` contains hyphens, the environment variable to be read is the hyphen replaced by an underscore., e.g.
```yaml
repositories:
- name: my-oci-registry
url: myregistry.azurecr.io
oci: true
```
```shell
export MY_OCI_REGISTRY_USERNAME=spongebob
export MY_OCI_REGISTRY_PASSWORD=squarepants
```
## Attribution ## Attribution
We use: We use:

View File

@ -455,15 +455,16 @@ func (st *HelmState) SyncRepos(helm RepoUpdater, shouldSkip map[string]bool) ([]
func gatherOCIUsernamePassword(repoName string, username string, password string) (string, string) { func gatherOCIUsernamePassword(repoName string, username string, password string) (string, string) {
var user, pass string var user, pass string
replacedRepoName := strings.ToUpper(strings.Replace(repoName, "-", "_", -1))
if username != "" { if username != "" {
user = username user = username
} else if u := os.Getenv(fmt.Sprintf("%s_USERNAME", strings.ToUpper(repoName))); u != "" { } else if u := os.Getenv(fmt.Sprintf("%s_USERNAME", replacedRepoName)); u != "" {
user = u user = u
} }
if password != "" { if password != "" {
pass = password pass = password
} else if p := os.Getenv(fmt.Sprintf("%s_PASSWORD", strings.ToUpper(repoName))); p != "" { } else if p := os.Getenv(fmt.Sprintf("%s_PASSWORD", replacedRepoName)); p != "" {
pass = p pass = p
} }

View File

@ -2420,3 +2420,72 @@ func TestReverse(t *testing.T) {
} }
} }
} }
func Test_gatherOCIUsernamePassword(t *testing.T) {
type args struct {
repoName string
username string
password string
}
tests := []struct {
name string
args args
envUsernameKey string
envUsernameValue string
envPasswordKey string
envPasswordValue string
wantUsername string
wantPassword string
}{
{
name: "pass username/password from args",
args: args{
repoName: "myOCIRegistry",
username: "username1",
password: "password1",
},
wantUsername: "username1",
wantPassword: "password1",
},
{
name: "repoName does not contain hyphen, read username/password from environment variables",
args: args{
repoName: "myOCIRegistry",
},
envUsernameKey: "MYOCIREGISTRY_USERNAME",
envUsernameValue: "username2",
envPasswordKey: "MYOCIREGISTRY_PASSWORD",
envPasswordValue: "password2",
wantUsername: "username2",
wantPassword: "password2",
},
{
name: "repoName contain hyphen, read username/password from environment variables",
args: args{
repoName: "my-oci-registry",
},
envUsernameKey: "MY_OCI_REGISTRY_USERNAME",
envUsernameValue: "username3",
envPasswordKey: "MY_OCI_REGISTRY_PASSWORD",
envPasswordValue: "password3",
wantUsername: "username3",
wantPassword: "password3",
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
if tt.envUsernameKey != "" && tt.envUsernameValue != "" {
t.Setenv(tt.envUsernameKey, tt.envUsernameValue)
}
if tt.envPasswordKey != "" && tt.envPasswordValue != "" {
t.Setenv(tt.envPasswordKey, tt.envPasswordValue)
}
gotUsername, gotPassword := gatherOCIUsernamePassword(tt.args.repoName, tt.args.username, tt.args.password)
if gotUsername != tt.wantUsername || gotPassword != tt.wantPassword {
t.Errorf("gatherOCIUsernamePassword() = got username/password %v/%v, want username/password %v/%v", gotUsername, gotPassword, tt.wantUsername, tt.wantPassword)
}
})
}
}