Skip to content

Commit

Permalink
refactor: public account describe (#135)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8bbb6b9)
  • Loading branch information
lynnleelhl committed Dec 22, 2023
1 parent 9f70ee1 commit 0c85522
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
32 changes: 22 additions & 10 deletions pkg/cmd/accounts/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -44,40 +45,51 @@ 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
}
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
}
13 changes: 7 additions & 6 deletions pkg/cmd/accounts/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,27 @@ 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() {
o := NewDescribeUserOptions(tf, streams)
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())
})
})
})
7 changes: 4 additions & 3 deletions pkg/cmd/cluster/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0c85522

Please sign in to comment.