github.com/latiif/helm@v2.15.0+incompatible/cmd/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  package main
    18  
    19  import (
    20  	"io"
    21  
    22  	"github.com/gosuri/uitable"
    23  	"github.com/spf13/cobra"
    24  
    25  	"k8s.io/helm/pkg/helm/helmpath"
    26  	"k8s.io/helm/pkg/repo"
    27  )
    28  
    29  type repoListCmd struct {
    30  	out    io.Writer
    31  	home   helmpath.Home
    32  	output string
    33  }
    34  
    35  type repositoryElement struct {
    36  	Name string
    37  	URL  string
    38  }
    39  
    40  func newRepoListCmd(out io.Writer) *cobra.Command {
    41  	list := &repoListCmd{out: out}
    42  
    43  	cmd := &cobra.Command{
    44  		Use:   "list [flags]",
    45  		Short: "List chart repositories",
    46  		RunE: func(cmd *cobra.Command, args []string) error {
    47  			list.home = settings.Home
    48  			return list.run()
    49  		},
    50  	}
    51  
    52  	bindOutputFlag(cmd, &list.output)
    53  	return cmd
    54  }
    55  
    56  func (a *repoListCmd) run() error {
    57  	repoFile, err := repo.LoadRepositoriesFile(a.home.RepositoryFile())
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	return write(a.out, &repoListWriter{repoFile.Repositories}, outputFormat(a.output))
    63  }
    64  
    65  //////////// Printer implementation below here
    66  type repoListWriter struct {
    67  	repos []*repo.Entry
    68  }
    69  
    70  func (r *repoListWriter) WriteTable(out io.Writer) error {
    71  	table := uitable.New()
    72  	table.AddRow("NAME", "URL")
    73  	for _, re := range r.repos {
    74  		table.AddRow(re.Name, re.URL)
    75  	}
    76  	return encodeTable(out, table)
    77  }
    78  
    79  func (r *repoListWriter) WriteJSON(out io.Writer) error {
    80  	return r.encodeByFormat(out, outputJSON)
    81  }
    82  
    83  func (r *repoListWriter) WriteYAML(out io.Writer) error {
    84  	return r.encodeByFormat(out, outputYAML)
    85  }
    86  
    87  func (r *repoListWriter) encodeByFormat(out io.Writer, format outputFormat) error {
    88  	var repolist []repositoryElement
    89  
    90  	for _, re := range r.repos {
    91  		repolist = append(repolist, repositoryElement{Name: re.Name, URL: re.URL})
    92  	}
    93  
    94  	switch format {
    95  	case outputJSON:
    96  		return encodeJSON(out, repolist)
    97  	case outputYAML:
    98  		return encodeYAML(out, repolist)
    99  	}
   100  
   101  	// Because this is a non-exported function and only called internally by
   102  	// WriteJSON and WriteYAML, we shouldn't get invalid types
   103  	return nil
   104  }