github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/team.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/jenkins-x/jx/v2/pkg/cmd/helper" 7 8 "github.com/spf13/cobra" 9 10 "github.com/jenkins-x/jx/v2/pkg/cmd/opts" 11 "github.com/jenkins-x/jx/v2/pkg/cmd/templates" 12 "github.com/jenkins-x/jx/v2/pkg/kube" 13 "k8s.io/client-go/tools/clientcmd" 14 15 "github.com/jenkins-x/jx/v2/pkg/util" 16 "github.com/pkg/errors" 17 ) 18 19 type TeamOptions struct { 20 *opts.CommonOptions 21 } 22 23 var ( 24 teamLong = templates.LongDesc(` 25 Displays or changes the current team. 26 27 For more documentation on Teams see: [https://jenkins-x.io/about/features/#teams](https://jenkins-x.io/about/features/#teams) 28 29 `) 30 teamExample = templates.Examples(` 31 # view the current team 32 jx team -b 33 34 # pick which team to switch to 35 jx team 36 37 # Change the current team to 'cheese' 38 jx team cheese 39 `) 40 ) 41 42 func NewCmdTeam(commonOpts *opts.CommonOptions) *cobra.Command { 43 options := &TeamOptions{ 44 CommonOptions: commonOpts, 45 } 46 cmd := &cobra.Command{ 47 Use: "team", 48 Aliases: []string{"env"}, 49 Short: "View or change the current team in the current Kubernetes cluster", 50 Long: teamLong, 51 Example: teamExample, 52 Run: func(cmd *cobra.Command, args []string) { 53 options.Cmd = cmd 54 options.Args = args 55 err := options.Run() 56 helper.CheckErr(err) 57 }, 58 } 59 return cmd 60 } 61 62 func (o *TeamOptions) Run() error { 63 kubeClient, currentTeam, err := o.KubeClientAndNamespace() 64 if err != nil { 65 return err 66 } 67 apisClient, err := o.ApiExtensionsClient() 68 if err != nil { 69 return err 70 } 71 err = kube.RegisterEnvironmentCRD(apisClient) 72 if err != nil { 73 return err 74 } 75 _, teamNames, err := kube.GetTeams(kubeClient) 76 if err != nil { 77 return err 78 } 79 80 config, po, err := o.Kube().LoadConfig() 81 if err != nil { 82 return err 83 } 84 team := "" 85 args := o.Args 86 if len(args) > 0 { 87 team = args[0] 88 } 89 if team == "" && !o.BatchMode { 90 pick, err := util.PickName(teamNames, "Pick Team: ", "", o.GetIOFileHandles()) 91 if err != nil { 92 return err 93 } 94 team = pick 95 } 96 info := util.ColorInfo 97 if team != "" && team != currentTeam { 98 newConfig := *config 99 ctx := kube.CurrentContext(config) 100 if ctx == nil { 101 return errors.New("there is no context defined in your Kubernetes configuration") 102 } 103 if ctx.Namespace == team { 104 return nil 105 } 106 ctx.Namespace = team 107 err = clientcmd.ModifyConfig(po, newConfig, false) 108 if err != nil { 109 return fmt.Errorf("Failed to update the kube config %s", err) 110 } 111 fmt.Fprintf(o.Out, "Now using team '%s' on server '%s'.\n", info(team), info(kube.Server(config, ctx))) 112 } else { 113 ns := kube.CurrentNamespace(config) 114 server := kube.CurrentServer(config) 115 if team == "" { 116 team = currentTeam 117 } 118 if team == "" { 119 fmt.Fprintf(o.Out, "Using namespace '%s' from context named '%s' on server '%s'.\n", info(ns), info(config.CurrentContext), info(server)) 120 } else { 121 fmt.Fprintf(o.Out, "Using team '%s' on server '%s'.\n", info(team), info(server)) 122 } 123 } 124 return nil 125 }