mirror of
https://github.com/helm/helm.git
synced 2026-04-23 15:17:05 -04:00
fix(cmd/helm): user friendly error message when repos are not configured
Signed-off-by: Adam Reese <adam@reese.io>
This commit is contained in:
parent
c19253d7cd
commit
fe92480ab4
8 changed files with 27 additions and 23 deletions
|
|
@ -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…
Reference in a new issue