Merge pull request #6308 from adamreese/fix/repos

fix(cmd/helm): user friendly error message when repos are not configured
pull/6320/head v3.0.0-beta.2
Adam Reese 5 years ago committed by GitHub
commit 26c7338408
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,7 +18,9 @@ package main
import (
"io"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"helm.sh/helm/cmd/helm/require"
@ -48,3 +50,7 @@ func newRepoCmd(out io.Writer) *cobra.Command {
return cmd
}
func isNotExist(err error) bool {
return os.IsNotExist(errors.Cause(err))
}

@ -35,10 +35,7 @@ func newRepoListCmd(out io.Writer) *cobra.Command {
Args: require.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
f, err := repo.LoadFile(settings.RepositoryConfig)
if err != nil {
return err
}
if len(f.Repositories) == 0 {
if isNotExist(err) || len(f.Repositories) == 0 {
return errors.New("no repositories to show")
}
table := uitable.New()

@ -56,8 +56,8 @@ func newRepoRemoveCmd(out io.Writer) *cobra.Command {
func (o *repoRemoveOptions) run(out io.Writer) error {
r, err := repo.LoadFile(o.repoFile)
if err != nil {
return err
if isNotExist(err) || len(r.Repositories) == 0 {
return errors.New("no repositories configured")
}
if !r.Remove(o.name) {

@ -60,11 +60,7 @@ func newRepoUpdateCmd(out io.Writer) *cobra.Command {
func (o *repoUpdateOptions) run(out io.Writer) error {
f, err := repo.LoadFile(o.repoFile)
if err != nil {
return err
}
if len(f.Repositories) == 0 {
if isNotExist(err) || len(f.Repositories) == 0 {
return errNoRepositories
}
var repos []*repo.ChartRepository

@ -147,8 +147,8 @@ func (o *searchRepoOptions) formatSearchResults(res []*search.Result) string {
func (o *searchRepoOptions) buildIndex(out io.Writer) (*search.Index, error) {
// Load the repositories.yaml
rf, err := repo.LoadFile(o.repoFile)
if err != nil {
return nil, errors.Wrap(err, "loading repository config")
if isNotExist(err) || len(rf.Repositories) == 0 {
return nil, errors.New("no repositories configured")
}
i := search.NewIndex()

@ -154,7 +154,7 @@ func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, er
}
c.Options = append(c.Options, getter.WithURL(ref))
rf, err := repo.LoadFile(c.RepositoryConfig)
rf, err := loadRepoConfig(c.RepositoryConfig)
if err != nil {
return u, err
}
@ -354,3 +354,11 @@ func (c *ChartDownloader) scanReposForURL(u string, rf *repo.File) (*repo.Entry,
// This means that there is no repo file for the given URL.
return nil, ErrNoOwnerRepo
}
func loadRepoConfig(file string) (*repo.File, error) {
r, err := repo.LoadFile(file)
if err != nil && !os.IsNotExist(errors.Cause(err)) {
return nil, err
}
return r, nil
}

@ -311,7 +311,7 @@ func (m *Manager) safeDeleteDep(name, dir string) error {
// hasAllRepos ensures that all of the referenced deps are in the local repo cache.
func (m *Manager) hasAllRepos(deps []*chart.Dependency) error {
rf, err := repo.LoadFile(m.RepositoryConfig)
rf, err := loadRepoConfig(m.RepositoryConfig)
if err != nil {
return err
}
@ -345,7 +345,7 @@ Loop:
// getRepoNames returns the repo names of the referenced deps which can be used to fetch the cahced index file.
func (m *Manager) getRepoNames(deps []*chart.Dependency) (map[string]string, error) {
rf, err := repo.LoadFile(m.RepositoryConfig)
rf, err := loadRepoConfig(m.RepositoryConfig)
if err != nil {
if os.IsNotExist(err) {
return make(map[string]string), nil
@ -415,7 +415,7 @@ repository, use "https://charts.example.com/" or "@example" instead of
// UpdateRepositories updates all of the local repos to the latest.
func (m *Manager) UpdateRepositories() error {
rf, err := repo.LoadFile(m.RepositoryConfig)
rf, err := loadRepoConfig(m.RepositoryConfig)
if err != nil {
return err
}
@ -553,7 +553,7 @@ func (m *Manager) loadChartRepositories() (map[string]*repo.ChartRepository, err
indices := map[string]*repo.ChartRepository{}
// Load repositories.yaml file
rf, err := repo.LoadFile(m.RepositoryConfig)
rf, err := loadRepoConfig(m.RepositoryConfig)
if err != nil {
return indices, errors.Wrapf(err, "failed to load %s", m.RepositoryConfig)
}

@ -46,15 +46,12 @@ func NewFile() *File {
// LoadFile takes a file at the given path and returns a File object
func LoadFile(path string) (*File, error) {
r := new(File)
b, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, errors.Wrapf(err, "couldn't load repositories file (%s)", path)
}
return nil, err
return r, errors.Wrapf(err, "couldn't load repositories file (%s)", path)
}
r := &File{}
err = yaml.Unmarshal(b, r)
return r, err
}

Loading…
Cancel
Save