From 0c85522700a264432f265886186037d51923b1df Mon Sep 17 00:00:00 2001 From: chantu Date: Fri, 22 Dec 2023 15:37:53 +0800 Subject: [PATCH] refactor: public account describe (#135) (cherry picked from commit 8bbb6b997ebabbab1c4bd92c50389dd4b5ada581) --- pkg/cmd/accounts/describe.go | 32 +++++++++++++++++++++---------- pkg/cmd/accounts/describe_test.go | 13 +++++++------ pkg/cmd/cluster/accounts.go | 7 ++++--- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/pkg/cmd/accounts/describe.go b/pkg/cmd/accounts/describe.go index e91e05041..14d2c66eb 100644 --- a/pkg/cmd/accounts/describe.go +++ b/pkg/cmd/accounts/describe.go @@ -33,7 +33,8 @@ import ( type DescribeUserOptions struct { *AccountBaseOptions - userName string + UserName string + User map[string]interface{} } func NewDescribeUserOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *DescribeUserOptions { @@ -44,21 +45,18 @@ func NewDescribeUserOptions(f cmdutil.Factory, streams genericiooptions.IOStream func (o *DescribeUserOptions) AddFlags(cmd *cobra.Command) { o.AccountBaseOptions.AddFlags(cmd) - cmd.Flags().StringVar(&o.userName, "name", "", "Required user name, please specify it.") + cmd.Flags().StringVar(&o.UserName, "name", "", "Required user name, please specify it.") _ = cmd.MarkFlagRequired("name") } -func (o DescribeUserOptions) Validate(args []string) error { - if err := o.AccountBaseOptions.Validate(args); err != nil { - return err - } - if len(o.userName) == 0 { +func (o DescribeUserOptions) Validate() error { + if len(o.UserName) == 0 { return errMissingUserName } return nil } -func (o *DescribeUserOptions) Complete(f cmdutil.Factory) error { +func (o *DescribeUserOptions) Complete() error { var err error if err = o.AccountBaseOptions.Complete(); err != nil { return err @@ -66,18 +64,32 @@ func (o *DescribeUserOptions) Complete(f cmdutil.Factory) error { return err } -func (o *DescribeUserOptions) Run(cmd *cobra.Command, f cmdutil.Factory, streams genericiooptions.IOStreams) error { +func (o *DescribeUserOptions) Run() error { klog.V(1).Info(fmt.Sprintf("connect to cluster %s, component %s, instance %s\n", o.ClusterName, o.ComponentName, o.PodName)) lorryClient, err := client.NewK8sExecClientWithPod(o.Config, o.Pod) if err != nil { return err } - user, err := lorryClient.DescribeUser(context.Background(), o.userName) + user, err := lorryClient.DescribeUser(context.Background(), o.UserName) if err != nil { o.printGeneralInfo("fail", err.Error()) return err } + o.User = user o.printRoleInfo([]map[string]any{user}) return nil } + +func (o *DescribeUserOptions) Exec() error { + if err := o.Validate(); err != nil { + return err + } + if err := o.Complete(); err != nil { + return err + } + if err := o.Run(); err != nil { + return err + } + return nil +} diff --git a/pkg/cmd/accounts/describe_test.go b/pkg/cmd/accounts/describe_test.go index c334dbc5d..0ad7e3e94 100644 --- a/pkg/cmd/accounts/describe_test.go +++ b/pkg/cmd/accounts/describe_test.go @@ -90,16 +90,17 @@ var _ = Describe("Describe Account Options", func() { o := NewDescribeUserOptions(tf, streams) Expect(o).ShouldNot(BeNil()) args := []string{} - Expect(o.Validate(args)).Should(MatchError(errClusterNameorInstName)) + Expect(o.AccountBaseOptions.Validate(args)).Should(MatchError(errClusterNameorInstName)) // add one element By("add one more args, should fail") args = []string{clusterName} - Expect(o.Validate(args)).Should(MatchError(errMissingUserName)) + Expect(o.AccountBaseOptions.Validate(args)).Should(Succeed()) + Expect(o.Validate()).Should(MatchError(errMissingUserName)) // set user name - o.userName = "like" - Expect(o.Validate(args)).Should(Succeed()) + o.UserName = "like" + Expect(o.Validate()).Should(Succeed()) }) It("complete options", func() { @@ -107,9 +108,9 @@ var _ = Describe("Describe Account Options", func() { Expect(o).ShouldNot(BeNil()) o.ClusterName = clusterName o.PodName = pods.Items[0].Name - o.userName = "you" + o.UserName = "you" - Expect(o.Complete(tf)).Should(Succeed()) + Expect(o.Complete()).Should(Succeed()) }) }) }) diff --git a/pkg/cmd/cluster/accounts.go b/pkg/cmd/cluster/accounts.go index 0977149d5..5f6798377 100644 --- a/pkg/cmd/cluster/accounts.go +++ b/pkg/cmd/cluster/accounts.go @@ -127,9 +127,10 @@ func NewDescAccountCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *c Example: descUserExamples, ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()), Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate(args)) - cmdutil.CheckErr(o.Complete(f)) - cmdutil.CheckErr(o.Run(cmd, f, streams)) + cmdutil.CheckErr(o.AccountBaseOptions.Validate(args)) + cmdutil.CheckErr(o.Validate()) + cmdutil.CheckErr(o.Complete()) + cmdutil.CheckErr(o.Run()) }, } o.AddFlags(cmd)