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