github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/get/get_teamrole.go (about)

     1  package get
     2  
     3  import (
     4  	"github.com/jenkins-x/jx-logging/pkg/log"
     5  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
     7  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
     8  	"github.com/olli-ai/jx/v2/pkg/kube"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  // GetTeamRoleOptions containers the CLI options
    13  type GetTeamRoleOptions struct {
    14  	Options
    15  }
    16  
    17  var (
    18  	getTeamRoleLong = templates.LongDesc(`
    19  		Display the roles for members of a Team
    20  `)
    21  
    22  	getTeamRoleExample = templates.Examples(`
    23  		# List the team roles for the current team
    24  		jx get teamrole
    25  
    26  	`)
    27  )
    28  
    29  // NewCmdGetTeamRole creates the new command for: jx get env
    30  func NewCmdGetTeamRole(commonOpts *opts.CommonOptions) *cobra.Command {
    31  	options := &GetTeamRoleOptions{
    32  		Options: Options{
    33  			CommonOptions: commonOpts,
    34  		},
    35  	}
    36  	cmd := &cobra.Command{
    37  		Use:     "teamroles",
    38  		Short:   "Display the Team or Teams the current user is a member of",
    39  		Aliases: []string{"teamrole"},
    40  		Long:    getTeamRoleLong,
    41  		Example: getTeamRoleExample,
    42  		Run: func(cmd *cobra.Command, args []string) {
    43  			options.Cmd = cmd
    44  			options.Args = args
    45  			err := options.Run()
    46  			helper.CheckErr(err)
    47  		},
    48  	}
    49  	options.AddGetFlags(cmd)
    50  	return cmd
    51  }
    52  
    53  // Run implements this command
    54  func (o *GetTeamRoleOptions) Run() error {
    55  	kubeClient, ns, err := o.KubeClientAndDevNamespace()
    56  	if err != nil {
    57  		return err
    58  	}
    59  	teamRoles, names, err := kube.GetTeamRoles(kubeClient, ns)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	if len(teamRoles) == 0 {
    64  		log.Logger().Info(`
    65  There are no Team roles defined so far!
    66  `)
    67  		return nil
    68  	}
    69  
    70  	table := o.CreateTable()
    71  	table.AddRow("NAME", "TITLE", "DESCRIPTION")
    72  	for _, name := range names {
    73  		title := ""
    74  		description := ""
    75  		teamRole := teamRoles[name]
    76  		if teamRole != nil {
    77  			ann := teamRole.Annotations
    78  			if ann != nil {
    79  				title = ann[kube.AnnotationTitle]
    80  				description = ann[kube.AnnotationDescription]
    81  			}
    82  		}
    83  		table.AddRow(name, title, description)
    84  	}
    85  	table.Render()
    86  	return nil
    87  }