From 52bbf937bb640bc8735927f18fc4de92de5d5d6a Mon Sep 17 00:00:00 2001 From: barnettZQG Date: Wed, 1 Jun 2022 10:59:02 +0800 Subject: [PATCH] Fix: load the provider subcommands on demand Signed-off-by: barnettZQG --- references/cli/cli.go | 1 - references/cli/provider.go | 36 +++++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/references/cli/cli.go b/references/cli/cli.go index ec6f05d8a..a4946b542 100644 --- a/references/cli/cli.go +++ b/references/cli/cli.go @@ -73,7 +73,6 @@ func NewCommandWithIOStreams(ioStream util.IOStreams) *cobra.Command { } f := velacmd.NewDeferredFactory(config.GetConfig) - _, _ = commandArgs, f if err := system.InitDirs(); err != nil { fmt.Println("InitDir err", err) os.Exit(1) diff --git a/references/cli/provider.go b/references/cli/provider.go index f3a48fcd0..3a5fb9e0d 100644 --- a/references/cli/provider.go +++ b/references/cli/provider.go @@ -20,7 +20,9 @@ import ( "context" "encoding/json" "fmt" + "os" "strings" + "time" "github.com/gosuri/uitable" tcv1beta1 "github.com/oam-dev/terraform-controller/api/v1beta1" @@ -57,12 +59,18 @@ func NewProviderCommand(c common.Args, order string, ioStreams cmdutil.IOStreams }, } add, err := prepareProviderAddCommand(c, ioStreams) - if err == nil { + if err != nil { + ioStreams.Errorf("fail to init the provider command:%s \n", err.Error()) + } + if add != nil { cmd.AddCommand(add) } delete, err := prepareProviderDeleteCommand(c, ioStreams) - if err == nil { + if err != nil { + ioStreams.Errorf("fail to init the provider command:%s \n", err.Error()) + } + if delete != nil { cmd.AddCommand(delete) } @@ -94,7 +102,11 @@ func NewProviderListCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.C } func prepareProviderAddCommand(c common.Args, ioStreams cmdutil.IOStreams) (*cobra.Command, error) { - ctx := context.Background() + if len(os.Args) < 2 || os.Args[1] != "provider" { + return nil, nil + } + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1) + defer cancel() k8sClient, err := c.GetClient() if err != nil { return nil, err @@ -147,7 +159,8 @@ func prepareProviderAddCommand(c common.Args, ioStreams cmdutil.IOStreams) (*cob } func prepareProviderAddSubCommand(c common.Args, ioStreams cmdutil.IOStreams) ([]*cobra.Command, error) { - ctx := context.Background() + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1) + defer cancel() k8sClient, err := c.GetClient() if err != nil { return nil, err @@ -316,10 +329,8 @@ func getTerraformProviderType(ctx context.Context, k8sClient client.Client, name } func prepareProviderDeleteCommand(c common.Args, ioStreams cmdutil.IOStreams) (*cobra.Command, error) { - ctx := context.Background() - k8sClient, err := c.GetClient() - if err != nil { - return nil, err + if len(os.Args) < 2 || os.Args[1] != "provider" { + return nil, nil } cmd := &cobra.Command{ @@ -337,6 +348,12 @@ func prepareProviderDeleteCommand(c common.Args, ioStreams cmdutil.IOStreams) (* cmd.AddCommand(deleteSubCommands...) cmd.RunE = func(cmd *cobra.Command, args []string) error { + k8sClient, err := c.GetClient() + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1) + defer cancel() defs, err := getTerraformProviderTypes(ctx, k8sClient) if len(args) < 1 { errMsg := "must specify a Terraform Cloud Provider type" @@ -370,7 +387,8 @@ func prepareProviderDeleteCommand(c common.Args, ioStreams cmdutil.IOStreams) (* } func prepareProviderDeleteSubCommand(c common.Args, ioStreams cmdutil.IOStreams) ([]*cobra.Command, error) { - ctx := context.Background() + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1) + defer cancel() k8sClient, err := c.GetClient() if err != nil { return nil, err