github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/repo_index.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"fmt"
    21  	"io"
    22  	"path/filepath"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"k8s.io/helm/pkg/repo"
    27  )
    28  
    29  const repoIndexDesc = `
    30  Read the current directory and generate an index file based on the charts found.
    31  
    32  This tool is used for creating an 'index.yaml' file for a chart repository. To
    33  set an absolute URL to the charts, use '--url' flag.
    34  
    35  To merge the generated index with an existing index file, use the '--merge'
    36  flag. In this case, the charts found in the current directory will be merged
    37  into the existing index, with local charts taking priority over existing charts.
    38  `
    39  
    40  type repoIndexCmd struct {
    41  	dir   string
    42  	url   string
    43  	out   io.Writer
    44  	merge string
    45  }
    46  
    47  func newRepoIndexCmd(out io.Writer) *cobra.Command {
    48  	index := &repoIndexCmd{
    49  		out: out,
    50  	}
    51  
    52  	cmd := &cobra.Command{
    53  		Use:   "index [flags] [DIR]",
    54  		Short: "generate an index file given a directory containing packaged charts",
    55  		Long:  repoIndexDesc,
    56  		RunE: func(cmd *cobra.Command, args []string) error {
    57  			if err := checkArgsLength(len(args), "path to a directory"); err != nil {
    58  				return err
    59  			}
    60  
    61  			index.dir = args[0]
    62  
    63  			return index.run()
    64  		},
    65  	}
    66  
    67  	f := cmd.Flags()
    68  	f.StringVar(&index.url, "url", "", "url of chart repository")
    69  	f.StringVar(&index.merge, "merge", "", "merge the generated index into the given index")
    70  
    71  	return cmd
    72  }
    73  
    74  func (i *repoIndexCmd) run() error {
    75  	path, err := filepath.Abs(i.dir)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	return index(path, i.url, i.merge)
    81  }
    82  
    83  func index(dir, url, mergeTo string) error {
    84  	out := filepath.Join(dir, "index.yaml")
    85  
    86  	i, err := repo.IndexDirectory(dir, url)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	if mergeTo != "" {
    91  		i2, err := repo.LoadIndexFile(mergeTo)
    92  		if err != nil {
    93  			return fmt.Errorf("Merge failed: %s", err)
    94  		}
    95  		i.Merge(i2)
    96  	}
    97  	i.SortEntries()
    98  	return i.WriteFile(out, 0755)
    99  }