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