Modify the repositories validation function `resloveRepoNames` and add a

unit test.

Signed-off-by: Dong Gang <dong.gang@daocloud.io>
pull/8491/head
Dong Gang 5 years ago
parent 3e275baef7
commit 6216bb5817

@ -16,12 +16,11 @@ limitations under the License.
package main package main
import ( import (
"fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/client-go/util/homedir" "k8s.io/client-go/util/homedir"
@ -69,7 +68,7 @@ func newDependencyBuildCmd(out io.Writer) *cobra.Command {
} }
err := man.Build() err := man.Build()
if e, ok := err.(downloader.ErrRepoNotFound); ok { if e, ok := err.(downloader.ErrRepoNotFound); ok {
return errors.Errorf("no repository definition for %s. Please add the missing repos via 'helm repo add'", strings.Join(e.Repos, ", ")) return fmt.Errorf("%s. Please add the missing repos via 'helm repo add'", e.Error())
} }
return err return err
}, },

@ -48,6 +48,7 @@ type ErrRepoNotFound struct {
Repos []string Repos []string
} }
// Error implements the error interface.
func (e ErrRepoNotFound) Error() string { func (e ErrRepoNotFound) Error() string {
return fmt.Sprintf("no repository definition for %s", strings.Join(e.Repos, ", ")) return fmt.Sprintf("no repository definition for %s", strings.Join(e.Repos, ", "))
} }
@ -479,31 +480,14 @@ func (m *Manager) resolveRepoNames(deps []*chart.Dependency) (map[string]string,
if !found { if !found {
repository := dd.Repository repository := dd.Repository
// Add if URL // Add if URL
_, err := url.ParseRequestURI(repository) if _, err := url.ParseRequestURI(repository); err != nil {
if err == nil { return nil, err
reposMap[repository] = repository
continue
} }
missing = append(missing, repository) missing = append(missing, repository)
} }
} }
if len(missing) > 0 { if len(missing) > 0 {
errorMessage := fmt.Sprintf("no repository definition for %s. Please add them via 'helm repo add'", strings.Join(missing, ", ")) return nil, ErrRepoNotFound{missing}
// It is common for people to try to enter "stable" as a repository instead of the actual URL.
// For this case, let's give them a suggestion.
containsNonURL := false
for _, repo := range missing {
if !strings.Contains(repo, "//") && !strings.HasPrefix(repo, "@") && !strings.HasPrefix(repo, "alias:") {
containsNonURL = true
}
}
if containsNonURL {
errorMessage += `
Note that repositories must be URLs or aliases. For example, to refer to the "example"
repository, use "https://charts.example.com/" or "@example" instead of
"example". Don't forget to add the repo, too ('helm repo add').`
}
return nil, errors.New(errorMessage)
} }
return reposMap, nil return reposMap, nil
} }

@ -360,3 +360,32 @@ func TestBuild_WithRepositoryAlias(t *testing.T) {
Repository: "@test", Repository: "@test",
}) })
} }
func TestErrRepoNotFound_Error(t *testing.T) {
type fields struct {
Repos []string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "OK",
fields: fields{
Repos: []string{"https://charts1.example.com", "https://charts2.example.com"},
},
want: "no repository definition for https://charts1.example.com, https://charts2.example.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := ErrRepoNotFound{
Repos: tt.fields.Repos,
}
if got := e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}
})
}
}

Loading…
Cancel
Save