diff --git a/cmd/helm/dependency_build.go b/cmd/helm/dependency_build.go index a610fb911..4e87684ce 100644 --- a/cmd/helm/dependency_build.go +++ b/cmd/helm/dependency_build.go @@ -16,12 +16,11 @@ limitations under the License. package main import ( + "fmt" "io" "os" "path/filepath" - "strings" - "github.com/pkg/errors" "github.com/spf13/cobra" "k8s.io/client-go/util/homedir" @@ -69,7 +68,7 @@ func newDependencyBuildCmd(out io.Writer) *cobra.Command { } err := man.Build() 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 }, diff --git a/pkg/downloader/manager.go b/pkg/downloader/manager.go index 1479cc9a6..d2adfbc78 100644 --- a/pkg/downloader/manager.go +++ b/pkg/downloader/manager.go @@ -48,6 +48,7 @@ type ErrRepoNotFound struct { Repos []string } +// Error implements the error interface. func (e ErrRepoNotFound) Error() string { 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 { repository := dd.Repository // Add if URL - _, err := url.ParseRequestURI(repository) - if err == nil { - reposMap[repository] = repository - continue + if _, err := url.ParseRequestURI(repository); err != nil { + return nil, err } missing = append(missing, repository) } } if len(missing) > 0 { - errorMessage := fmt.Sprintf("no repository definition for %s. Please add them via 'helm repo add'", strings.Join(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 nil, ErrRepoNotFound{missing} } return reposMap, nil } diff --git a/pkg/downloader/manager_test.go b/pkg/downloader/manager_test.go index ea235c13f..dd3cbbd7b 100644 --- a/pkg/downloader/manager_test.go +++ b/pkg/downloader/manager_test.go @@ -360,3 +360,32 @@ func TestBuild_WithRepositoryAlias(t *testing.T) { 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) + } + }) + } +}