/
plugins.go
283 lines (226 loc) · 7.22 KB
/
plugins.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
282
283
package cmd
import (
"encoding/json"
"fmt"
"strings"
"github.com/nikhilsbhat/common/content"
"github.com/nikhilsbhat/gocd-cli/pkg/errors"
"github.com/nikhilsbhat/gocd-cli/pkg/query"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
type goCdPlugin struct {
pluginID string
groovy bool
json bool
yaml bool
}
var goCdPluginObj goCdPlugin
func registerPluginsCommand() *cobra.Command {
pluginCommand := &cobra.Command{
Use: "plugin",
Short: "Command to operate on plugins present in GoCD",
Long: `Command leverages GoCD config repo apis' [https://api.gocd.org/current/#plugin-settings, https://api.gocd.org/current/#plugin-info] to
GET/CREATE/UPDATE plugins settings or information`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
}
pluginCommand.SetUsageTemplate(getUsageTemplate())
pluginCommand.AddCommand(getPluginSettingsCommand())
pluginCommand.AddCommand(updatePluginSettingsCommand())
pluginCommand.AddCommand(createPluginSettingsCommand())
pluginCommand.AddCommand(getPluginsInfoCommand())
pluginCommand.AddCommand(getPluginInfoCommand())
pluginCommand.AddCommand(listPluginsCommand())
for _, command := range pluginCommand.Commands() {
command.SilenceUsage = true
}
return pluginCommand
}
func getPluginSettingsCommand() *cobra.Command {
getPluginSettingsCmd := &cobra.Command{
Use: "get-settings",
Short: "Command to GET settings of a specific plugin present in GoCD [https://api.gocd.org/current/#get-plugin-settings]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetPluginSettings(args[0])
if err != nil {
return err
}
if len(jsonQuery) != 0 {
cliLogger.Debugf(queryEnabledMessage, jsonQuery)
baseQuery, err := query.SetQuery(response, jsonQuery)
if err != nil {
return err
}
cliLogger.Debugf(baseQuery.Print())
return cliRenderer.Render(baseQuery.RunQuery())
}
return cliRenderer.Render(response)
},
}
getPluginSettingsCmd.SetUsageTemplate(getUsageTemplate())
return getPluginSettingsCmd
}
func createPluginSettingsCommand() *cobra.Command {
createPluginSettingsCmd := &cobra.Command{
Use: "create-settings",
Short: "Command to CREATE settings of a specified plugin present in GoCD [https://api.gocd.org/current/#create-plugin-settings]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
var setting gocd.PluginSettings
object, err := readObject(cmd)
if err != nil {
return err
}
switch objType := object.CheckFileType(cliLogger); objType {
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &setting); err != nil {
return err
}
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &setting); err != nil {
return err
}
default:
return &errors.UnknownObjectTypeError{Name: objType}
}
response, err := client.CreatePluginSettings(setting)
if err != nil {
return err
}
if err = cliRenderer.Render(fmt.Sprintf("setting for plugin %s created successfully", setting.ID)); err != nil {
return err
}
return cliRenderer.Render(response)
},
}
createPluginSettingsCmd.SetUsageTemplate(getUsageTemplate())
return createPluginSettingsCmd
}
func updatePluginSettingsCommand() *cobra.Command {
updatePluginSettingsCmd := &cobra.Command{
Use: "update-settings",
Short: "Command to UPDATE settings of a specified plugin present in GoCD [https://api.gocd.org/current/#update-plugin-settings]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
var setting gocd.PluginSettings
object, err := readObject(cmd)
if err != nil {
return err
}
switch objType := object.CheckFileType(cliLogger); objType {
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &setting); err != nil {
return err
}
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &setting); err != nil {
return err
}
default:
return &errors.UnknownObjectTypeError{Name: objType}
}
pluginSettingsFetched, err := client.GetPluginSettings(setting.ID)
if err != nil {
return err
}
cliShellReadConfig.ShellMessage = fmt.Sprintf(updateMessage, "pipeline-settings", setting.ID)
existing, err := diffCfg.String(pluginSettingsFetched)
if err != nil {
return err
}
if err = cliCfg.CheckDiffAndAllow(existing, object.String()); err != nil {
return err
}
response, err := client.UpdatePluginSettings(setting)
if err != nil {
return err
}
if err = cliRenderer.Render(fmt.Sprintf("setting for plugin %s updated successfully", setting.ID)); err != nil {
return err
}
return cliRenderer.Render(response)
},
}
updatePluginSettingsCmd.SetUsageTemplate(getUsageTemplate())
return updatePluginSettingsCmd
}
func getPluginsInfoCommand() *cobra.Command {
getPluginInformationCmd := &cobra.Command{
Use: "get-info-all",
Short: "Command to GET information of all plugins present in GoCD [https://api.gocd.org/current/#get-all-plugin-info]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetPluginsInfo()
if err != nil {
return err
}
if len(jsonQuery) != 0 {
cliLogger.Debugf(queryEnabledMessage, jsonQuery)
baseQuery, err := query.SetQuery(response, jsonQuery)
if err != nil {
return err
}
cliLogger.Debugf(baseQuery.Print())
return cliRenderer.Render(baseQuery.RunQuery())
}
return cliRenderer.Render(response)
},
}
getPluginInformationCmd.SetUsageTemplate(getUsageTemplate())
return getPluginInformationCmd
}
func getPluginInfoCommand() *cobra.Command {
getPluginInofCmd := &cobra.Command{
Use: "get-info",
Short: "Command to GET information of a specific plugin present in GoCD [https://api.gocd.org/current/#get-plugin-info]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetPluginInfo(args[0])
if err != nil {
return err
}
if len(jsonQuery) != 0 {
cliLogger.Debugf(queryEnabledMessage, jsonQuery)
baseQuery, err := query.SetQuery(response, jsonQuery)
if err != nil {
return err
}
cliLogger.Debugf(baseQuery.Print())
return cliRenderer.Render(baseQuery.RunQuery())
}
return cliRenderer.Render(response)
},
}
getPluginInofCmd.SetUsageTemplate(getUsageTemplate())
return getPluginInofCmd
}
func listPluginsCommand() *cobra.Command {
listPluginsCmd := &cobra.Command{
Use: "list",
Short: "Command to LIST all plugins present in GoCD",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetPluginsInfo()
if err != nil {
return err
}
var pluginList []string
for _, plugin := range response.Plugins {
pluginList = append(pluginList, plugin.ID)
}
return cliRenderer.Render(strings.Join(pluginList, "\n"))
},
}
listPluginsCmd.SetUsageTemplate(getUsageTemplate())
return listPluginsCmd
}