From a623b610ef571d2691b48a3c79b07a204bcf86bf Mon Sep 17 00:00:00 2001 From: dingben Date: Mon, 20 Nov 2023 09:14:41 +0800 Subject: [PATCH] chore: move init addon repo in index cmd (#57) --- docs/user_docs/cli/kbcli_plugin_index_add.md | 4 +++- pkg/cmd/addon/index.go | 16 ++++++++++------ pkg/cmd/plugin/index.go | 4 +++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/docs/user_docs/cli/kbcli_plugin_index_add.md b/docs/user_docs/cli/kbcli_plugin_index_add.md index 052b81627..66ae11dc3 100644 --- a/docs/user_docs/cli/kbcli_plugin_index_add.md +++ b/docs/user_docs/cli/kbcli_plugin_index_add.md @@ -12,7 +12,9 @@ kbcli plugin index add [flags] ``` # Add a new plugin index - kbcli plugin index add myIndex + kbcli plugin index add default https://github.com/apecloud/block-index.git + + kbcli plugin index add krew https://github.com/kubernetes-sigs/krew-index.git ``` ### Options diff --git a/pkg/cmd/addon/index.go b/pkg/cmd/addon/index.go index b179ec830..cec8cad38 100644 --- a/pkg/cmd/addon/index.go +++ b/pkg/cmd/addon/index.go @@ -63,9 +63,6 @@ func newIndexAddCmd() *cobra.Command { Long: "Configure a new index to install KubeBlocks addon from.", Example: "kbcli addon index add kubeblocks " + types.DefaultAddonIndexURL, Args: cobra.ExactArgs(2), - PersistentPreRun: func(_ *cobra.Command, _ []string) { - util.CheckErr(addDefaultIndex()) - }, Run: func(_ *cobra.Command, args []string) { util.CheckErr(addIndex(args)) }, @@ -95,6 +92,9 @@ func newIndexCmd(streams genericiooptions.IOStreams) *cobra.Command { Short: "Manage custom addon indexes", Long: "Manage which repositories are used to discover and install addon from.", Args: cobra.NoArgs, + PersistentPreRun: func(_ *cobra.Command, _ []string) { + util.CheckErr(addDefaultIndex()) + }, } indexCmd.AddCommand( newIndexAddCmd(), @@ -211,7 +211,7 @@ func addIndex(args []string) error { } else if err != nil { return err } - return errors.New("index already exists") + return fmt.Errorf("index %s:%s already exists", name, url) } func listIndexes(out io.Writer) error { @@ -261,15 +261,19 @@ func addDefaultIndex() error { return fmt.Errorf("can't get the index dir : %s", err.Error()) } + // check if the default index is already added defaultIndexDir := path.Join(addonDir, types.KubeBlocksReleaseName) - if _, err := os.Stat(defaultIndexDir); err != nil && os.IsNotExist(err) { + if _, err := os.Stat(defaultIndexDir); os.IsNotExist(err) { if err = util.EnsureCloned(types.DefaultAddonIndexURL, defaultIndexDir); err != nil { return err } fmt.Printf("Default addon index \"kubeblocks\" has been added.") return nil + } else if err != nil { + return err } - return fmt.Errorf("default index %s:%s already exists", types.KubeBlocksReleaseName, types.DefaultAddonIndexURL) + + return nil } func getAllIndexes() ([]index, error) { diff --git a/pkg/cmd/plugin/index.go b/pkg/cmd/plugin/index.go index 3e60d19c4..e7927d4a4 100644 --- a/pkg/cmd/plugin/index.go +++ b/pkg/cmd/plugin/index.go @@ -43,7 +43,9 @@ var ( pluginAddIndexExample = templates.Examples(` # Add a new plugin index - kbcli plugin index add myIndex + kbcli plugin index add default https://github.com/apecloud/block-index.git + + kbcli plugin index add krew https://github.com/kubernetes-sigs/krew-index.git `) pluginDeleteIndexExample = templates.Examples(`