1/* 2Copyright The Helm Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package main 18 19import ( 20 "io" 21 "strings" 22 23 "github.com/gosuri/uitable" 24 "github.com/pkg/errors" 25 "github.com/spf13/cobra" 26 27 "helm.sh/helm/v3/cmd/helm/require" 28 "helm.sh/helm/v3/pkg/cli/output" 29 "helm.sh/helm/v3/pkg/repo" 30) 31 32func newRepoListCmd(out io.Writer) *cobra.Command { 33 var outfmt output.Format 34 cmd := &cobra.Command{ 35 Use: "list", 36 Aliases: []string{"ls"}, 37 Short: "list chart repositories", 38 Args: require.NoArgs, 39 RunE: func(cmd *cobra.Command, args []string) error { 40 f, err := repo.LoadFile(settings.RepositoryConfig) 41 if isNotExist(err) || len(f.Repositories) == 0 { 42 return errors.New("no repositories to show") 43 } 44 45 return outfmt.Write(out, &repoListWriter{f.Repositories}) 46 }, 47 } 48 49 bindOutputFlag(cmd, &outfmt) 50 51 return cmd 52} 53 54type repositoryElement struct { 55 Name string `json:"name"` 56 URL string `json:"url"` 57} 58 59type repoListWriter struct { 60 repos []*repo.Entry 61} 62 63func (r *repoListWriter) WriteTable(out io.Writer) error { 64 table := uitable.New() 65 table.AddRow("NAME", "URL") 66 for _, re := range r.repos { 67 table.AddRow(re.Name, re.URL) 68 } 69 return output.EncodeTable(out, table) 70} 71 72func (r *repoListWriter) WriteJSON(out io.Writer) error { 73 return r.encodeByFormat(out, output.JSON) 74} 75 76func (r *repoListWriter) WriteYAML(out io.Writer) error { 77 return r.encodeByFormat(out, output.YAML) 78} 79 80func (r *repoListWriter) encodeByFormat(out io.Writer, format output.Format) error { 81 // Initialize the array so no results returns an empty array instead of null 82 repolist := make([]repositoryElement, 0, len(r.repos)) 83 84 for _, re := range r.repos { 85 repolist = append(repolist, repositoryElement{Name: re.Name, URL: re.URL}) 86 } 87 88 switch format { 89 case output.JSON: 90 return output.EncodeJSON(out, repolist) 91 case output.YAML: 92 return output.EncodeYAML(out, repolist) 93 } 94 95 // Because this is a non-exported function and only called internally by 96 // WriteJSON and WriteYAML, we shouldn't get invalid types 97 return nil 98} 99 100// Returns all repos from repos, except those with names matching ignoredRepoNames 101// Inspired by https://stackoverflow.com/a/28701031/893211 102func filterRepos(repos []*repo.Entry, ignoredRepoNames []string) []*repo.Entry { 103 // if ignoredRepoNames is nil, just return repo 104 if ignoredRepoNames == nil { 105 return repos 106 } 107 108 filteredRepos := make([]*repo.Entry, 0) 109 110 ignored := make(map[string]bool, len(ignoredRepoNames)) 111 for _, repoName := range ignoredRepoNames { 112 ignored[repoName] = true 113 } 114 115 for _, repo := range repos { 116 if _, removed := ignored[repo.Name]; !removed { 117 filteredRepos = append(filteredRepos, repo) 118 } 119 } 120 121 return filteredRepos 122} 123 124// Provide dynamic auto-completion for repo names 125func compListRepos(prefix string, ignoredRepoNames []string) []string { 126 var rNames []string 127 128 f, err := repo.LoadFile(settings.RepositoryConfig) 129 if err == nil && len(f.Repositories) > 0 { 130 filteredRepos := filterRepos(f.Repositories, ignoredRepoNames) 131 for _, repo := range filteredRepos { 132 if strings.HasPrefix(repo.Name, prefix) { 133 rNames = append(rNames, repo.Name) 134 } 135 } 136 } 137 return rNames 138} 139