-
Notifications
You must be signed in to change notification settings - Fork 787
/
edit_config.go
200 lines (179 loc) · 4.81 KB
/
edit_config.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
package cmd
import (
"fmt"
"io"
"strings"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/log"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/config"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/util"
)
const (
chatKind = "chat"
issueKind = "issues"
wikiKind = "wiki"
)
var (
editConfigLong = templates.LongDesc(`
Edits the project configuration
`)
editConfigExample = templates.Examples(`
# Edit the project configuration for the current directory
jx edit config
`)
configKinds = []string{
chatKind,
issueKind,
wikiKind,
}
)
// EditConfigOptions the options for the create spring command
type EditConfigOptions struct {
EditOptions
Dir string
Kind string
IssuesAuthConfigSvc auth.ConfigService
ChatAuthConfigSvc auth.ConfigService
}
// NewCmdEditConfig creates a command object for the "create" command
func NewCmdEditConfig(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &EditConfigOptions{
EditOptions: EditOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "config",
Short: "Edits the project configuration",
Aliases: []string{"project"},
Long: editConfigLong,
Example: editConfigExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Dir, "dir", "d", "", "The root project directory")
cmd.Flags().StringVarP(&options.Kind, "kind", "k", "", "The kind of configuration to edit root project directory. Possible values "+strings.Join(configKinds, ", "))
return cmd
}
// Run implements the command
func (o *EditConfigOptions) Run() error {
pc, fileName, err := config.LoadProjectConfig(o.Dir)
if err != nil {
return err
}
o.IssuesAuthConfigSvc, err = o.createIssueTrackerAuthConfigService()
if err != nil {
return err
}
o.ChatAuthConfigSvc, err = o.createChatAuthConfigService()
if err != nil {
return err
}
kind := o.Kind
if kind == "" && !o.BatchMode {
kind, err = util.PickRequiredNameWithDefault(configKinds, "Which configuration do you want to edit", issueKind, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
}
if kind == "" {
return fmt.Errorf("No kind option!")
}
if util.StringArrayIndex(configKinds, kind) < 0 {
return util.InvalidOption("kind", kind, configKinds)
}
modified := false
switch kind {
case chatKind:
modified, err = o.EditChat(pc)
case issueKind:
modified, err = o.EditIssueTracker(pc)
default:
return fmt.Errorf("Editing %s is not yet supported!", kind)
}
if err != nil {
return err
}
if modified {
err = pc.SaveConfig(fileName)
if err != nil {
return err
}
log.Infof("Saved project configuration %s\n", util.ColorInfo(fileName))
}
return nil
}
func (o *EditConfigOptions) EditIssueTracker(pc *config.ProjectConfig) (bool, error) {
answer := false
if pc.IssueTracker == nil {
pc.IssueTracker = &config.IssueTrackerConfig{}
answer = true
}
it := pc.IssueTracker
config := o.IssuesAuthConfigSvc.Config()
if len(config.Servers) == 0 {
return answer, fmt.Errorf("No issue tracker servers available. Please add one via: jx create tracker server")
}
server, err := config.PickServer("Issue tracker service", o.BatchMode, o.In, o.Out, o.Err)
if err != nil {
return answer, err
}
if server == nil || server.URL == "" {
return answer, fmt.Errorf("No issue tracker server URL found!")
}
it.URL = server.URL
if server.Kind != "" {
it.Kind = server.Kind
}
answer = true
it.Project, err = util.PickValue("Issue tracker project name: ", it.Project, true, "", o.In, o.Out, o.Err)
if err != nil {
return answer, err
}
return answer, nil
}
func (o *EditConfigOptions) EditChat(pc *config.ProjectConfig) (bool, error) {
answer := false
if pc.Chat == nil {
pc.Chat = &config.ChatConfig{}
answer = true
}
it := pc.Chat
config := o.ChatAuthConfigSvc.Config()
if len(config.Servers) == 0 {
return answer, fmt.Errorf("No chat servers available. Please add one via: jx create chat server")
}
server, err := config.PickServer("Chat service", o.BatchMode, o.In, o.Out, o.Err)
if err != nil {
return answer, err
}
if server == nil || server.URL == "" {
return answer, fmt.Errorf("No chat server URL found!")
}
it.URL = server.URL
if server.Kind != "" {
it.Kind = server.Kind
}
answer = true
it.DeveloperChannel, err = util.PickValue("Developer channel: ", it.DeveloperChannel, false, "", o.In, o.Out, o.Err)
if err != nil {
return answer, err
}
it.UserChannel, err = util.PickValue("User channel: ", it.UserChannel, false, "", o.In, o.Out, o.Err)
if err != nil {
return answer, err
}
return answer, nil
}