@@ -30,6 +30,7 @@ import (
30
30
"github.com/argoproj-labs/argocd-autopilot/pkg/application"
31
31
"github.com/argoproj-labs/argocd-autopilot/pkg/fs"
32
32
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
33
+ apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store"
33
34
aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
34
35
wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
35
36
wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
@@ -49,10 +50,17 @@ type (
49
50
}
50
51
51
52
GitSourceDeleteOptions struct {
52
- RuntimeName string
53
- GsName string
54
- CloneOpts * git.CloneOptions
55
- Timeout time.Duration
53
+ RuntimeName string
54
+ GsName string
55
+ InsCloneOpts * git.CloneOptions
56
+ Timeout time.Duration
57
+ }
58
+
59
+ GitSourceEditOptions struct {
60
+ RuntimeName string
61
+ GsName string
62
+ InsCloneOpts * git.CloneOptions
63
+ GsCloneOpts * git.CloneOptions
56
64
}
57
65
)
58
66
@@ -70,6 +78,7 @@ func NewGitSourceCommand() *cobra.Command {
70
78
cmd .AddCommand (NewGitSourceCreateCommand ())
71
79
cmd .AddCommand (NewGitSourceListCommand ())
72
80
cmd .AddCommand (NewGitSourceDeleteCommand ())
81
+ cmd .AddCommand (NewGitSourceEditCommand ())
73
82
74
83
return cmd
75
84
}
@@ -189,7 +198,7 @@ func RunGitSourceList(runtimeName string) error {
189
198
190
199
func NewGitSourceDeleteCommand () * cobra.Command {
191
200
var (
192
- cloneOpts * git.CloneOptions
201
+ insCloneOpts * git.CloneOptions
193
202
)
194
203
195
204
cmd := & cobra.Command {
@@ -209,27 +218,116 @@ func NewGitSourceDeleteCommand() *cobra.Command {
209
218
log .G (ctx ).Fatal ("must enter git-source name" )
210
219
}
211
220
212
- cloneOpts .Parse ()
221
+ insCloneOpts .Parse ()
213
222
},
214
223
RunE : func (cmd * cobra.Command , args []string ) error {
215
224
ctx := cmd .Context ()
216
225
217
226
return RunDeleteGitSource (ctx , & GitSourceDeleteOptions {
218
- RuntimeName : args [0 ],
219
- GsName : args [1 ],
220
- Timeout : aputil .MustParseDuration (cmd .Flag ("request-timeout" ).Value .String ()),
221
- CloneOpts : cloneOpts ,
227
+ RuntimeName : args [0 ],
228
+ GsName : args [1 ],
229
+ Timeout : aputil .MustParseDuration (cmd .Flag ("request-timeout" ).Value .String ()),
230
+ InsCloneOpts : insCloneOpts ,
222
231
})
223
232
},
224
233
}
225
234
226
- cloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
235
+ insCloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
227
236
FS : memfs .New (),
228
237
})
229
238
230
239
return cmd
231
240
}
232
241
242
+ func NewGitSourceEditCommand () * cobra.Command {
243
+ var (
244
+ insCloneOpts * git.CloneOptions
245
+ gsCloneOpts * git.CloneOptions
246
+ )
247
+
248
+ cmd := & cobra.Command {
249
+ Use : "edit runtime_name git-source_name" ,
250
+ Short : "edit a git-source of a runtime" ,
251
+ Example : util .Doc (`
252
+ <BIN> git-source edit runtime_name git-source_name --git-src-repo https://github.com/owner/repo-name/my-workflow
253
+ ` ),
254
+ PreRun : func (cmd * cobra.Command , args []string ) {
255
+ ctx := cmd .Context ()
256
+
257
+ if len (args ) < 1 {
258
+ log .G (ctx ).Fatal ("must enter a runtime name" )
259
+ }
260
+
261
+ if len (args ) < 2 {
262
+ log .G (ctx ).Fatal ("must enter a git-source name" )
263
+ }
264
+
265
+ if gsCloneOpts .Repo == "" {
266
+ log .G (ctx ).Fatal ("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name/path/to/workflow" )
267
+ }
268
+
269
+ insCloneOpts .Parse ()
270
+ gsCloneOpts .Parse ()
271
+ },
272
+ RunE : func (cmd * cobra.Command , args []string ) error {
273
+ ctx := cmd .Context ()
274
+
275
+ return RunEditGitSource (ctx , & GitSourceEditOptions {
276
+ RuntimeName : args [0 ],
277
+ GsName : args [1 ],
278
+ InsCloneOpts : insCloneOpts ,
279
+ GsCloneOpts : gsCloneOpts ,
280
+ })
281
+ },
282
+ }
283
+
284
+ insCloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
285
+ CreateIfNotExist : true ,
286
+ FS : memfs .New (),
287
+ })
288
+
289
+ gsCloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
290
+ Prefix : "git-src" ,
291
+ Optional : true ,
292
+ CreateIfNotExist : true ,
293
+ FS : memfs .New (),
294
+ })
295
+
296
+ return cmd
297
+ }
298
+
299
+ func RunEditGitSource (ctx context.Context , opts * GitSourceEditOptions ) error {
300
+ repo , fs , err := opts .InsCloneOpts .GetRepo (ctx )
301
+ if err != nil {
302
+ return fmt .Errorf ("failed to clone the installation repo, attemptint to edit git-source %s. Err: %w" , opts .GsName , err )
303
+ }
304
+
305
+ c := & application.Config {}
306
+ err = fs .ReadJson (fs .Join (apstore .Default .AppsDir , opts .GsName , opts .RuntimeName , "config.json" ), c )
307
+ if err != nil {
308
+ return fmt .Errorf ("failed to read the config.json of git-source: %s. Err: %w" , opts .GsName , err )
309
+ }
310
+
311
+ c .SrcPath = opts .GsCloneOpts .Path ()
312
+ c .SrcRepoURL = opts .GsCloneOpts .URL ()
313
+ c .SrcTargetRevision = opts .GsCloneOpts .Revision ()
314
+
315
+ err = fs .WriteJson (fs .Join (apstore .Default .AppsDir , opts .GsName , opts .RuntimeName , "config.json" ), c )
316
+ if err != nil {
317
+ return fmt .Errorf ("failed to write the updated config.json of git-source: %s. Err: %w" , opts .GsName , err )
318
+ }
319
+
320
+ _ , err = repo .Persist (ctx , & git.PushOptions {
321
+ CommitMsg : "Persisted an updated git-source" ,
322
+ })
323
+
324
+ if err != nil {
325
+ return fmt .Errorf ("failed to persist the updated git-source: %s. Err: %w" , opts .GsName , err )
326
+ }
327
+
328
+ return nil
329
+ }
330
+
233
331
func RunCreateGitSource (ctx context.Context , opts * GitSourceCreateOptions ) error {
234
332
gsRepo , gsFs , err := opts .gsCloneOpts .GetRepo (ctx )
235
333
if err != nil {
@@ -264,24 +362,24 @@ func RunCreateGitSource(ctx context.Context, opts *GitSourceCreateOptions) error
264
362
return fmt .Errorf ("failed to create git-source application. Err: %w" , err )
265
363
}
266
364
267
- log .G (ctx ).Infof ("done creating a new git-source: '%s'" , opts .gsName )
365
+ log .G (ctx ).Infof ("Successfully created the git-source: '%s'" , opts .gsName )
268
366
269
367
return nil
270
368
}
271
369
272
370
func RunDeleteGitSource (ctx context.Context , opts * GitSourceDeleteOptions ) error {
273
371
err := apcmd .RunAppDelete (ctx , & apcmd.AppDeleteOptions {
274
- CloneOpts : opts .CloneOpts ,
372
+ CloneOpts : opts .InsCloneOpts ,
275
373
ProjectName : opts .RuntimeName ,
276
374
AppName : opts .GsName ,
277
375
Global : false ,
278
376
})
279
377
280
378
if err != nil {
281
- return fmt .Errorf ("failed to delete git-source %s. Err: %w" , opts .GsName , err )
379
+ return fmt .Errorf ("failed to delete the git-source %s. Err: %w" , opts .GsName , err )
282
380
}
283
381
284
- log .G (ctx ).Debug ("successfully deleted git-source: %s" , opts .GsName )
382
+ log .G (ctx ).Debug ("Successfully deleted the git-source: %s" , opts .GsName )
285
383
286
384
return nil
287
385
}
0 commit comments