github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/accounts/describe.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package accounts
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  
    26  	"github.com/spf13/cobra"
    27  	"k8s.io/cli-runtime/pkg/genericiooptions"
    28  	"k8s.io/klog/v2"
    29  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    30  
    31  	"github.com/1aal/kubeblocks/pkg/lorry/client"
    32  )
    33  
    34  type DescribeUserOptions struct {
    35  	*AccountBaseOptions
    36  	userName string
    37  }
    38  
    39  func NewDescribeUserOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *DescribeUserOptions {
    40  	return &DescribeUserOptions{
    41  		AccountBaseOptions: NewAccountBaseOptions(f, streams),
    42  	}
    43  }
    44  
    45  func (o *DescribeUserOptions) AddFlags(cmd *cobra.Command) {
    46  	o.AccountBaseOptions.AddFlags(cmd)
    47  	cmd.Flags().StringVar(&o.userName, "name", "", "Required user name, please specify it.")
    48  	_ = cmd.MarkFlagRequired("name")
    49  }
    50  
    51  func (o DescribeUserOptions) Validate(args []string) error {
    52  	if err := o.AccountBaseOptions.Validate(args); err != nil {
    53  		return err
    54  	}
    55  	if len(o.userName) == 0 {
    56  		return errMissingUserName
    57  	}
    58  	return nil
    59  }
    60  
    61  func (o *DescribeUserOptions) Complete(f cmdutil.Factory) error {
    62  	var err error
    63  	if err = o.AccountBaseOptions.Complete(f); err != nil {
    64  		return err
    65  	}
    66  	return err
    67  }
    68  
    69  func (o *DescribeUserOptions) Run(cmd *cobra.Command, f cmdutil.Factory, streams genericiooptions.IOStreams) error {
    70  	klog.V(1).Info(fmt.Sprintf("connect to cluster %s, component %s, instance %s\n", o.ClusterName, o.ComponentName, o.PodName))
    71  	lorryClient, err := client.NewK8sExecClientWithPod(o.Pod)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	user, err := lorryClient.DescribeUser(context.Background(), o.userName)
    77  	if err != nil {
    78  		o.printGeneralInfo("fail", err.Error())
    79  		return err
    80  	}
    81  	o.printRoleInfo([]map[string]any{user})
    82  	return nil
    83  }