github.com/loafoe/helm@v1.0.1/cmd/helm/repo_index.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  	"os"
    22  	"path/filepath"
    23  
    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/repo"
    29  )
    30  
    31  const repoIndexDesc = `
    32  Read the current directory and generate an index file based on the charts found.
    33  
    34  This tool is used for creating an 'index.yaml' file for a chart repository. To
    35  set an absolute URL to the charts, use '--url' flag.
    36  
    37  To merge the generated index with an existing index file, use the '--merge'
    38  flag. In this case, the charts found in the current directory will be merged
    39  into the existing index, with local charts taking priority over existing charts.
    40  `
    41  
    42  type repoIndexOptions struct {
    43  	dir   string
    44  	url   string
    45  	merge string
    46  }
    47  
    48  func newRepoIndexCmd(out io.Writer) *cobra.Command {
    49  	o := &repoIndexOptions{}
    50  
    51  	cmd := &cobra.Command{
    52  		Use:   "index [DIR]",
    53  		Short: "generate an index file given a directory containing packaged charts",
    54  		Long:  repoIndexDesc,
    55  		Args:  require.ExactArgs(1),
    56  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    57  			if len(args) == 0 {
    58  				// Allow file completion when completing the argument for the directory
    59  				return nil, cobra.ShellCompDirectiveDefault
    60  			}
    61  			// No more completions, so disable file completion
    62  			return nil, cobra.ShellCompDirectiveNoFileComp
    63  		},
    64  		RunE: func(cmd *cobra.Command, args []string) error {
    65  			o.dir = args[0]
    66  			return o.run(out)
    67  		},
    68  	}
    69  
    70  	f := cmd.Flags()
    71  	f.StringVar(&o.url, "url", "", "url of chart repository")
    72  	f.StringVar(&o.merge, "merge", "", "merge the generated index into the given index")
    73  
    74  	return cmd
    75  }
    76  
    77  func (i *repoIndexOptions) run(out io.Writer) error {
    78  	path, err := filepath.Abs(i.dir)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	return index(path, i.url, i.merge)
    84  }
    85  
    86  func index(dir, url, mergeTo string) error {
    87  	out := filepath.Join(dir, "index.yaml")
    88  
    89  	i, err := repo.IndexDirectory(dir, url)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	if mergeTo != "" {
    94  		// if index.yaml is missing then create an empty one to merge into
    95  		var i2 *repo.IndexFile
    96  		if _, err := os.Stat(mergeTo); os.IsNotExist(err) {
    97  			i2 = repo.NewIndexFile()
    98  			i2.WriteFile(mergeTo, 0644)
    99  		} else {
   100  			i2, err = repo.LoadIndexFile(mergeTo)
   101  			if err != nil {
   102  				return errors.Wrap(err, "merge failed")
   103  			}
   104  		}
   105  		i.Merge(i2)
   106  	}
   107  	i.SortEntries()
   108  	return i.WriteFile(out, 0644)
   109  }