github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/cmd/tools/helm/repo_list.go (about)

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