github.com/ryanhartje/helm@v3.0.0-beta.3+incompatible/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/cmd/helm/require" 28 "helm.sh/helm/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 RunE: func(cmd *cobra.Command, args []string) error { 57 o.dir = args[0] 58 return o.run(out) 59 }, 60 } 61 62 f := cmd.Flags() 63 f.StringVar(&o.url, "url", "", "url of chart repository") 64 f.StringVar(&o.merge, "merge", "", "merge the generated index into the given index") 65 66 return cmd 67 } 68 69 func (i *repoIndexOptions) run(out io.Writer) error { 70 path, err := filepath.Abs(i.dir) 71 if err != nil { 72 return err 73 } 74 75 return index(path, i.url, i.merge) 76 } 77 78 func index(dir, url, mergeTo string) error { 79 out := filepath.Join(dir, "index.yaml") 80 81 i, err := repo.IndexDirectory(dir, url) 82 if err != nil { 83 return err 84 } 85 if mergeTo != "" { 86 // if index.yaml is missing then create an empty one to merge into 87 var i2 *repo.IndexFile 88 if _, err := os.Stat(mergeTo); os.IsNotExist(err) { 89 i2 = repo.NewIndexFile() 90 i2.WriteFile(mergeTo, 0644) 91 } else { 92 i2, err = repo.LoadIndexFile(mergeTo) 93 if err != nil { 94 return errors.Wrap(err, "merge failed") 95 } 96 } 97 i.Merge(i2) 98 } 99 i.SortEntries() 100 return i.WriteFile(out, 0644) 101 }