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