github.com/amundsenjunior/helm@v2.8.0-rc.1.0.20180119233529-2b92431476e1+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{out: out} 49 50 cmd := &cobra.Command{ 51 Use: "index [flags] [DIR]", 52 Short: "generate an index file given a directory containing packaged charts", 53 Long: repoIndexDesc, 54 RunE: func(cmd *cobra.Command, args []string) error { 55 if err := checkArgsLength(len(args), "path to a directory"); err != nil { 56 return err 57 } 58 59 index.dir = args[0] 60 61 return index.run() 62 }, 63 } 64 65 f := cmd.Flags() 66 f.StringVar(&index.url, "url", "", "url of chart repository") 67 f.StringVar(&index.merge, "merge", "", "merge the generated index into the given index") 68 69 return cmd 70 } 71 72 func (i *repoIndexCmd) run() error { 73 path, err := filepath.Abs(i.dir) 74 if err != nil { 75 return err 76 } 77 78 return index(path, i.url, i.merge) 79 } 80 81 func index(dir, url, mergeTo string) error { 82 out := filepath.Join(dir, "index.yaml") 83 84 i, err := repo.IndexDirectory(dir, url) 85 if err != nil { 86 return err 87 } 88 if mergeTo != "" { 89 i2, err := repo.LoadIndexFile(mergeTo) 90 if err != nil { 91 return fmt.Errorf("Merge failed: %s", err) 92 } 93 i.Merge(i2) 94 } 95 i.SortEntries() 96 return i.WriteFile(out, 0755) 97 }