-
Notifications
You must be signed in to change notification settings - Fork 787
/
edit_extensionsrepository.go
281 lines (253 loc) · 9.3 KB
/
edit_extensionsrepository.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package cmd
import (
"fmt"
"io"
"strings"
"github.com/ghodss/yaml"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/log"
"gopkg.in/AlecAivazis/survey.v1"
"github.com/jenkins-x/jx/pkg/extensions"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
var (
editExtensionsRepositoryLong = templates.LongDesc(`
Sets Extensions Repository in use
`)
editExtensionsRepositoryExample = templates.Examples(`
# Prompt to select an Extensions Repository from a list or enter your own
jx edit extensionsrepository
"
`)
)
const (
optionExtensionsRepositoryUrl = "url"
optionExtensionsRepositoryGitHub = "github"
optionExtensionsRepositoryHelmChart = "helm-chart"
optionExtensionsRepositoryHelmRepo = "helm-repo"
optionExtensionsRepositoryHelmRepoName = "helm-repo-name"
optionExtensionsRepositoryHelmUsername = "helm-username"
optionExtensionsRepositoryHelmPassword = "helm-password"
other = "Other"
)
type EditExtensionsRepositoryOptions struct {
EditOptions
ExtensionsRepositoryUrl string
ExtensionsRepositoryGitHub string
ExtensionsRepositoryHelmChart string
ExtensionsRepositoryHelmRepo string
ExtensionsRepositoryHelmRepoName string
ExtensionsRepositoryHelmUsername string
ExtensionsRepositoryHelmPassword string
}
func NewCmdEditExtensionsRepository(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &EditExtensionsRepositoryOptions{
EditOptions: EditOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "extensionsrepository",
Short: editExtensionsRepositoryLong,
Example: editExtensionsRepositoryExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.ExtensionsRepositoryUrl, optionExtensionsRepositoryUrl, "", "", "The extensions repository URL to use")
cmd.Flags().StringVarP(&options.ExtensionsRepositoryGitHub, optionExtensionsRepositoryGitHub, "", "", "The extensions repository GitHub repo to use")
cmd.Flags().StringVarP(&options.ExtensionsRepositoryHelmChart, optionExtensionsRepositoryHelmChart, "", "", "The extensions repository Helm Chart to use")
cmd.Flags().StringVarP(&options.ExtensionsRepositoryHelmRepo, optionExtensionsRepositoryHelmRepo, "", "", "The extensions repository Helm Chart Repo to use")
cmd.Flags().StringVarP(&options.ExtensionsRepositoryHelmRepoName, optionExtensionsRepositoryHelmRepoName, "", "", "The extensions repository Helm Chart Repo Name to use")
cmd.Flags().StringVarP(&options.ExtensionsRepositoryHelmUsername, optionExtensionsRepositoryHelmUsername, "", "", "The extensions repository Helm Chart Username to use")
cmd.Flags().StringVarP(&options.ExtensionsRepositoryHelmPassword, optionExtensionsRepositoryHelmPassword, "", "", "The extensions repository Helm Chart Password to use")
// TODO enable this
// options.addCommonFlags(cmd)
return cmd
}
// Run implements the command
func (o *EditExtensionsRepositoryOptions) Run() error {
kubeClient, ns, err := o.KubeClientAndNamespace()
if err != nil {
return err
}
extensionsConfig, err := extensions.GetOrCreateExtensionsConfig(kubeClient, ns)
if err != nil {
return err
}
current := jenkinsv1.ExtensionRepositoryReference{}
var userpass bool
var username string
var password string
knownRepositories := jenkinsv1.ExtensionRepositoryReferenceList{}
err = yaml.Unmarshal([]byte(extensionsConfig.Data[jenkinsv1.ExtensionsConfigKnownRepositories]), &knownRepositories)
if err != nil {
return err
}
if o.ExtensionsRepositoryGitHub != "" {
current.GitHub = o.ExtensionsRepositoryGitHub
} else if o.ExtensionsRepositoryUrl != "" {
current.Url = o.ExtensionsRepositoryUrl
} else if o.ExtensionsRepositoryHelmChart != "" && o.ExtensionsRepositoryHelmRepo != "" && o.ExtensionsRepositoryHelmRepoName != "" {
if o.ExtensionsRepositoryHelmUsername != "" && o.ExtensionsRepositoryHelmPassword != "" {
userpass = true
username = o.ExtensionsRepositoryHelmUsername
password = o.ExtensionsRepositoryHelmPassword
}
current.Chart.Repo = o.ExtensionsRepositoryHelmRepo
current.Chart.RepoName = o.ExtensionsRepositoryHelmRepoName
current.Chart.Name = o.ExtensionsRepositoryHelmChart
} else {
err = yaml.Unmarshal([]byte(extensionsConfig.Data[jenkinsv1.ExtensionsConfigRepository]), ¤t)
if err != nil {
return err
}
askMap := make(map[string]jenkinsv1.ExtensionRepositoryReference)
asks := make([]string, 0)
for _, k := range knownRepositories.Repositories {
if k.Url != "" {
asks = append(asks, k.Url)
askMap[k.Url] = k
} else if k.GitHub != "" {
asks = append(asks, k.GitHub)
askMap[k.GitHub] = k
} else if k.Chart.Name != "" {
msg := fmt.Sprintf("helm chart %s in repo %s", k.Chart.Name, k.Chart.Repo)
asks = append(asks, msg)
askMap[msg] = k
}
}
asks = append(asks, other)
r, err := util.PickName(asks, "Pick the repository to use", "", o.In, o.Out, o.Err)
if err != nil {
return err
}
if r == other {
types := []string{
"URL", "Helm", "GitHub",
}
current = jenkinsv1.ExtensionRepositoryReference{}
t, err := util.PickName(types, "What type of repository?", "", o.In, o.Out, o.Err)
if t == "URL" {
prompt := &survey.Input{
Message: "Extensions Repository URL",
Help: "Enter an Extensions Repository URL to use",
}
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
survey.AskOne(prompt, ¤t.Url, nil, surveyOpts)
} else if t == "GitHub" {
prompt := &survey.Input{
Message: "GitHub org/repo",
Help: "Enter Github org and repo to use e.g. acme/myrepo",
}
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
survey.AskOne(prompt, ¤t.GitHub, nil, surveyOpts)
} else if t == "Helm" {
prompt := &survey.Input{
Message: "Helm Chart Repo Name",
Help: "Enter the Helm Chart Repo Name to use e.g. acme-corp",
}
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
survey.AskOne(prompt, ¤t.Chart.RepoName, nil, surveyOpts)
prompt = &survey.Input{
Message: "Helm Chart Repo",
Help: "Enter the Helm Chart Repo to use e.g. storage.googleapis.com/jenkinsx-chartmuseum",
}
surveyOpts = survey.WithStdio(o.In, o.Out, o.Err)
survey.AskOne(prompt, ¤t.Chart.Repo, nil, surveyOpts)
confirmPrompt := &survey.Confirm{
Message: "Username/Password required?",
Help: "Does the Chart Repo require a username and password?",
Default: true,
}
surveyOpts = survey.WithStdio(o.In, o.Out, o.Err)
survey.AskOne(confirmPrompt, &userpass, nil, surveyOpts)
if userpass {
prompt = &survey.Input{
Message: "Username",
Help: "Enter the Helm Chart Name to use",
}
surveyOpts = survey.WithStdio(o.In, o.Out, o.Err)
survey.AskOne(prompt, &username, nil, surveyOpts)
promptPass := &survey.Password{
Message: "Password",
Help: "Enter the Helm Chart Name to use",
}
surveyOpts = survey.WithStdio(o.In, o.Out, o.Err)
survey.AskOne(promptPass, &password, nil, surveyOpts)
if err != nil {
return err
}
} else {
err := o.addHelmRepoIfMissing(current.Chart.Repo, current.Chart.RepoName, "", "")
if err != nil {
return err
}
}
prompt = &survey.Input{
Message: "Helm Chart Name",
Help: "Enter the Helm Chart Name to use",
}
surveyOpts = survey.WithStdio(o.In, o.Out, o.Err)
survey.AskOne(prompt, ¤t.Chart.Name, nil, surveyOpts)
}
} else {
current = askMap[r]
}
}
cBytes, err := yaml.Marshal(¤t)
if err != nil {
return err
}
extensionsConfig.Data[jenkinsv1.ExtensionsConfigRepository] = string(cBytes)
found := false
for _, r := range knownRepositories.Repositories {
if (r.Chart.Name != "" && r.Chart.Name == current.Chart.Name) || (r.Url != "" && r.Url == current.Url) || (r.GitHub != "" && r.GitHub == current.GitHub) {
found = true
}
}
if !found {
knownRepositories.Repositories = append(knownRepositories.Repositories, current)
kRBytes, err := yaml.Marshal(&knownRepositories)
if err != nil {
return err
}
extensionsConfig.Data[jenkinsv1.ExtensionsConfigKnownRepositories] = string(kRBytes)
}
_, err = kubeClient.CoreV1().ConfigMaps(ns).Update(extensionsConfig)
if err != nil {
return err
}
var msg string
if current.Url != "" {
msg = current.Url
} else if current.GitHub != "" {
msg = fmt.Sprintf("GitHub repo at %s", util.ColorInfo(current.GitHub))
} else if current.Chart.Name != "" {
repoUrl := current.Chart.Repo
if userpass {
chartRepo := strings.TrimPrefix(strings.TrimPrefix(current.Chart.Repo, "https://"), "http://")
repoUrl = fmt.Sprintf("https://%s:%s@%s", username, password, chartRepo)
} else {
repoUrl = fmt.Sprintf("https://%s", current.Chart.Repo)
}
err := o.addHelmRepoIfMissing(repoUrl, current.Chart.RepoName, "", "")
if err != nil {
return err
}
msg = fmt.Sprintf("Chart %s in repo %s", util.ColorInfo(current.Chart.Name), util.ColorInfo(current.Chart.Repo))
}
log.Infof("Set Extensions Repository to %s\n", msg)
return nil
}