github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/get/get_team.go (about)

     1  package get
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
     7  
     8  	"github.com/jenkins-x/jx-logging/pkg/log"
     9  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
    10  	"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
    11  	"github.com/jenkins-x/jx/v2/pkg/kube"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  // GetTeamOptions containers the CLI options
    16  type GetTeamOptions struct {
    17  	Options
    18  
    19  	Pending bool
    20  }
    21  
    22  var (
    23  	getTeamLong = templates.LongDesc(`
    24  		Display the Team or Teams a user is a member of.
    25  `)
    26  
    27  	getTeamExample = templates.Examples(`
    28  		# List the provisioned team or teams the current user is a member of
    29  		jx get team
    30  
    31  		# List the pending Teams which are not yet provisioned and available for use
    32  		jx get team -p
    33  	`)
    34  )
    35  
    36  // NewCmdGetTeam creates the new command for: jx get env
    37  func NewCmdGetTeam(commonOpts *opts.CommonOptions) *cobra.Command {
    38  	options := &GetTeamOptions{
    39  		Options: Options{
    40  			CommonOptions: commonOpts,
    41  		},
    42  	}
    43  	cmd := &cobra.Command{
    44  		Use:     "teams",
    45  		Short:   "Display the Team or Teams the current user is a member of",
    46  		Aliases: []string{"team"},
    47  		Long:    getTeamLong,
    48  		Example: getTeamExample,
    49  		Run: func(cmd *cobra.Command, args []string) {
    50  			options.Cmd = cmd
    51  			options.Args = args
    52  			err := options.Run()
    53  			helper.CheckErr(err)
    54  		},
    55  	}
    56  	cmd.Flags().BoolVarP(&options.Pending, "pending", "p", false, "Display only pending Teams which are not yet provisioned yet")
    57  
    58  	options.AddGetFlags(cmd)
    59  	return cmd
    60  }
    61  
    62  // Run implements this command
    63  func (o *GetTeamOptions) Run() error {
    64  	kubeClient, err := o.KubeClient()
    65  	if err != nil {
    66  		return err
    67  	}
    68  	if o.Pending {
    69  		return o.getPendingTeams()
    70  	}
    71  	teams, _, err := kube.GetTeams(kubeClient)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	if len(teams) == 0 {
    76  		log.Logger().Info(`
    77  You do not belong to any teams.
    78  Have you installed Jenkins X yet to create a team?
    79  See https://jenkins-x.io/getting-started/\n for more detail
    80  `)
    81  		return nil
    82  	}
    83  
    84  	table := o.CreateTable()
    85  	table.AddRow("NAME")
    86  	for _, team := range teams {
    87  		table.AddRow(team.Name)
    88  	}
    89  	table.Render()
    90  	return nil
    91  }
    92  
    93  func (o *GetTeamOptions) getPendingTeams() error {
    94  	err := o.RegisterTeamCRD()
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	jxClient, ns, err := o.JXClientAndAdminNamespace()
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	teams, names, err := kube.GetPendingTeams(jxClient, ns)
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	if len(names) == 0 {
   110  		log.Logger().Info(`
   111  There are no pending Teams yet. Try create one via: jx create team --pending
   112  `)
   113  		return nil
   114  	}
   115  
   116  	table := o.CreateTable()
   117  	table.AddRow("NAME", "STATUS", "KIND", "MEMBERS")
   118  	for _, team := range teams {
   119  		spec := &team.Spec
   120  		table.AddRow(team.Name, string(team.Status.ProvisionStatus), string(spec.Kind), strings.Join(spec.Members, ", "))
   121  	}
   122  	table.Render()
   123  	return nil
   124  
   125  }