Implement helm repo import command

Signed-off-by: Itai Spiegel <itai.spiegel@gmail.com>
pull/12557/head
Itai Spiegel 2 years ago
parent 7f4c6ae643
commit ca7415b731
No known key found for this signature in database
GPG Key ID: FCFA89345CC08751

@ -18,38 +18,81 @@ package main
import ( import (
"io" "io"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/yaml"
"helm.sh/helm/v3/cmd/helm/require" "helm.sh/helm/v3/cmd/helm/require"
"helm.sh/helm/v3/pkg/repo"
) )
type repoImportOptions struct { type repoImportOptions struct {
filePath string importedFilePath string
repoFile string
repoCache string
} }
func newRepoImportCmd(out io.Writer) *cobra.Command { func newRepoImportCmd(out io.Writer) *cobra.Command {
o := &repoImportOptions{} o := &repoImportOptions{}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "import [NAME]", Use: "import [PATH]",
Aliases: []string{"im"},
Short: "import chart repositories", Short: "import chart repositories",
Args: require.ExactArgs(1), Args: require.ExactArgs(1),
ValidArgsFunction: noCompletions, // FIXME ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
// Allow file completion when completing the argument for the directory
return nil, cobra.ShellCompDirectiveDefault
}
// No more completions, so disable file completion
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
o.filePath = args[0] o.importedFilePath = args[0]
o.repoFile = settings.RepositoryConfig
o.repoCache = settings.RepositoryCache
return o.run(out) return o.run(out)
}, },
} }
f := cmd.Flags() f := cmd.Flags()
f.StringVar(&o.filePath, "file", "", "path to the file to import") f.StringVar(&o.importedFilePath, "file", "", "path to the file to import")
return cmd return cmd
} }
func (o *repoImportOptions) run(out io.Writer) error { func (o *repoImportOptions) run(out io.Writer) error {
out.Write([]byte("repo import called\n")) var helmRepoEntries []repo.Entry
fileContent, err := os.ReadFile(o.importedFilePath)
if err != nil {
return err
}
err = yaml.UnmarshalStrict(fileContent, &helmRepoEntries)
if err != nil {
return errors.Errorf("%s is an invalid YAML file", o.importedFilePath)
}
for i := range helmRepoEntries {
helmRepoEntry := helmRepoEntries[i]
info := repoAddOptions{
name: helmRepoEntry.Name,
url: helmRepoEntry.URL,
username: helmRepoEntry.Username,
password: helmRepoEntry.Password,
passCredentialsAll: helmRepoEntry.PassCredentialsAll,
certFile: helmRepoEntry.CertFile,
keyFile: helmRepoEntry.KeyFile,
caFile: helmRepoEntry.CAFile,
insecureSkipTLSverify: helmRepoEntry.InsecureSkipTLSverify,
repoFile: o.repoFile,
repoCache: o.repoCache}
err = info.run(out)
if err != nil {
return err
}
}
return nil return nil
} }

Loading…
Cancel
Save